update cmp, grep

This commit is contained in:
Connor Lane Smith 2011-06-18 06:42:24 +01:00
parent f24772dcbb
commit 8ec404cdec
3 changed files with 27 additions and 33 deletions

27
cmp.c
View File

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

4
grep.1
View File

@ -8,7 +8,9 @@ grep \- search files for a pattern
.RI [ file ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION
.B grep .B grep
searches the input files for lines that match the pattern, a regular expression as defined in searches the input files for lines that match the
.IR pattern ,
a regular expression as defined in
.IR regex (7). .IR regex (7).
By default each matching line is printed to stdout. If no file is given, grep By default each matching line is printed to stdout. If no file is given, grep
reads from stdin. reads from stdin.

29
grep.c
View File

@ -5,6 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "util.h"
enum { Match = 0, NoMatch = 1, Error = 2 };
static void grep(FILE *, const char *, regex_t *); static void grep(FILE *, const char *, regex_t *);
@ -39,27 +42,22 @@ main(int argc, char *argv[])
vflag = true; vflag = true;
break; break;
default: default:
exit(2); exit(Error);
} }
if(optind == argc) { if(optind == argc)
fprintf(stderr, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]); enprintf(Error, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]);
exit(2);
}
regcomp(&preg, argv[optind++], flags); regcomp(&preg, argv[optind++], flags);
many = (argc > optind+1); many = (argc > optind+1);
if(optind == argc) if(optind == argc)
grep(stdin, "<stdin>", &preg); grep(stdin, "<stdin>", &preg);
else for(; optind < argc; optind++) { else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[optind], "r"))) { if(!(fp = fopen(argv[optind], "r")))
fprintf(stderr, "fopen %s: ", argv[optind]); enprintf(Error, "fopen %s:", argv[optind]);
perror(NULL);
exit(2);
}
grep(fp, argv[optind], &preg); grep(fp, argv[optind], &preg);
fclose(fp); fclose(fp);
} }
return match ? 0 : 1; return match ? Match : NoMatch;
} }
void void
@ -80,7 +78,7 @@ grep(FILE *fp, const char *str, regex_t *preg)
puts(str); puts(str);
goto end; goto end;
case 'q': case 'q':
exit(0); exit(Match);
default: default:
if(many) if(many)
printf("%s:", str); printf("%s:", str);
@ -94,10 +92,7 @@ grep(FILE *fp, const char *str, regex_t *preg)
if(mode == 'c') if(mode == 'c')
printf("%ld\n", c); printf("%ld\n", c);
end: end:
if(ferror(fp)) { if(ferror(fp))
fprintf(stderr, "%s: read error: ", str); enprintf(Error, "%s: read error:", str);
perror(NULL);
exit(2);
}
free(buf); free(buf);
} }