Annotation of src/usr.bin/diff/diff.c, Revision 1.21
1.21 ! millert 1: /* $OpenBSD: diff.c,v 1.20 2003/07/04 17:37:07 millert Exp $ */
1.2 deraadt 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36:
1.15 millert 37: #include <errno.h>
1.3 tedu 38: #include <stdlib.h>
1.15 millert 39: #include <stdarg.h>
1.18 david 40: #include <string.h>
1.3 tedu 41: #include <unistd.h>
1.1 deraadt 42:
43: #include "diff.h"
44: #include "pathnames.h"
45:
1.3 tedu 46: #if 0
47: static char const sccsid[] = "@(#)diff.c 4.7 5/11/89";
48: #endif
49:
1.1 deraadt 50: /*
51: * diff - driver and subroutines
52: */
1.12 tedu 53: int opt;
54: int aflag; /* treat all files as text */
55: int tflag; /* expand tabs on output */
56: /* Algorithm related options. */
57: int bflag; /* ignore blanks in comparisons */
58: int wflag; /* totally ignore blanks in comparisons */
59: int iflag; /* ignore case in comparisons */
60: /* Options on hierarchical diffs. */
61: int rflag; /* recursively trace directories */
62: int sflag; /* announce files which are same */
63: char *start; /* do file only if name >= this */
1.19 millert 64: /* Variable for -D D_IFDEF option. */
1.17 millert 65: char *ifdefname; /* What we will print for #ifdef/#endif */
1.12 tedu 66: /* Variables for -c and -u context option. */
67: int context; /* lines of context to be printed */
68: /* State for exit status. */
69: int status;
70: int anychange;
71: /* Variables for diffdir. */
72: char **diffargv; /* option list to pass to recursive diffs */
73:
74: /*
75: * Input file names.
1.19 millert 76: * With diffdir, file1 and file2 are allocated MAXPATHLEN space,
77: * and padded with a '/', and then efile1 and efile2 point after
1.12 tedu 78: * the '/'.
79: */
80: char *file1, *file2, *efile1, *efile2;
81: struct stat stb1, stb2;
1.1 deraadt 82:
1.6 millert 83: __dead void usage(void);
1.3 tedu 84:
85: int
86: main(int argc, char **argv)
1.1 deraadt 87: {
1.6 millert 88: int ch;
1.1 deraadt 89:
90: status = 2;
91: diffargv = argv;
1.6 millert 92:
1.21 ! millert 93: while ((ch = getopt(argc, argv, "abC:cD:efinrS:stU:uw")) != -1) {
1.6 millert 94: switch (ch) {
1.12 tedu 95: case 'a':
96: aflag++;
97: break;
1.6 millert 98: case 'b':
99: bflag++;
100: break;
101: case 'C':
102: opt = D_CONTEXT;
103: if (!isdigit(*optarg))
104: usage();
105: context = atoi(optarg); /* XXX - use strtol */
106: break;
107: case 'c':
108: opt = D_CONTEXT;
109: context = 3;
110: break;
111: case 'D':
112: opt = D_IFDEF;
1.17 millert 113: ifdefname = optarg;
1.6 millert 114: break;
115: case 'e':
116: opt = D_EDIT;
117: break;
118: case 'f':
119: opt = D_REVERSE;
120: break;
121: case 'i':
122: iflag++;
123: break;
124: case 'n':
125: opt = D_NREVERSE;
126: break;
127: case 'r':
1.16 millert 128: rflag++;
1.6 millert 129: break;
130: case 'S':
131: start = optarg;
132: break;
133: case 's':
134: sflag++;
135: break;
136: case 't':
137: tflag++;
138: break;
1.9 millert 139: case 'U':
140: opt = D_UNIFIED;
141: if (!isdigit(*optarg))
142: usage();
143: context = atoi(optarg); /* XXX - use strtol */
144: break;
145: case 'u':
146: opt = D_UNIFIED;
147: context = 3;
148: break;
1.6 millert 149: case 'w':
150: wflag++;
151: break;
152: default:
153: usage();
154: break;
155: }
1.1 deraadt 156: }
1.6 millert 157: argc -= optind;
158: argv += optind;
159:
160: if (argc != 2)
1.15 millert 161: errorx("two filename arguments required");
1.1 deraadt 162: file1 = argv[0];
163: file2 = argv[1];
164: if (!strcmp(file1, "-"))
165: stb1.st_mode = S_IFREG;
1.6 millert 166: else if (stat(file1, &stb1) < 0)
1.15 millert 167: error("%s", file1);
1.1 deraadt 168: if (!strcmp(file2, "-"))
169: stb2.st_mode = S_IFREG;
1.6 millert 170: else if (stat(file2, &stb2) < 0)
1.15 millert 171: error("%s", file2);
1.8 millert 172: if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
1.1 deraadt 173: diffdir(argv);
1.8 millert 174: else
1.1 deraadt 175: diffreg();
1.4 deraadt 176: done(0);
1.1 deraadt 177: }
178:
1.3 tedu 179: int
180: min(int a, int b)
1.1 deraadt 181: {
182:
183: return (a < b ? a : b);
184: }
185:
1.3 tedu 186: int
187: max(int a, int b)
1.1 deraadt 188: {
189:
190: return (a > b ? a : b);
191: }
192:
1.6 millert 193: __dead void
1.4 deraadt 194: done(int sig)
1.1 deraadt 195: {
1.15 millert 196: if (tempfiles[0] != NULL)
1.13 millert 197: unlink(tempfiles[0]);
1.15 millert 198: if (tempfiles[1] != NULL)
1.13 millert 199: unlink(tempfiles[1]);
1.4 deraadt 200: if (sig)
201: _exit(status);
1.1 deraadt 202: exit(status);
1.3 tedu 203: }
1.1 deraadt 204:
1.3 tedu 205: void *
1.8 millert 206: emalloc(size_t n)
1.3 tedu 207: {
208: void *p;
209:
210: if ((p = malloc(n)) == NULL)
1.15 millert 211: error("files too big, try -h");
1.3 tedu 212: return (p);
1.1 deraadt 213: }
214:
1.3 tedu 215: void *
1.8 millert 216: erealloc(void *p, size_t n)
1.1 deraadt 217: {
1.3 tedu 218: void *q;
1.1 deraadt 219:
1.3 tedu 220: if ((q = realloc(p, n)) == NULL)
1.15 millert 221: error("files too big, try -h");
1.3 tedu 222: return (q);
1.1 deraadt 223: }
224:
1.15 millert 225: __dead void
226: error(const char *fmt, ...)
227: {
228: va_list ap;
229: int sverrno = errno;
230:
231: if (tempfiles[0] != NULL)
232: unlink(tempfiles[0]);
233: if (tempfiles[1] != NULL)
234: unlink(tempfiles[1]);
235: errno = sverrno;
236: va_start(ap, fmt);
237: verr(status, fmt, ap);
238: va_end(ap);
239: }
240:
241: __dead void
242: errorx(const char *fmt, ...)
1.1 deraadt 243: {
1.15 millert 244: va_list ap;
245:
246: if (tempfiles[0] != NULL)
247: unlink(tempfiles[0]);
248: if (tempfiles[1] != NULL)
249: unlink(tempfiles[1]);
250: va_start(ap, fmt);
251: verrx(status, fmt, ap);
252: va_end(ap);
1.6 millert 253: }
254:
255: __dead void
256: usage(void)
257: {
1.14 deraadt 258: (void)fprintf(stderr,
1.21 ! millert 259: "usage: diff [-bitw] [-c | -e | -f | -n | -u ] file1 file2\n"
1.11 millert 260: " diff [-bitw] -C number file1 file2\n"
261: " diff [-bitw] -D string file1 file2\n"
262: " diff [-bitw] -U number file1 file2\n"
1.21 ! millert 263: " diff [-biwt] [-c | -e | -f | -n | -u ] "
! 264: "[-r] [-s] [-S name]\n dir1 dir2\n");
1.6 millert 265:
1.15 millert 266: exit(2);
1.1 deraadt 267: }