1 /* $OpenBSD: diff.c,v 1.41 2003/09/07 18:50:58 jmc 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.41 2003/09/07 18:50:58 jmc Exp $"; 25 #endif /* not lint */ 26 27 #include <sys/param.h> 28 #include <sys/stat.h> 29 30 #include <err.h> 31 #include <errno.h> 32 #include <getopt.h> 33 #include <signal.h> 34 #include <stdlib.h> 35 #include <stdio.h> 36 #include <stdarg.h> 37 #include <string.h> 38 #include <unistd.h> 39 40 #include "diff.h" 41 42 int aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, rflag; 43 int sflag, tflag, Tflag, wflag; 44 int format, context, status; 45 char *start, *ifdefname, *diffargs, *label; 46 struct stat stb1, stb2; 47 struct excludes *excludes_list; 48 49 #define OPTIONS "0123456789abC:cdD:efhiL:lnNPqrS:sTtU:uwX:x:" 50 static struct option longopts[] = { 51 { "text", no_argument, 0, 'a' }, 52 { "ignore-space-change", no_argument, 0, 'b' }, 53 { "context", optional_argument, 0, 'C' }, 54 { "ifdef", required_argument, 0, 'D' }, 55 { "minimal", no_argument, 0, 'd' }, 56 { "ed", no_argument, 0, 'e' }, 57 { "forward-ed", no_argument, 0, 'f' }, 58 { "ignore-case", no_argument, 0, 'i' }, 59 { "paginate", no_argument, 0, 'l' }, 60 { "label", required_argument, 0, 'L' }, 61 { "new-file", no_argument, 0, 'N' }, 62 { "rcs", no_argument, 0, 'n' }, 63 { "unidirectional-new-file", no_argument, 0, 'P' }, 64 { "brief", no_argument, 0, 'q' }, 65 { "recursive", no_argument, 0, 'r' }, 66 { "report-identical-files", no_argument, 0, 's' }, 67 { "starting-file", required_argument, 0, 'S' }, 68 { "expand-tabs", no_argument, 0, 't' }, 69 { "initial-tab", no_argument, 0, 'T' }, 70 { "unified", optional_argument, 0, 'U' }, 71 { "ignore-all-space", no_argument, 0, 'w' }, 72 { "exclude", required_argument, 0, 'x' }, 73 { "exclude-from", required_argument, 0, 'X' }, 74 { NULL, 0, 0, '\0'} 75 }; 76 77 __dead void usage(void); 78 void push_excludes(char *); 79 void read_excludes_file(char *file); 80 void set_argstr(char **, char **); 81 82 int 83 main(int argc, char **argv) 84 { 85 char *ep, **oargv; 86 long l; 87 int ch, lastch, gotstdin; 88 89 oargv = argv; 90 gotstdin = 0; 91 92 lastch = 0; 93 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 94 switch (ch) { 95 case '0': case '1': case '2': case '3': case '4': 96 case '5': case '6': case '7': case '8': case '9': 97 if (!(lastch == 'c' || lastch == 'u' || 98 (lastch >= '0' && lastch <= '9'))) 99 usage(); 100 if (lastch == 'c' || lastch == 'u') 101 context = 0; 102 context = context * 10 + ch - '0'; 103 break; 104 case 'a': 105 aflag = 1; 106 break; 107 case 'b': 108 bflag = 1; 109 break; 110 case 'C': 111 case 'c': 112 format = D_CONTEXT; 113 if (optarg != NULL) { 114 l = strtol(optarg, &ep, 10); 115 if (*ep != '\0' || l < 0 || l >= INT_MAX) 116 usage(); 117 context = (int)l; 118 } else 119 context = 3; 120 break; 121 case 'd': 122 dflag = 1; 123 break; 124 case 'D': 125 format = D_IFDEF; 126 ifdefname = optarg; 127 break; 128 case 'e': 129 format = D_EDIT; 130 break; 131 case 'f': 132 format = D_REVERSE; 133 break; 134 case 'h': 135 /* silently ignore for backwards compatibility */ 136 break; 137 case 'i': 138 iflag = 1; 139 break; 140 case 'L': 141 label = optarg; 142 break; 143 case 'l': 144 lflag = 1; 145 signal(SIGPIPE, SIG_IGN); 146 break; 147 case 'N': 148 Nflag = 1; 149 break; 150 case 'n': 151 format = D_NREVERSE; 152 break; 153 case 'P': 154 Pflag = 1; 155 break; 156 case 'r': 157 rflag = 1; 158 break; 159 case 'q': 160 format = D_BRIEF; 161 break; 162 case 'S': 163 start = optarg; 164 break; 165 case 's': 166 sflag = 1; 167 break; 168 case 'T': 169 Tflag = 1; 170 break; 171 case 't': 172 tflag = 1; 173 break; 174 case 'U': 175 case 'u': 176 format = D_UNIFIED; 177 if (optarg != NULL) { 178 l = strtol(optarg, &ep, 10); 179 if (*ep != '\0' || l < 0 || l >= INT_MAX) 180 usage(); 181 context = (int)l; 182 } else 183 context = 3; 184 break; 185 case 'w': 186 wflag = 1; 187 break; 188 case 'X': 189 read_excludes_file(optarg); 190 break; 191 case 'x': 192 push_excludes(optarg); 193 break; 194 default: 195 usage(); 196 break; 197 } 198 lastch = ch; 199 } 200 argc -= optind; 201 argv += optind; 202 203 /* 204 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 205 * driver routine. Both drivers use the contents of stb1 and stb2. 206 */ 207 if (argc != 2) 208 usage(); 209 if (strcmp(argv[0], "-") == 0) { 210 fstat(STDIN_FILENO, &stb1); 211 gotstdin = 1; 212 } else if (stat(argv[0], &stb1) != 0) 213 err(2, "%s", argv[0]); 214 if (strcmp(argv[1], "-") == 0) { 215 fstat(STDIN_FILENO, &stb2); 216 gotstdin = 1; 217 } else if (stat(argv[1], &stb2) != 0) 218 err(2, "%s", argv[1]); 219 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 220 errx(2, "can't compare - to a directory"); 221 set_argstr(oargv + 1, argv); 222 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 223 if (format == D_IFDEF) 224 errx(2, "-D option not supported with directories"); 225 diffdir(argv[0], argv[1]); 226 } else { 227 if (S_ISDIR(stb1.st_mode)) { 228 argv[0] = splice(argv[0], argv[1]); 229 if (stat(argv[0], &stb1) < 0) 230 err(2, "%s", argv[0]); 231 } 232 if (S_ISDIR(stb2.st_mode)) { 233 argv[1] = splice(argv[1], argv[0]); 234 if (stat(argv[1], &stb2) < 0) 235 err(2, "%s", argv[1]); 236 } 237 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], 238 NULL); 239 } 240 exit(status); 241 } 242 243 void * 244 emalloc(size_t n) 245 { 246 void *p; 247 248 if ((p = malloc(n)) == NULL) 249 err(2, NULL); 250 return (p); 251 } 252 253 void * 254 erealloc(void *p, size_t n) 255 { 256 void *q; 257 258 if ((q = realloc(p, n)) == NULL) 259 err(2, NULL); 260 return (q); 261 } 262 263 int 264 easprintf(char **ret, const char *fmt, ...) 265 { 266 int len; 267 va_list ap; 268 269 va_start(ap, fmt); 270 len = vasprintf(ret, fmt, ap); 271 va_end(ap); 272 273 if (len == -1) 274 err(2, NULL); 275 return (len); 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 = emalloc(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 = emalloc(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 = emalloc(sizeof(*entry)); 330 entry->pattern = pattern; 331 entry->next = excludes_list; 332 excludes_list = entry; 333 } 334 335 void 336 print_status(int val, char *path1, char *path2, char *entry) 337 { 338 switch (val) { 339 case D_ONLY: 340 /* must strip off the trailing '/' */ 341 printf("Only in %.*s: %s\n", (int)(strlen(path1) - 1), 342 path1, entry); 343 break; 344 case D_COMMON: 345 printf("Common subdirectories: %s%s and %s%s\n", 346 path1, entry ? entry : "", path2, entry ? entry : ""); 347 break; 348 case D_BINARY: 349 printf("Binary files %s%s and %s%s differ\n", 350 path1, entry ? entry : "", path2, entry ? entry : ""); 351 break; 352 case D_DIFFER: 353 if (format == D_BRIEF) 354 printf("Files %s%s and %s%s differ\n", 355 path1, entry ? entry : "", 356 path2, entry ? entry : ""); 357 break; 358 case D_SAME: 359 if (sflag) 360 printf("Files %s%s and %s%s are identical\n", 361 path1, entry ? entry : "", 362 path2, entry ? entry : ""); 363 break; 364 case D_MISMATCH1: 365 printf("File %s%s is a directory while file %s%s is a regular file\n", 366 path1, entry ? entry : "", path2, entry ? entry : ""); 367 break; 368 case D_MISMATCH2: 369 printf("File %s%s is a regular file while file %s%s is a directory\n", 370 path1, entry ? entry : "", path2, entry ? entry : ""); 371 break; 372 } 373 } 374 375 __dead void 376 usage(void) 377 { 378 (void)fprintf(stderr, 379 "usage: diff [-abdilqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" 380 " diff [-abdilqtTw] [-L label] -C number file1 file2\n" 381 " diff [-abdilqtw] -D string file1 file2\n" 382 " diff [-abdilqtTw] [-L label] -U number file1 file2\n" 383 " diff [-abdilNPqtTw] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s]\n" 384 " [-S name] [-X file] [-x pattern] dir1 dir2\n"); 385 386 exit(2); 387 } 388