1 static char sccsid[] = "@(#)diff.c 4.8 03/01/91"; 2 3 #include "diff.h" 4 #include "pathnames.h" 5 6 /* 7 * diff - driver and subroutines 8 */ 9 10 char diff[] = _PATH_DIFF; 11 char diffh[] = _PATH_DIFFH; 12 char pr[] = _PATH_PR; 13 14 main(argc, argv) 15 int argc; 16 char **argv; 17 { 18 register char *argp; 19 20 ifdef1 = "FILE1"; ifdef2 = "FILE2"; 21 status = 2; 22 diffargv = argv; 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 'n': 63 opt = D_NREVERSE; 64 continue; 65 case 'b': 66 bflag = 1; 67 continue; 68 case 'w': 69 wflag = 1; 70 continue; 71 case 'i': 72 iflag = 1; 73 continue; 74 case 't': 75 tflag = 1; 76 continue; 77 case 'c': 78 opt = D_CONTEXT; 79 if (isdigit(*argp)) { 80 context = atoi(argp); 81 while (isdigit(*argp)) 82 argp++; 83 if (*argp) { 84 fprintf(stderr, 85 "diff: -c: bad count\n"); 86 done(); 87 } 88 argp = ""; 89 } else 90 context = 3; 91 continue; 92 case 'h': 93 hflag++; 94 continue; 95 case 'S': 96 if (*argp == 0) { 97 fprintf(stderr, "diff: use -Sstart\n"); 98 done(); 99 } 100 start = argp; 101 *--argp = 0; /* don't pass it on */ 102 continue; 103 case 'r': 104 rflag++; 105 continue; 106 case 's': 107 sflag++; 108 continue; 109 case 'l': 110 lflag++; 111 continue; 112 default: 113 fprintf(stderr, "diff: -%s: unknown option\n", 114 --argp); 115 done(); 116 } 117 } 118 if (argc != 2) { 119 fprintf(stderr, "diff: two filename arguments required\n"); 120 done(); 121 } 122 file1 = argv[0]; 123 file2 = argv[1]; 124 if (hflag && opt) { 125 fprintf(stderr, 126 "diff: -h doesn't support -e, -f, -n, -c, or -I\n"); 127 done(); 128 } 129 if (!strcmp(file1, "-")) 130 stb1.st_mode = S_IFREG; 131 else if (stat(file1, &stb1) < 0) { 132 fprintf(stderr, "diff: "); 133 perror(file1); 134 done(); 135 } 136 if (!strcmp(file2, "-")) 137 stb2.st_mode = S_IFREG; 138 else if (stat(file2, &stb2) < 0) { 139 fprintf(stderr, "diff: "); 140 perror(file2); 141 done(); 142 } 143 if ((stb1.st_mode & S_IFMT) == S_IFDIR && 144 (stb2.st_mode & S_IFMT) == S_IFDIR) { 145 diffdir(argv); 146 } else 147 diffreg(); 148 done(); 149 } 150 151 char * 152 savestr(cp) 153 register char *cp; 154 { 155 register char *dp = malloc(strlen(cp)+1); 156 157 if (dp == 0) { 158 fprintf(stderr, "diff: ran out of memory\n"); 159 done(); 160 } 161 strcpy(dp, cp); 162 return (dp); 163 } 164 165 min(a,b) 166 int a,b; 167 { 168 169 return (a < b ? a : b); 170 } 171 172 max(a,b) 173 int a,b; 174 { 175 176 return (a > b ? a : b); 177 } 178 179 void 180 done() 181 { 182 if (tempfile) 183 unlink(tempfile); 184 exit(status); 185 } 186 187 char * 188 talloc(n) 189 { 190 register char *p; 191 192 if ((p = malloc((unsigned)n)) != NULL) 193 return(p); 194 noroom(); 195 } 196 197 char * 198 ralloc(p,n) 199 char *p; 200 { 201 register char *q; 202 char *realloc(); 203 204 if ((q = realloc(p, (unsigned)n)) == NULL) 205 noroom(); 206 return(q); 207 } 208 209 noroom() 210 { 211 fprintf(stderr, "diff: files too big, try -h\n"); 212 done(); 213 } 214