xref: /original-bsd/usr.bin/diff/diff/diff.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1991, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)diff.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include "diff.h"
19 #include "pathnames.h"
20 
21 /*
22  * diff - driver and subroutines
23  */
24 
25 char	diff[] = _PATH_DIFF;
26 char	diffh[] = _PATH_DIFFH;
27 char	pr[] = _PATH_PR;
28 
29 main(argc, argv)
30 	int argc;
31 	char **argv;
32 {
33 	register char *argp;
34 
35 	ifdef1 = "FILE1"; ifdef2 = "FILE2";
36 	status = 2;
37 	diffargv = argv;
38 	argc--, argv++;
39 	while (argc > 2 && argv[0][0] == '-') {
40 		argp = &argv[0][1];
41 		argv++, argc--;
42 		while (*argp) switch(*argp++) {
43 
44 #ifdef notdef
45 		case 'I':
46 			opt = D_IFDEF;
47 			wantelses = 0;
48 			continue;
49 		case 'E':
50 			opt = D_IFDEF;
51 			wantelses = 1;
52 			continue;
53 		case '1':
54 			opt = D_IFDEF;
55 			ifdef1 = argp;
56 			*--argp = 0;
57 			continue;
58 #endif
59 		case 'D':
60 			/* -Dfoo = -E -1 -2foo */
61 			wantelses = 1;
62 			ifdef1 = "";
63 			/* fall through */
64 #ifdef notdef
65 		case '2':
66 #endif
67 			opt = D_IFDEF;
68 			ifdef2 = argp;
69 			*--argp = 0;
70 			continue;
71 		case 'e':
72 			opt = D_EDIT;
73 			continue;
74 		case 'f':
75 			opt = D_REVERSE;
76 			continue;
77 		case 'n':
78 			opt = D_NREVERSE;
79 			continue;
80 		case 'b':
81 			bflag = 1;
82 			continue;
83 		case 'w':
84 			wflag = 1;
85 			continue;
86 		case 'i':
87 			iflag = 1;
88 			continue;
89 		case 't':
90 			tflag = 1;
91 			continue;
92 		case 'c':
93 			opt = D_CONTEXT;
94 			if (isdigit(*argp)) {
95 				context = atoi(argp);
96 				while (isdigit(*argp))
97 					argp++;
98 				if (*argp) {
99 					fprintf(stderr,
100 					    "diff: -c: bad count\n");
101 					done();
102 				}
103 				argp = "";
104 			} else
105 				context = 3;
106 			continue;
107 		case 'h':
108 			hflag++;
109 			continue;
110 		case 'S':
111 			if (*argp == 0) {
112 				fprintf(stderr, "diff: use -Sstart\n");
113 				done();
114 			}
115 			Start = argp;
116 			*--argp = 0;		/* don't pass it on */
117 			continue;
118 		case 'r':
119 			rflag++;
120 			continue;
121 		case 's':
122 			sflag++;
123 			continue;
124 		case 'l':
125 			lflag++;
126 			continue;
127 		default:
128 			fprintf(stderr, "diff: -%s: unknown option\n",
129 			    --argp);
130 			done();
131 		}
132 	}
133 	if (argc != 2) {
134 		fprintf(stderr, "diff: two filename arguments required\n");
135 		done();
136 	}
137 	file1 = argv[0];
138 	file2 = argv[1];
139 	if (hflag && opt) {
140 		fprintf(stderr,
141 		    "diff: -h doesn't support -e, -f, -n, -c, or -I\n");
142 		done();
143 	}
144 	if (!strcmp(file1, "-"))
145 		stb1.st_mode = S_IFREG;
146 	else if (stat(file1, &stb1) < 0) {
147 		fprintf(stderr, "diff: ");
148 		perror(file1);
149 		done();
150 	}
151 	if (!strcmp(file2, "-"))
152 		stb2.st_mode = S_IFREG;
153 	else if (stat(file2, &stb2) < 0) {
154 		fprintf(stderr, "diff: ");
155 		perror(file2);
156 		done();
157 	}
158 	if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
159 	    (stb2.st_mode & S_IFMT) == S_IFDIR) {
160 		diffdir(argv);
161 	} else
162 		diffreg();
163 	done();
164 }
165 
166 char *
167 savestr(cp)
168 	register char *cp;
169 {
170 	register char *dp = malloc(strlen(cp)+1);
171 
172 	if (dp == 0) {
173 		fprintf(stderr, "diff: ran out of memory\n");
174 		done();
175 	}
176 	strcpy(dp, cp);
177 	return (dp);
178 }
179 
180 min(a,b)
181 	int a,b;
182 {
183 
184 	return (a < b ? a : b);
185 }
186 
187 max(a,b)
188 	int a,b;
189 {
190 
191 	return (a > b ? a : b);
192 }
193 
194 void
195 done()
196 {
197 	if (tempfile)
198 		unlink(tempfile);
199 	exit(status);
200 }
201 
202 char *
203 talloc(n)
204 {
205 	register char *p;
206 
207 	if ((p = malloc((unsigned)n)) != NULL)
208 		return(p);
209 	noroom();
210 }
211 
212 char *
213 ralloc(p,n)
214 char *p;
215 {
216 	register char *q;
217 	char *realloc();
218 
219 	if ((q = realloc(p, (unsigned)n)) == NULL)
220 		noroom();
221 	return(q);
222 }
223 
224 noroom()
225 {
226 	fprintf(stderr, "diff: files too big, try -h\n");
227 	done();
228 }
229