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