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