diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2003-06-25 17:49:23 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2003-06-25 17:49:23 +0000 |
commit | b58c2bdb0a4daa62a6dd41ed6ccf35984541ff10 (patch) | |
tree | 3ce734ef986ba5f427df7877ad7e8a50ab5404ba /usr.bin | |
parent | 325ed9f2091ba4a40f167a225dea4a812abec807 (diff) |
o use getopt()
o use err/warn
o only call done() when needed (after mkstemp)
o add "-C lines" like GNU grep
OK deraadt@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/diff/diff.c | 205 | ||||
-rw-r--r-- | usr.bin/diff/diff.h | 15 | ||||
-rw-r--r-- | usr.bin/diff/diffdir.c | 18 | ||||
-rw-r--r-- | usr.bin/diff/diffreg.c | 33 |
4 files changed, 114 insertions, 157 deletions
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index 65df89d0fc7..4eb8d4b371b 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.c,v 1.5 2003/06/25 07:26:59 tedu Exp $ */ +/* $OpenBSD: diff.c,v 1.6 2003/06/25 17:49:22 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -53,144 +53,101 @@ char diffh[] = _PATH_DIFFH; char pr[] = _PATH_PR; static void noroom(void); +__dead void usage(void); int main(int argc, char **argv) { - char *argp; + int ch; ifdef1 = "FILE1"; ifdef2 = "FILE2"; status = 2; diffargv = argv; - argc--, argv++; - while (argc > 2 && argv[0][0] == '-') { - argp = &argv[0][1]; - argv++, argc--; - while (*argp) - switch (*argp++) { -#ifdef notdef - case 'I': - opt = D_IFDEF; - wantelses = 0; - continue; - case 'E': - opt = D_IFDEF; - wantelses = 1; - continue; - case '1': - opt = D_IFDEF; - ifdef1 = argp; - *--argp = 0; - continue; -#endif - case 'D': - /* -Dfoo = -E -1 -2foo */ - wantelses = 1; - ifdef1 = ""; - /* fall through */ -#ifdef notdef - case '2': -#endif - opt = D_IFDEF; - ifdef2 = argp; - *--argp = 0; - continue; - case 'e': - opt = D_EDIT; - continue; - case 'f': - opt = D_REVERSE; - continue; - case 'n': - opt = D_NREVERSE; - continue; - case 'b': - bflag = 1; - continue; - case 'w': - wflag = 1; - continue; - case 'i': - iflag = 1; - continue; - case 't': - tflag = 1; - continue; - case 'c': - opt = D_CONTEXT; - if (isdigit(*argp)) { - context = atoi(argp); - while (isdigit(*argp)) - argp++; - if (*argp) { - fprintf(stderr, - "diff: -c: bad count\n"); - done(0); - } - argp = ""; - } else - context = 3; - continue; - case 'h': - hflag++; - continue; - case 'S': - if (*argp == 0) { - fprintf(stderr, "diff: use -Sstart\n"); - done(0); - } - start = argp; - *--argp = 0; /* don't pass it on */ - continue; - case 'r': - rflag++; - continue; - case 's': - sflag++; - continue; - case 'l': - lflag++; - continue; - default: - fprintf(stderr, "diff: -%s: unknown option\n", - --argp); - done(0); - } - } - if (argc != 2) { - fprintf(stderr, "diff: two filename arguments required\n"); - done(0); + + while ((ch = getopt(argc, argv, "bC:cDefhilnrS:stw")) != -1) { + switch (ch) { + case 'b': + bflag++; + break; + case 'C': + opt = D_CONTEXT; + if (!isdigit(*optarg)) + usage(); + context = atoi(optarg); /* XXX - use strtol */ + break; + case 'c': + opt = D_CONTEXT; + context = 3; + break; + case 'D': + /* -Dfoo = -E -1 -2foo */ + opt = D_IFDEF; + ifdef1 = ""; + ifdef2 = optarg; + wantelses++; + break; + case 'e': + opt = D_EDIT; + break; + case 'f': + opt = D_REVERSE; + break; + case 'h': + hflag++; + break; + case 'i': + iflag++; + break; + case 'l': + lflag++; + break; + case 'n': + opt = D_NREVERSE; + break; + case 'r': + opt = D_REVERSE; + break; + case 'S': + start = optarg; + break; + case 's': + sflag++; + break; + case 't': + tflag++; + break; + case 'w': + wflag++; + break; + default: + usage(); + break; + } } + argc -= optind; + argv += optind; + + if (argc != 2) + errx(1, "two filename arguments required"); file1 = argv[0]; file2 = argv[1]; - if (hflag && opt) { - fprintf(stderr, - "diff: -h doesn't support -e, -f, -n, -c, or -I\n"); - done(0); - } + if (hflag && opt) + errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I or -n"); if (!strcmp(file1, "-")) stb1.st_mode = S_IFREG; - else if (stat(file1, &stb1) < 0) { - fprintf(stderr, "diff: "); - perror(file1); - done(0); - } + else if (stat(file1, &stb1) < 0) + err(1, "%s", file1); if (!strcmp(file2, "-")) stb2.st_mode = S_IFREG; - else if (stat(file2, &stb2) < 0) { - fprintf(stderr, "diff: "); - perror(file2); - done(0); - } + else if (stat(file2, &stb2) < 0) + err(1, "%s", file2); if ((stb1.st_mode & S_IFMT) == S_IFDIR && (stb2.st_mode & S_IFMT) == S_IFDIR) { diffdir(argv); } else diffreg(); done(0); - /* notreached */ - return (0); } int @@ -207,7 +164,7 @@ max(int a, int b) return (a > b ? a : b); } -void +__dead void done(int sig) { if (tempfile) @@ -240,6 +197,16 @@ ralloc(void *p, size_t n) static void noroom(void) { - fprintf(stderr, "diff: files too big, try -h\n"); + warn("files too big, try -h"); done(0); } + +__dead void +usage(void) +{ + (void)fprintf(stderr, "usage: diff [-c | -C lines | -e | -f | -h | -n ] [-biwt] file1 file2\n" + "usage: diff [-Dstring] [-biw] file1 file2\n" + "usage: diff [-l] [-r] [-s] [-c | -C lines | -e | -f | -h | -n ] [-biwt]\n [-Sname] dir1 dir2\n"); + + exit(1); +} diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index f8a373a88c8..b49f5cd8264 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.h,v 1.5 2003/06/25 07:26:59 tedu Exp $ */ +/* $OpenBSD: diff.h,v 1.6 2003/06/25 17:49:22 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -40,12 +40,14 @@ * diff - common declarations */ -#include <stdio.h> -#include <ctype.h> #include <sys/param.h> #include <sys/stat.h> #include <sys/dir.h> + +#include <ctype.h> +#include <err.h> #include <signal.h> +#include <stdio.h> /* * Output format options @@ -56,8 +58,9 @@ int opt; #define D_EDIT -1 /* Editor script out */ #define D_REVERSE 1 /* Reverse editor script */ #define D_CONTEXT 2 /* Diff with context */ -#define D_IFDEF 3 /* Diff with merged #ifdef's */ -#define D_NREVERSE 4 /* Reverse ed script with numbered +#define D_UNIFIED 3 /* Unified context diff */ +#define D_IFDEF 4 /* Diff with merged #ifdef's */ +#define D_NREVERSE 5 /* Reverse ed script with numbered lines and no trailing . */ int tflag; /* expand tabs on output */ @@ -117,10 +120,10 @@ void *talloc(size_t); void *ralloc(void *, size_t); char *splice(char *, char *); char *copytemp(void); -void done(int); void diffdir(char **); void diffreg(void); int max(int, int); int min(int, int); +__dead void done(int); extern char diffh[], diff[], pr[]; diff --git a/usr.bin/diff/diffdir.c b/usr.bin/diff/diffdir.c index 7d7ab6913c0..12236c84e19 100644 --- a/usr.bin/diff/diffdir.c +++ b/usr.bin/diff/diffdir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffdir.c,v 1.10 2003/06/25 03:50:27 deraadt Exp $ */ +/* $OpenBSD: diffdir.c,v 1.11 2003/06/25 17:49:22 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -91,13 +91,10 @@ diffdir(char **argv) struct dir *d1, *d2; int i, cmp; - if (opt == D_IFDEF) { - fprintf(stderr, "diff: can't specify -I with directories\n"); - done(0); - } + if (opt == D_IFDEF) + warnx("can't specify -I with directories"); if (opt == D_EDIT && (sflag || lflag)) - fprintf(stderr, - "diff: warning: shouldn't give -s or -l with -e\n"); + warnx("warning: shouldn't give -s or -l with -e"); strlcpy(title, "diff ", sizeof title); for (i = 1; diffargv[i + 2]; i++) { if (!strcmp(diffargv[i], "-")) @@ -230,8 +227,7 @@ setupdir(char *cp) dirp = opendir(cp); if (dirp == NULL) { - fprintf(stderr, "diff: "); - perror(cp); + warn("%s", cp); done(0); } nitems = 0; @@ -370,7 +366,7 @@ calldiff(char *wantpr) pipe(pv); pid = fork(); if (pid == -1) { - fprintf(stderr, "No more processes"); + warnx("No more processes"); done(0); } if (pid == 0) { @@ -386,7 +382,7 @@ calldiff(char *wantpr) } pid = fork(); if (pid == -1) { - fprintf(stderr, "diff: No more processes\n"); + warnx("No more processes"); done(0); } if (pid == 0) { diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index 332033afead..195002401b3 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diffreg.c,v 1.14 2003/06/25 07:26:59 tedu Exp $ */ +/* $OpenBSD: diffreg.c,v 1.15 2003/06/25 17:49:22 millert Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -231,52 +231,45 @@ diffreg(void) if (hflag) { diffargv[0] = "diffh"; execv(diffh, diffargv); - fprintf(stderr, "diff: "); - perror(diffh); + warn("%s", diffh); done(0); } chrtran = (iflag ? cup2low : clow2low); if ((stb1.st_mode & S_IFMT) == S_IFDIR) { file1 = splice(file1, file2); if (stat(file1, &stb1) < 0) { - fprintf(stderr, "diff: "); - perror(file1); + warn("%s", file1); done(0); } } else if ((stb2.st_mode & S_IFMT) == S_IFDIR) { file2 = splice(file2, file1); if (stat(file2, &stb2) < 0) { - fprintf(stderr, "diff: "); - perror(file2); + warn("%s", file2); done(0); } } else if ((stb1.st_mode & S_IFMT) != S_IFREG || !strcmp(file1, "-")) { if (!strcmp(file2, "-")) { - fprintf(stderr, "diff: can't specify - -\n"); + warnx("can't specify - -"); done(0); } file1 = copytemp(); if (stat(file1, &stb1) < 0) { - fprintf(stderr, "diff: "); - perror(file1); + warn("%s", file1); done(0); } } else if ((stb2.st_mode & S_IFMT) != S_IFREG || !strcmp(file2, "-")) { file2 = copytemp(); if (stat(file2, &stb2) < 0) { - fprintf(stderr, "diff: "); - perror(file2); + warn("%s", file2); done(0); } } if ((f1 = fopen(file1, "r")) == NULL) { - fprintf(stderr, "diff: "); - perror(file1); + warn("%s", file1); done(0); } if ((f2 = fopen(file2, "r")) == NULL) { - fprintf(stderr, "diff: "); - perror(file2); + warn("%s", file2); fclose(f1); done(0); } @@ -360,14 +353,12 @@ copytemp(void) signal(SIGTERM, done); f = mkstemp(tempfile); if (f < 0) { - fprintf(stderr, "diff: "); - perror(tempfile); + warn("%s", tempfile); done(0); } while ((i = read(0, buf, BUFSIZ)) > 0) if (write(f, buf, i) != i) { - fprintf(stderr, "diff: "); - perror(tempfile); + warn("%s", tempfile); done(0); } close(f); @@ -380,7 +371,7 @@ splice(char *dir, char *file) char *tail, buf[BUFSIZ]; if (!strcmp(file, "-")) { - fprintf(stderr, "diff: can't specify - with other arg directory\n"); + warnx("can't specify - with other arg directory"); done(0); } tail = strrchr(file, '/'); |