sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 8ec404cdec24bc9af7ebb63f5ac4835e7bf7d623
parent f24772dcbb411c631b31cae61717a60c8568bc28
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Sat, 18 Jun 2011 06:42:24 +0100

update cmp, grep
Diffstat:
Mcmp.c | 27++++++++++++---------------
Mgrep.1 | 4+++-
Mgrep.c | 29++++++++++++-----------------
3 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/cmp.c b/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; } diff --git a/grep.1 b/grep.1 @@ -8,7 +8,9 @@ grep \- search files for a pattern .RI [ file ...] .SH DESCRIPTION .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). By default each matching line is printed to stdout. If no file is given, grep reads from stdin. diff --git a/grep.c b/grep.c @@ -5,6 +5,9 @@ #include <stdlib.h> #include <unistd.h> #include "text.h" +#include "util.h" + +enum { Match = 0, NoMatch = 1, Error = 2 }; static void grep(FILE *, const char *, regex_t *); @@ -39,27 +42,22 @@ main(int argc, char *argv[]) vflag = true; break; default: - exit(2); + exit(Error); } - if(optind == argc) { - fprintf(stderr, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]); - exit(2); - } + if(optind == argc) + enprintf(Error, "usage: %s [-Ecilnqv] pattern [files...]\n", argv[0]); regcomp(&preg, argv[optind++], flags); many = (argc > optind+1); if(optind == argc) grep(stdin, "<stdin>", &preg); else for(; optind < argc; optind++) { - if(!(fp = fopen(argv[optind], "r"))) { - fprintf(stderr, "fopen %s: ", argv[optind]); - perror(NULL); - exit(2); - } + if(!(fp = fopen(argv[optind], "r"))) + enprintf(Error, "fopen %s:", argv[optind]); grep(fp, argv[optind], &preg); fclose(fp); } - return match ? 0 : 1; + return match ? Match : NoMatch; } void @@ -80,7 +78,7 @@ grep(FILE *fp, const char *str, regex_t *preg) puts(str); goto end; case 'q': - exit(0); + exit(Match); default: if(many) printf("%s:", str); @@ -94,10 +92,7 @@ grep(FILE *fp, const char *str, regex_t *preg) if(mode == 'c') printf("%ld\n", c); end: - if(ferror(fp)) { - fprintf(stderr, "%s: read error: ", str); - perror(NULL); - exit(2); - } + if(ferror(fp)) + enprintf(Error, "%s: read error:", str); free(buf); }