1 /* $OpenBSD: diff.c,v 1.17 2003/06/26 22:04:45 millert Exp $ */ 2 3 /* 4 * Copyright (C) Caldera International Inc. 2001-2002. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code and documentation must retain the above 11 * copyright notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed or owned by Caldera 18 * International, Inc. 19 * 4. Neither the name of Caldera International, Inc. nor the names of other 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA 24 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, 28 * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <stdarg.h> 40 #include <unistd.h> 41 42 #include "diff.h" 43 #include "pathnames.h" 44 45 #if 0 46 static char const sccsid[] = "@(#)diff.c 4.7 5/11/89"; 47 #endif 48 49 /* 50 * diff - driver and subroutines 51 */ 52 int opt; 53 int aflag; /* treat all files as text */ 54 int tflag; /* expand tabs on output */ 55 /* Algorithm related options. */ 56 int hflag; /* -h, use halfhearted DIFFH */ 57 int bflag; /* ignore blanks in comparisons */ 58 int wflag; /* totally ignore blanks in comparisons */ 59 int iflag; /* ignore case in comparisons */ 60 /* Options on hierarchical diffs. */ 61 int lflag; /* long output format with header */ 62 int rflag; /* recursively trace directories */ 63 int sflag; /* announce files which are same */ 64 char *start; /* do file only if name >= this */ 65 /* Variables for -D D_IFDEF option. */ 66 char *ifdefname; /* What we will print for #ifdef/#endif */ 67 int inifdef; 68 /* Variables for -c and -u context option. */ 69 int context; /* lines of context to be printed */ 70 /* State for exit status. */ 71 int status; 72 int anychange; 73 char *tempfile; /* used when comparing against std input */ 74 /* Variables for diffdir. */ 75 char **diffargv; /* option list to pass to recursive diffs */ 76 77 /* 78 * Input file names. 79 * With diffdir, file1 and file2 are allocated BUFSIZ space, 80 * and padded with a '/', and then efile0 and efile1 point after 81 * the '/'. 82 */ 83 char *file1, *file2, *efile1, *efile2; 84 struct stat stb1, stb2; 85 86 const char *diff = _PATH_DIFF; 87 const char *diffh = _PATH_DIFFH; 88 const char *pr = _PATH_PR; 89 90 __dead void usage(void); 91 92 int 93 main(int argc, char **argv) 94 { 95 int ch; 96 97 status = 2; 98 diffargv = argv; 99 100 while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) { 101 switch (ch) { 102 case 'a': 103 aflag++; 104 break; 105 case 'b': 106 bflag++; 107 break; 108 case 'C': 109 opt = D_CONTEXT; 110 if (!isdigit(*optarg)) 111 usage(); 112 context = atoi(optarg); /* XXX - use strtol */ 113 break; 114 case 'c': 115 opt = D_CONTEXT; 116 context = 3; 117 break; 118 case 'D': 119 opt = D_IFDEF; 120 ifdefname = optarg; 121 break; 122 case 'e': 123 opt = D_EDIT; 124 break; 125 case 'f': 126 opt = D_REVERSE; 127 break; 128 case 'h': 129 hflag++; 130 break; 131 case 'i': 132 iflag++; 133 break; 134 case 'l': 135 lflag++; 136 break; 137 case 'n': 138 opt = D_NREVERSE; 139 break; 140 case 'r': 141 rflag++; 142 break; 143 case 'S': 144 start = optarg; 145 break; 146 case 's': 147 sflag++; 148 break; 149 case 't': 150 tflag++; 151 break; 152 case 'U': 153 opt = D_UNIFIED; 154 if (!isdigit(*optarg)) 155 usage(); 156 context = atoi(optarg); /* XXX - use strtol */ 157 break; 158 case 'u': 159 opt = D_UNIFIED; 160 context = 3; 161 break; 162 case 'w': 163 wflag++; 164 break; 165 default: 166 usage(); 167 break; 168 } 169 } 170 argc -= optind; 171 argv += optind; 172 173 if (argc != 2) 174 errorx("two filename arguments required"); 175 file1 = argv[0]; 176 file2 = argv[1]; 177 if (hflag && opt) 178 errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U"); 179 if (!strcmp(file1, "-")) 180 stb1.st_mode = S_IFREG; 181 else if (stat(file1, &stb1) < 0) 182 error("%s", file1); 183 if (!strcmp(file2, "-")) 184 stb2.st_mode = S_IFREG; 185 else if (stat(file2, &stb2) < 0) 186 error("%s", file2); 187 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) 188 diffdir(argv); 189 else 190 diffreg(); 191 done(0); 192 } 193 194 int 195 min(int a, int b) 196 { 197 198 return (a < b ? a : b); 199 } 200 201 int 202 max(int a, int b) 203 { 204 205 return (a > b ? a : b); 206 } 207 208 __dead void 209 done(int sig) 210 { 211 if (tempfiles[0] != NULL) 212 unlink(tempfiles[0]); 213 if (tempfiles[1] != NULL) 214 unlink(tempfiles[1]); 215 if (sig) 216 _exit(status); 217 exit(status); 218 } 219 220 void * 221 emalloc(size_t n) 222 { 223 void *p; 224 225 if ((p = malloc(n)) == NULL) 226 error("files too big, try -h"); 227 return (p); 228 } 229 230 void * 231 erealloc(void *p, size_t n) 232 { 233 void *q; 234 235 if ((q = realloc(p, n)) == NULL) 236 error("files too big, try -h"); 237 return (q); 238 } 239 240 __dead void 241 error(const char *fmt, ...) 242 { 243 va_list ap; 244 int sverrno = errno; 245 246 if (tempfiles[0] != NULL) 247 unlink(tempfiles[0]); 248 if (tempfiles[1] != NULL) 249 unlink(tempfiles[1]); 250 errno = sverrno; 251 va_start(ap, fmt); 252 verr(status, fmt, ap); 253 va_end(ap); 254 } 255 256 __dead void 257 errorx(const char *fmt, ...) 258 { 259 va_list ap; 260 261 if (tempfiles[0] != NULL) 262 unlink(tempfiles[0]); 263 if (tempfiles[1] != NULL) 264 unlink(tempfiles[1]); 265 va_start(ap, fmt); 266 verrx(status, fmt, ap); 267 va_end(ap); 268 } 269 270 __dead void 271 usage(void) 272 { 273 (void)fprintf(stderr, 274 "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n" 275 " diff [-bitw] -C number file1 file2\n" 276 " diff [-bitw] -D string file1 file2\n" 277 " diff [-bitw] -U number file1 file2\n" 278 " diff [-biwt] [-c | -e | -f | -h | -n | -u ] " 279 "[-l] [-r] [-s] [-S name]\n dir1 dir2\n"); 280 281 exit(2); 282 } 283