1 /* $OpenBSD: diff.c,v 1.50 2007/05/29 18:24:56 ray 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.50 2007/05/29 18:24:56 ray 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 #include "xmalloc.h" 43 44 int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag; 45 int sflag, tflag, Tflag, wflag; 46 int format, context, status; 47 char *start, *ifdefname, *diffargs, *label[2], *ignore_pats; 48 struct stat stb1, stb2; 49 struct excludes *excludes_list; 50 regex_t ignore_re; 51 52 #define OPTIONS "0123456789abC:cdD:efhI:iL:lnNPpqrS:sTtU:uwX:x:" 53 static struct option longopts[] = { 54 { "text", no_argument, 0, 'a' }, 55 { "ignore-space-change", no_argument, 0, 'b' }, 56 { "context", optional_argument, 0, 'C' }, 57 { "ifdef", required_argument, 0, 'D' }, 58 { "minimal", no_argument, 0, 'd' }, 59 { "ed", no_argument, 0, 'e' }, 60 { "forward-ed", no_argument, 0, 'f' }, 61 { "ignore-matching-lines", required_argument, 0, 'I' }, 62 { "ignore-case", no_argument, 0, 'i' }, 63 { "paginate", no_argument, 0, 'l' }, 64 { "label", required_argument, 0, 'L' }, 65 { "new-file", no_argument, 0, 'N' }, 66 { "rcs", no_argument, 0, 'n' }, 67 { "unidirectional-new-file", no_argument, 0, 'P' }, 68 { "show-c-function", no_argument, 0, 'p' }, 69 { "brief", no_argument, 0, 'q' }, 70 { "recursive", no_argument, 0, 'r' }, 71 { "report-identical-files", no_argument, 0, 's' }, 72 { "starting-file", required_argument, 0, 'S' }, 73 { "expand-tabs", no_argument, 0, 't' }, 74 { "initial-tab", no_argument, 0, 'T' }, 75 { "unified", optional_argument, 0, 'U' }, 76 { "ignore-all-space", no_argument, 0, 'w' }, 77 { "exclude", required_argument, 0, 'x' }, 78 { "exclude-from", required_argument, 0, 'X' }, 79 { NULL, 0, 0, '\0'} 80 }; 81 82 __dead void usage(void); 83 void push_excludes(char *); 84 void push_ignore_pats(char *); 85 void read_excludes_file(char *file); 86 void set_argstr(char **, char **); 87 88 int 89 main(int argc, char **argv) 90 { 91 char *ep, **oargv; 92 long l; 93 int ch, lastch, gotstdin, prevoptind, newarg; 94 95 oargv = argv; 96 gotstdin = 0; 97 98 lastch = '\0'; 99 prevoptind = 1; 100 newarg = 1; 101 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 102 switch (ch) { 103 case '0': case '1': case '2': case '3': case '4': 104 case '5': case '6': case '7': case '8': case '9': 105 if (newarg) 106 usage(); /* disallow -[0-9]+ */ 107 else if (lastch == 'c' || lastch == 'u') 108 context = 0; 109 else if (!isdigit(lastch) || context > INT_MAX / 10) 110 usage(); 111 context = (context * 10) + (ch - '0'); 112 break; 113 case 'a': 114 aflag = 1; 115 break; 116 case 'b': 117 bflag = 1; 118 break; 119 case 'C': 120 case 'c': 121 format = D_CONTEXT; 122 if (optarg != NULL) { 123 l = strtol(optarg, &ep, 10); 124 if (*ep != '\0' || l < 0 || l >= INT_MAX) 125 usage(); 126 context = (int)l; 127 } else 128 context = 3; 129 break; 130 case 'd': 131 dflag = 1; 132 break; 133 case 'D': 134 format = D_IFDEF; 135 ifdefname = optarg; 136 break; 137 case 'e': 138 format = D_EDIT; 139 break; 140 case 'f': 141 format = D_REVERSE; 142 break; 143 case 'h': 144 /* silently ignore for backwards compatibility */ 145 break; 146 case 'I': 147 push_ignore_pats(optarg); 148 break; 149 case 'i': 150 iflag = 1; 151 break; 152 case 'L': 153 if (label[0] == NULL) 154 label[0] = optarg; 155 else if (label[1] == NULL) 156 label[1] = optarg; 157 else 158 usage(); 159 break; 160 case 'l': 161 lflag = 1; 162 signal(SIGPIPE, SIG_IGN); 163 break; 164 case 'N': 165 Nflag = 1; 166 break; 167 case 'n': 168 format = D_NREVERSE; 169 break; 170 case 'p': 171 pflag = 1; 172 break; 173 case 'P': 174 Pflag = 1; 175 break; 176 case 'r': 177 rflag = 1; 178 break; 179 case 'q': 180 format = D_BRIEF; 181 break; 182 case 'S': 183 start = optarg; 184 break; 185 case 's': 186 sflag = 1; 187 break; 188 case 'T': 189 Tflag = 1; 190 break; 191 case 't': 192 tflag = 1; 193 break; 194 case 'U': 195 case 'u': 196 format = D_UNIFIED; 197 if (optarg != NULL) { 198 l = strtol(optarg, &ep, 10); 199 if (*ep != '\0' || l < 0 || l >= INT_MAX) 200 usage(); 201 context = (int)l; 202 } else 203 context = 3; 204 break; 205 case 'w': 206 wflag = 1; 207 break; 208 case 'X': 209 read_excludes_file(optarg); 210 break; 211 case 'x': 212 push_excludes(optarg); 213 break; 214 default: 215 usage(); 216 break; 217 } 218 lastch = ch; 219 newarg = optind != prevoptind; 220 prevoptind = optind; 221 } 222 argc -= optind; 223 argv += optind; 224 225 /* 226 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 227 * driver routine. Both drivers use the contents of stb1 and stb2. 228 */ 229 if (argc != 2) 230 usage(); 231 if (ignore_pats != NULL) { 232 char buf[BUFSIZ]; 233 int error; 234 235 if ((error = regcomp(&ignore_re, ignore_pats, 236 REG_NEWLINE | REG_EXTENDED)) != 0) { 237 regerror(error, &ignore_re, buf, sizeof(buf)); 238 if (*ignore_pats != '\0') 239 errx(2, "%s: %s", ignore_pats, buf); 240 else 241 errx(2, "%s", buf); 242 } 243 } 244 if (strcmp(argv[0], "-") == 0) { 245 fstat(STDIN_FILENO, &stb1); 246 gotstdin = 1; 247 } else if (stat(argv[0], &stb1) != 0) 248 err(2, "%s", argv[0]); 249 if (strcmp(argv[1], "-") == 0) { 250 fstat(STDIN_FILENO, &stb2); 251 gotstdin = 1; 252 } else if (stat(argv[1], &stb2) != 0) 253 err(2, "%s", argv[1]); 254 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 255 errx(2, "can't compare - to a directory"); 256 set_argstr(oargv, argv); 257 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 258 if (format == D_IFDEF) 259 errx(2, "-D option not supported with directories"); 260 diffdir(argv[0], argv[1]); 261 } else { 262 if (S_ISDIR(stb1.st_mode)) { 263 argv[0] = splice(argv[0], argv[1]); 264 if (stat(argv[0], &stb1) < 0) 265 err(2, "%s", argv[0]); 266 } 267 if (S_ISDIR(stb2.st_mode)) { 268 argv[1] = splice(argv[1], argv[0]); 269 if (stat(argv[1], &stb2) < 0) 270 err(2, "%s", argv[1]); 271 } 272 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], 273 NULL); 274 } 275 exit(status); 276 } 277 278 void 279 set_argstr(char **av, char **ave) 280 { 281 size_t argsize; 282 char **ap; 283 284 argsize = 4 + *ave - *av + 1; 285 diffargs = xmalloc(argsize); 286 strlcpy(diffargs, "diff", argsize); 287 for (ap = av + 1; ap < ave; ap++) { 288 if (strcmp(*ap, "--") != 0) { 289 strlcat(diffargs, " ", argsize); 290 strlcat(diffargs, *ap, argsize); 291 } 292 } 293 } 294 295 /* 296 * Read in an excludes file and push each line. 297 */ 298 void 299 read_excludes_file(char *file) 300 { 301 FILE *fp; 302 char *buf, *pattern; 303 size_t len; 304 305 if (strcmp(file, "-") == 0) 306 fp = stdin; 307 else if ((fp = fopen(file, "r")) == NULL) 308 err(2, "%s", file); 309 while ((buf = fgetln(fp, &len)) != NULL) { 310 if (buf[len - 1] == '\n') 311 len--; 312 pattern = xmalloc(len + 1); 313 memcpy(pattern, buf, len); 314 pattern[len] = '\0'; 315 push_excludes(pattern); 316 } 317 if (strcmp(file, "-") != 0) 318 fclose(fp); 319 } 320 321 /* 322 * Push a pattern onto the excludes list. 323 */ 324 void 325 push_excludes(char *pattern) 326 { 327 struct excludes *entry; 328 329 entry = xmalloc(sizeof(*entry)); 330 entry->pattern = pattern; 331 entry->next = excludes_list; 332 excludes_list = entry; 333 } 334 335 void 336 push_ignore_pats(char *pattern) 337 { 338 size_t len; 339 340 if (ignore_pats == NULL) 341 ignore_pats = xstrdup(pattern); 342 else { 343 /* old + "|" + new + NUL */ 344 len = strlen(ignore_pats) + strlen(pattern) + 2; 345 ignore_pats = xrealloc(ignore_pats, 1, len); 346 strlcat(ignore_pats, "|", len); 347 strlcat(ignore_pats, pattern, len); 348 } 349 } 350 351 void 352 print_only(const char *path, size_t dirlen, const char *entry) 353 { 354 if (dirlen > 1) 355 dirlen--; 356 printf("Only in %.*s: %s\n", (int)dirlen, path, entry); 357 } 358 359 void 360 print_status(int val, char *path1, char *path2, char *entry) 361 { 362 switch (val) { 363 case D_ONLY: 364 print_only(path1, strlen(path1), entry); 365 break; 366 case D_COMMON: 367 printf("Common subdirectories: %s%s and %s%s\n", 368 path1, entry ? entry : "", path2, entry ? entry : ""); 369 break; 370 case D_BINARY: 371 printf("Binary files %s%s and %s%s differ\n", 372 path1, entry ? entry : "", path2, entry ? entry : ""); 373 break; 374 case D_DIFFER: 375 if (format == D_BRIEF) 376 printf("Files %s%s and %s%s differ\n", 377 path1, entry ? entry : "", 378 path2, entry ? entry : ""); 379 break; 380 case D_SAME: 381 if (sflag) 382 printf("Files %s%s and %s%s are identical\n", 383 path1, entry ? entry : "", 384 path2, entry ? entry : ""); 385 break; 386 case D_MISMATCH1: 387 printf("File %s%s is a directory while file %s%s is a regular file\n", 388 path1, entry ? entry : "", path2, entry ? entry : ""); 389 break; 390 case D_MISMATCH2: 391 printf("File %s%s is a regular file while file %s%s is a directory\n", 392 path1, entry ? entry : "", path2, entry ? entry : ""); 393 break; 394 case D_SKIPPED1: 395 printf("File %s%s is not a regular file or directory and was skipped\n", 396 path1, entry ? entry : ""); 397 break; 398 case D_SKIPPED2: 399 printf("File %s%s is not a regular file or directory and was skipped\n", 400 path2, entry ? entry : ""); 401 break; 402 } 403 } 404 405 __dead void 406 usage(void) 407 { 408 (void)fprintf(stderr, 409 "usage: diff [-abdilpqTtw] [-I pattern] [-c | -e | -f | -n | -u]\n" 410 " [-L label] file1 file2\n" 411 " diff [-abdilpqTtw] [-I pattern] [-L label] -C number file1 file2\n" 412 " diff [-abdilqtw] [-I pattern] -D string file1 file2\n" 413 " diff [-abdilpqTtw] [-I pattern] [-L label] -U number file1 file2\n" 414 " diff [-abdilNPpqrsTtw] [-I pattern] [-c | -e | -f | -n | -u]\n" 415 " [-L label] [-S name] [-X file] [-x pattern] dir1 dir2\n"); 416 417 exit(2); 418 } 419