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