1 /* $OpenBSD: diff.c,v 1.37 2003/07/29 18:38:47 deraadt 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.37 2003/07/29 18:38:47 deraadt 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 "abC: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 { "intial-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, gotstdin; 88 89 oargv = argv; 90 gotstdin = 0; 91 92 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) { 93 switch (ch) { 94 case 'a': 95 aflag = 1; 96 break; 97 case 'b': 98 bflag = 1; 99 break; 100 case 'C': 101 case 'c': 102 format = D_CONTEXT; 103 if (optarg != NULL) { 104 l = strtol(optarg, &ep, 10); 105 if (*ep != '\0' || l < 0 || l >= INT_MAX) 106 usage(); 107 context = (int)l; 108 } else 109 context = 3; 110 break; 111 case 'd': 112 dflag = 1; 113 break; 114 case 'D': 115 format = D_IFDEF; 116 ifdefname = optarg; 117 break; 118 case 'e': 119 format = D_EDIT; 120 break; 121 case 'f': 122 format = D_REVERSE; 123 break; 124 case 'h': 125 /* silently ignore for backwards compatibility */ 126 break; 127 case 'i': 128 iflag = 1; 129 break; 130 case 'L': 131 label = optarg; 132 break; 133 case 'l': 134 lflag = 1; 135 signal(SIGPIPE, SIG_IGN); 136 break; 137 case 'N': 138 Nflag = 1; 139 break; 140 case 'n': 141 format = D_NREVERSE; 142 break; 143 case 'P': 144 Pflag = 1; 145 break; 146 case 'r': 147 rflag = 1; 148 break; 149 case 'q': 150 format = D_BRIEF; 151 break; 152 case 'S': 153 start = optarg; 154 break; 155 case 's': 156 sflag = 1; 157 break; 158 case 'T': 159 Tflag = 1; 160 break; 161 case 't': 162 tflag = 1; 163 break; 164 case 'U': 165 case 'u': 166 format = D_UNIFIED; 167 if (optarg != NULL) { 168 l = strtol(optarg, &ep, 10); 169 if (*ep != '\0' || l < 0 || l >= INT_MAX) 170 usage(); 171 context = (int)l; 172 } else 173 context = 3; 174 break; 175 case 'w': 176 wflag = 1; 177 break; 178 case 'X': 179 read_excludes_file(optarg); 180 break; 181 case 'x': 182 push_excludes(optarg); 183 break; 184 default: 185 usage(); 186 break; 187 } 188 } 189 argc -= optind; 190 argv += optind; 191 192 /* 193 * Do sanity checks, fill in stb1 and stb2 and call the appropriate 194 * driver routine. Both drivers use the contents of stb1 and stb2. 195 */ 196 if (argc != 2) 197 usage(); 198 if (strcmp(argv[0], "-") == 0) { 199 fstat(STDIN_FILENO, &stb1); 200 gotstdin = 1; 201 } else if (stat(argv[0], &stb1) != 0) 202 err(2, "%s", argv[0]); 203 if (strcmp(argv[1], "-") == 0) { 204 fstat(STDIN_FILENO, &stb2); 205 gotstdin = 1; 206 } else if (stat(argv[1], &stb2) != 0) 207 err(2, "%s", argv[1]); 208 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode))) 209 errx(2, "can't compare - to a directory"); 210 set_argstr(oargv + 1, argv); 211 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) { 212 if (format == D_IFDEF) 213 errx(2, "-D option not supported with directories"); 214 diffdir(argv[0], argv[1]); 215 } else { 216 if (S_ISDIR(stb1.st_mode)) { 217 argv[0] = splice(argv[0], argv[1]); 218 if (stat(argv[0], &stb1) < 0) 219 err(2, "%s", argv[0]); 220 } 221 if (S_ISDIR(stb2.st_mode)) { 222 argv[1] = splice(argv[1], argv[0]); 223 if (stat(argv[1], &stb2) < 0) 224 err(2, "%s", argv[1]); 225 } 226 print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1], 227 NULL); 228 } 229 exit(status); 230 } 231 232 void * 233 emalloc(size_t n) 234 { 235 void *p; 236 237 if ((p = malloc(n)) == NULL) 238 err(2, NULL); 239 return (p); 240 } 241 242 void * 243 erealloc(void *p, size_t n) 244 { 245 void *q; 246 247 if ((q = realloc(p, n)) == NULL) 248 err(2, NULL); 249 return (q); 250 } 251 252 int 253 easprintf(char **ret, const char *fmt, ...) 254 { 255 int len; 256 va_list ap; 257 258 va_start(ap, fmt); 259 len = vasprintf(ret, fmt, ap); 260 va_end(ap); 261 262 if (len == -1) 263 err(2, NULL); 264 return (len); 265 } 266 267 void 268 set_argstr(char **av, char **ave) 269 { 270 size_t argsize; 271 char **ap; 272 273 argsize = 4 + *ave - *av + 1; 274 diffargs = emalloc(argsize); 275 strlcpy(diffargs, "diff", argsize); 276 for (ap = av + 1; ap < ave; ap++) { 277 if (strcmp(*ap, "--") != 0) { 278 strlcat(diffargs, " ", argsize); 279 strlcat(diffargs, *ap, argsize); 280 } 281 } 282 } 283 284 /* 285 * Read in an excludes file and push each line. 286 */ 287 void 288 read_excludes_file(char *file) 289 { 290 FILE *fp; 291 char *buf, *pattern; 292 size_t len; 293 294 if (strcmp(file, "-") == 0) 295 fp = stdin; 296 else if ((fp = fopen(file, "r")) == NULL) 297 err(2, "%s", file); 298 while ((buf = fgetln(fp, &len)) != NULL) { 299 if (buf[len - 1] == '\n') 300 len--; 301 pattern = emalloc(len + 1); 302 memcpy(pattern, buf, len); 303 pattern[len] = '\0'; 304 push_excludes(pattern); 305 } 306 if (strcmp(file, "-") != 0) 307 fclose(fp); 308 } 309 310 /* 311 * Push a pattern onto the excludes list. 312 */ 313 void 314 push_excludes(char *pattern) 315 { 316 struct excludes *entry; 317 318 entry = emalloc(sizeof(*entry)); 319 entry->pattern = pattern; 320 entry->next = excludes_list; 321 excludes_list = entry; 322 } 323 324 void 325 print_status(int val, char *path1, char *path2, char *entry) 326 { 327 switch (val) { 328 case D_ONLY: 329 /* must strip off the trailing '/' */ 330 printf("Only in %.*s: %s\n", (int)(strlen(path1) - 1), 331 path1, entry); 332 break; 333 case D_COMMON: 334 printf("Common subdirectories: %s%s and %s%s\n", 335 path1, entry ? entry : "", path2, entry ? entry : ""); 336 break; 337 case D_BINARY: 338 printf("Binary files %s%s and %s%s differ\n", 339 path1, entry ? entry : "", path2, entry ? entry : ""); 340 break; 341 case D_DIFFER: 342 if (format == D_BRIEF) 343 printf("Files %s%s and %s%s differ\n", 344 path1, entry ? entry : "", 345 path2, entry ? entry : ""); 346 break; 347 case D_SAME: 348 if (sflag) 349 printf("Files %s%s and %s%s are identical\n", 350 path1, entry ? entry : "", 351 path2, entry ? entry : ""); 352 break; 353 case D_MISMATCH1: 354 printf("File %s%s is a directory while file %s%s is a regular file\n", 355 path1, entry ? entry : "", path2, entry ? entry : ""); 356 break; 357 case D_MISMATCH2: 358 printf("File %s%s is a regular file while file %s%s is a directory\n", 359 path1, entry ? entry : "", path2, entry ? entry : ""); 360 break; 361 } 362 } 363 364 __dead void 365 usage(void) 366 { 367 (void)fprintf(stderr, 368 "usage: diff [-bdilqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n" 369 " diff [-bdilqtTw] [-L label] -C number file1 file2\n" 370 " diff [-bdilqtw] -D string file1 file2\n" 371 " diff [-bdilqtTw] [-L label] -U number file1 file2\n" 372 " diff [-bdilNPqwtT] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s] [-S name]\n" 373 " [-X file] [-x pattern] dir1 dir2\n"); 374 375 exit(2); 376 } 377