|
|
1.1 root 1: static char sccsid[] = "@(#)diff.c 4.1 10/9/80";
2:
3: #include "diff.h"
4: /*
5: * diff - driver and subroutines
6: */
7:
8: char diff[] = DIFF;
9: char diffh[] = DIFFH;
10:
11: main(argc, argv)
12: int argc;
13: char **argv;
14: {
15: register char *argp;
16:
17: dummy = malloc(0); /* shut off malloc cache in v9 */
18: dummy = malloc(1);
19: status = 2;
20: whichtemp = 0; /* no temp files used yet! */
21: diffargv = argv;
22: argc--, argv++;
23: while (argc > 2 && argv[0][0] == '-') {
24: argp = &argv[0][1];
25: argv++, argc--;
26: while (*argp) switch(*argp++) {
27:
28: case 'e':
29: opt = D_EDIT;
30: continue;
31: case 'f':
32: opt = D_REVERSE;
33: continue;
34: case 'b':
35: bflag = 1;
36: continue;
37: case 'B':
38: bflag = 2;
39: continue;
40: case 'c':
41: opt = D_CONTEXT;
42: if (isdigit(*argp)) {
43: context = atoi(argp);
44: while (isdigit(*argp))
45: argp++;
46: if (*argp) {
47: fprintf(stderr,
48: "diff: -c: bad count\n");
49: done();
50: }
51: argp = "";
52: } else
53: context = 3;
54: continue;
55: case 'h':
56: hflag++;
57: continue;
58: case 'r':
59: rflag++;
60: continue;
61: case 's':
62: sflag++;
63: continue;
64: default:
65: fprintf(stderr, "diff: -%s: unknown option\n",
66: --argp);
67: done();
68: }
69: }
70: if (argc != 2) {
71: fprintf(stderr, "diff: two filename arguments required\n");
72: done();
73: }
74: file1 = argv[0];
75: file2 = argv[1];
76: if (hflag && opt) {
77: fprintf(stderr,
78: "diff: -h doesn't support -e, -f, -c, or -I\n");
79: done();
80: }
81: if (!strcmp(file1, "-"))
82: stb1.st_mode = S_IFREG;
83: else if (stat(file1, &stb1) < 0) {
84: fprintf(stderr, "diff: ");
85: perror(file1);
86: done();
87: }
88: if (!strcmp(file2, "-"))
89: stb2.st_mode = S_IFREG;
90: else if (stat(file2, &stb2) < 0) {
91: fprintf(stderr, "diff: ");
92: perror(file2);
93: done();
94: }
95: if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
96: (stb2.st_mode & S_IFMT) == S_IFDIR) {
97: diffdir(argv);
98: } else
99: diffreg();
100: done();
101: }
102:
103: char *
104: savestr(cp)
105: register char *cp;
106: {
107: register char *dp = malloc(strlen(cp)+1);
108:
109: if (dp == 0) {
110: fprintf(stderr, "diff: ran out of memory\n");
111: done();
112: }
113: strcpy(dp, cp);
114: return (dp);
115: }
116:
117: min(a,b)
118: int a,b;
119: {
120:
121: return (a < b ? a : b);
122: }
123:
124: max(a,b)
125: int a,b;
126: {
127:
128: return (a > b ? a : b);
129: }
130:
131: done()
132: {
133: if (whichtemp) unlink(tempfile[0]);
134: if (whichtemp==2) unlink(tempfile[1]);
135: exit(status);
136: }
137:
138: char *
139: talloc(n)
140: {
141: register char *p;
142: p = malloc((unsigned)n);
143: if(p!=NULL)
144: return(p);
145: noroom();
146: }
147:
148: char *
149: ralloc(p,n) /*compacting reallocation */
150: char *p;
151: {
152: register char *q;
153: /* uses discredited realloc doctrine
154: free(p);
155: free(dummy);
156: dummy = malloc(1);
157: */
158: q = realloc(p, (unsigned)n);
159: if(q==NULL)
160: noroom();
161: return(q);
162: }
163:
164: noroom()
165: {
166: fprintf(stderr, "diff: files too big, try -h\n");
167: done();
168: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.