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/diff/diff.c | |
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/diff/diff.c')
-rw-r--r-- | usr.bin/diff/diff.c | 205 |
1 files changed, 86 insertions, 119 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); +} |