update cmp, grep
This commit is contained in:
27
cmp.c
27
cmp.c
@@ -3,6 +3,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "util.h"
|
||||
|
||||
enum { Same = 0, Diff = 1, Error = 2 };
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
@@ -24,37 +27,31 @@ main(int argc, char *argv[])
|
||||
sflag = true;
|
||||
break;
|
||||
default:
|
||||
exit(2);
|
||||
exit(Error);
|
||||
}
|
||||
if(optind != argc-2) {
|
||||
fprintf(stderr, "usage: %s [-ls] file1 file2\n", argv[0]);
|
||||
exit(2);
|
||||
}
|
||||
if(optind != argc-2)
|
||||
enprintf(Error, "usage: %s [-ls] file1 file2\n", argv[0]);
|
||||
for(i = 0; i < 2; i++)
|
||||
if(!(fp[i] = fopen(argv[optind+i], "r"))) {
|
||||
fprintf(stderr, "fopen %s:", argv[optind+i]);
|
||||
exit(2);
|
||||
}
|
||||
if(!(fp[i] = fopen(argv[optind+i], "r")))
|
||||
enprintf(Error, "fopen %s:", argv[optind+i]);
|
||||
for(n = 1; ((b[0] = getc(fp[0])) != EOF) | ((b[1] = getc(fp[1])) != EOF); n++) {
|
||||
if(b[0] == '\n')
|
||||
line++;
|
||||
if(b[0] == b[1])
|
||||
continue;
|
||||
for(i = 0; i < 2; i++)
|
||||
if(b[i] == EOF) {
|
||||
fprintf(stderr, "cmp: EOF on %s\n", argv[optind+i]);
|
||||
return 1;
|
||||
}
|
||||
if(b[i] == EOF)
|
||||
enprintf(Diff, "cmp: EOF on %s\n", argv[optind+i]);
|
||||
if(!lflag) {
|
||||
if(!sflag)
|
||||
printf("%s %s differ: char %ld, line %ld\n",
|
||||
argv[optind], argv[optind+1], n, line);
|
||||
return 1;
|
||||
exit(Diff);
|
||||
}
|
||||
else {
|
||||
printf("%4ld %3o %3o\n", n, b[0], b[1]);
|
||||
same = false;
|
||||
}
|
||||
}
|
||||
return same ? 0 : 1;
|
||||
return same ? Same : Diff;
|
||||
}
|
||||
|
Reference in New Issue
Block a user