1 /* $OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * Sponsored in part by the Defense Advanced Research Projects 19 * Agency (DARPA) and Air Force Research Laboratory, Air Force 20 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 21 */ 22 23 #ifndef lint 24 static const char rcsid[] = "$OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $"; 25 #endif /* not lint */ 26 27 #include <sys/param.h> 28 #include <sys/stat.h> 29 30 #include <ctype.h> 31 #include <err.h> 32 #include <errno.h> 33 #include <getopt.h> 34 #include <signal.h> 35 #include <stdlib.h> 36 #include <stdio.h> 37 #include <stdarg.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #include "diff.h" 42 43 int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag; 44 int sflag, tflag, Tflag, wflag; 45 int format, context, status; 46 char *start, *ifdefname, *diffargs, *label; 47 struct stat stb1, stb2; 48 struct excludes *excludes_list; 49 50 #define OPTIONS "0123456789abC:cdD:efhiL:lnNPpqrS:sTtU:uwX:x:" 51 static struct option longopts[] = { 52 { "text", no_argument, 0, 'a' }, 53 { "ignore-space-change", no_argument, 0, 'b' }, 54 { "context", optional_argument, 0, 'C' }, 55 { "ifdef", required_argument, 0, 'D' }, 56 { "minimal", no_argument, 0, 'd' }, 57 { "ed", no_argument, 0, 'e' }, 58 { "forward-ed", no_argument, 0, 'f' }, 59 { "ignore-case", no_argument, 0, 'i' }, 60 { "paginate", no_argument, 0, 'l' }, 61 { "label", required_argument, 0, 'L' }, 62 { "new-file", no_argument, 0, 'N' }, 63 { "rcs", no_argument, 0, 'n' }, 64 { "unidirectional-new-file", no_argument, 0, 'P' }, 65 { "show-c-function", no_argument, 0, 'p' }, 66 { "brief", no_argument, 0, 'q' }, 67 { "recursive", no_argument, 0, 'r' }, 68 { "report-identical-files", no_argument, 0, 's' }, 69 { "starting-file", required_argument, 0, 'S' }, 70 { "expand-tabs", no_argument, 0, 't' }, 71 { "initial-tab", no_argument, 0, 'T' }, 72 { "unified", optional_argument, 0, 'U' }, 73 { "ignore-all-space", no_argument, 0, 'w' }, 74 { "exclude", required_argument, 0, 'x' }, 75 { "exclude-from", required_argument, 0, 'X' }, 76 { NULL, 0, 0, '\0'} 77 }; 78 79 __dead void usage(void); 80 void push_excludes(char *); 81 void read_excludes_file(char *file); 82 void set_argstr(char **, char **); 83 84 int 85 main(int argc, char **argv) 86 { 87 char *ep, **oargv; 88 long l; 89 int ch, lastch, gotstdin, prevoptind, newarg; 90 91 oargv = argv; 92 gotstdin = 0; 93 94 lastch = '\0'; 95 prevoptind = 1; 96 newarg = 1; 97 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 98 switch (ch) { 99 case '0': case '1': case '2': case '3': case '4': 100 case '5': case '6': case '7': case '8': case '9': 101 if (newarg) 102 usage(); /* disallow -[0-9]+ */ 103 else if (lastch == 'c' || lastch == 'u') 104 context = 0; 105 else if (!isdigit(lastch) || context > INT_MAX / 10) 106 usage(); 107 context = (context * 10) + (ch - '0'); 108 break; 109 case 'a': 110 aflag = 1; 111 break; 112 case 'b': 113 bflag = 1; 114 break; 115 case 'C': 116 case 'c': 117 format = D_CONTEXT; 118 if (optarg != NULL) { 119 l = strtol(optarg, &ep, 10); 120 if (*ep != '\0' || l < 0 || l >= INT_MAX) 121 usage(); 122 context = (int)l; 123 } else 124 context = 3; 125 break; 126 case 'd': 127 dflag = 1; 128 break; 129 case 'D': 130 format = D_IFDEF; 131 ifdefname = optarg; 132 break; 133 case 'e': 134 format = D_EDIT; 135 break; 136 case 'f': 137 format = D_REVERSE; 138 break; 139 case 'h': 140 /* silently ignore for backwards compatibility */ 141 break; 142 case 'i': 143 iflag = 1; 144 break; 145 case 'L': 146 label = optarg; 147 break; 148 case 'l': 149 lflag = 1; 150 signal(SIGPIPE, SIG_IGN); 151 break; 152 case 'N': 153 Nflag = 1; 154 break; 155 case 'n': 156 format = D_NREVERSE; 157 break; 158 case 'p': 159 pflag = 1; 160 break; 161 case 'P': 162 Pflag = 1; 163 break; 164 case 'r': 165 rflag = 1; 166 break; 167 case 'q': 168 format = D_BRIEF; 169 break; 170 case 'S': 171 start = optarg; 172 break; 173 case 's': 174 sflag = 1; 175 break; 176 case 'T': 177 Tflag = 1; 178 break; 179 case 't': 180 tflag = 1; 181 break; 182 case 'U': 183 case 'u': 184 format = D_UNIFIED; 185 if (optarg != NULL) { 186 l = strtol(optarg, &ep, 10); 187 if (*ep != '\0' || l < 0 || l >= INT_MAX) 188 usage(); 189 context = (int)l; 190 } else 191 context = 3; 192 break; 193 case 'w': 194 wflag = 1; 195 break; 196 case 'X': 197 read_excludes_file(optarg); 198 break; 199 case 'x': 200 push_excludes(optarg); 201 break; 202 default: 203 usage(); 204 break; 205 } 206 lastch = ch; 207 newarg = optind != prevoptind; 208 prevoptind = optind; 209 } 210 argc -= optind; 211 argv += optind; 212 213 /* 214 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 215 * driver routine. Both drivers use the contents of stb1 and stb2. 216 */ 217 if (argc != 2) 218 usage(); 219 if (strcmp(argv[0], "-") == 0) { 220 fstat(STDIN_FILENO, &stb1); 221 gotstdin = 1; 222 } else if (stat(argv[0], &stb1) != 0) 223 err(2, "%s", argv[0]); 224 if (strcmp(argv[1], "-") == 0) { 225 fstat(STDIN_FILENO, &stb2); 226 gotstdin = 1; 227 } else if (stat(argv[1], &stb2) != 0) 228 err(2, "%s", argv[1]); 229 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 230 errx(2, "can't compare - to a directory"); 231 set_argstr(oargv + 1, argv); 232 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 233 if (format == D_IFDEF) 234 errx(2, "-D option not supported with directories"); 235 diffdir(argv[0], argv[1]); 236 } else { 237 if (S_ISDIR(stb1.st_mode)) { 238 argv[0] = splice(argv[0], argv[1]); 239 if (stat(argv[0], &stb1) < 0) 240 err(2, "%s", argv[0]); 241 } 242 if (S_ISDIR(stb2.st_mode)) { 243 argv[1] = splice(argv[1], argv[0]); 244 if (stat(argv[1], &stb2) < 0) 245 err(2, "%s", argv[1]); 246 } 247 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], 248 NULL); 249 } 250 exit(status); 251 } 252 253 void * 254 emalloc(size_t n) 255 { 256 void *p; 257 258 if ((p = malloc(n)) == NULL) 259 err(2, NULL); 260 return (p); 261 } 262 263 void * 264 erealloc(void *p, size_t n) 265 { 266 void *q; 267 268 if ((q = realloc(p, n)) == NULL) 269 err(2, NULL); 270 return (q); 271 } 272 273 int 274 easprintf(char **ret, const char *fmt, ...) 275 { 276 int len; 277 va_list ap; 278 279 va_start(ap, fmt); 280 len = vasprintf(ret, fmt, ap); 281 va_end(ap); 282 283 if (len == -1) 284 err(2, NULL); 285 return (len); 286 } 287 288 void 289 set_argstr(char **av, char **ave) 290 { 291 size_t argsize; 292 char **ap; 293 294 argsize = 4 + *ave - *av + 1; 295 diffargs = emalloc(argsize); 296 strlcpy(diffargs, "diff", argsize); 297 for (ap = av + 1; ap < ave; ap++) { 298 if (strcmp(*ap, "--") != 0) { 299 strlcat(diffargs, " ", argsize); 300 strlcat(diffargs, *ap, argsize); 301 } 302 } 303 } 304 305 /* 306 * Read in an excludes file and push each line. 307 */ 308 void 309 read_excludes_file(char *file) 310 { 311 FILE *fp; 312 char *buf, *pattern; 313 size_t len; 314 315 if (strcmp(file, "-") == 0) 316 fp = stdin; 317 else if ((fp = fopen(file, "r")) == NULL) 318 err(2, "%s", file); 319 while ((buf = fgetln(fp, &len)) != NULL) { 320 if (buf[len - 1] == '\n') 321 len--; 322 pattern = emalloc(len + 1); 323 memcpy(pattern, buf, len); 324 pattern[len] = '\0'; 325 push_excludes(pattern); 326 } 327 if (strcmp(file, "-") != 0) 328 fclose(fp); 329 } 330 331 /* 332 * Push a pattern onto the excludes list. 333 */ 334 void 335 push_excludes(char *pattern) 336 { 337 struct excludes *entry; 338 339 entry = emalloc(sizeof(*entry)); 340 entry->pattern = pattern; 341 entry->next = excludes_list; 342 excludes_list = entry; 343 } 344 345 void 346 print_only(const char *path, size_t dirlen, const char *entry) 347 { 348 if (dirlen > 1) 349 dirlen--; 350 printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 351 } 352 353 void 354 print_status(int val, char *path1, char *path2, char *entry) 355 { 356 switch (val) { 357 case D_ONLY: 358 print_only(path1, strlen(path1), entry); 359 break; 360 case D_COMMON: 361 printf("Common subdirectories: %s%s and %s%s\n", 362 path1, entry ? entry : "", path2, entry ? entry : ""); 363 break; 364 case D_BINARY: 365 printf("Binary files %s%s and %s%s differ\n", 366 path1, entry ? entry : "", path2, entry ? entry : ""); 367 break; 368 case D_DIFFER: 369 if (format == D_BRIEF) 370 printf("Files %s%s and %s%s differ\n", 371 path1, entry ? entry : "", 372 path2, entry ? entry : ""); 373 break; 374 case D_SAME: 375 if (sflag) 376 printf("Files %s%s and %s%s are identical\n", 377 path1, entry ? entry : "", 378 path2, entry ? entry : ""); 379 break; 380 case D_MISMATCH1: 381 printf("File %s%s is a directory while file %s%s is a regular file\n", 382 path1, entry ? entry : "", path2, entry ? entry : ""); 383 break; 384 case D_MISMATCH2: 385 printf("File %s%s is a regular file while file %s%s is a directory\n", 386 path1, entry ? entry : "", path2, entry ? entry : ""); 387 break; 388 } 389 } 390 391 __dead void 392 usage(void) 393 { 394 (void)fprintf(stderr, 395 "usage: diff [-abdilpqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" 396 " diff [-abdilpqtTw] [-L label] -C number file1 file2\n" 397 " diff [-abdilqtw] -D string file1 file2\n" 398 " diff [-abdilpqtTw] [-L label] -U number file1 file2\n" 399 " diff [-abdilNPpqtTw] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s]\n" 400 " [-S name] [-X file] [-x pattern] dir1 dir2\n"); 401 402 exit(2); 403 } 404