1 /* $OpenBSD: diff.c,v 1.19 2003/07/04 02:54:36 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 <string.h> 41 #include <unistd.h> 42 43 #include "diff.h" 44 #include "pathnames.h" 45 46 #if 0 47 static char const sccsid[] = "@(#)diff.c 4.7 5/11/89"; 48 #endif 49 50 /* 51 * diff - driver and subroutines 52 */ 53 int opt; 54 int aflag; /* treat all files as text */ 55 int tflag; /* expand tabs on output */ 56 /* Algorithm related options. */ 57 int hflag; /* -h, use halfhearted DIFFH */ 58 int bflag; /* ignore blanks in comparisons */ 59 int wflag; /* totally ignore blanks in comparisons */ 60 int iflag; /* ignore case in comparisons */ 61 /* Options on hierarchical diffs. */ 62 int lflag; /* long output format with header */ 63 int rflag; /* recursively trace directories */ 64 int sflag; /* announce files which are same */ 65 char *start; /* do file only if name >= this */ 66 /* Variable for -D D_IFDEF option. */ 67 char *ifdefname; /* What we will print for #ifdef/#endif */ 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 /* Variables for diffdir. */ 74 char **diffargv; /* option list to pass to recursive diffs */ 75 76 /* 77 * Input file names. 78 * With diffdir, file1 and file2 are allocated MAXPATHLEN space, 79 * and padded with a '/', and then efile1 and efile2 point after 80 * the '/'. 81 */ 82 char *file1, *file2, *efile1, *efile2; 83 struct stat stb1, stb2; 84 85 __dead void usage(void); 86 87 int 88 main(int argc, char **argv) 89 { 90 int ch; 91 92 status = 2; 93 diffargv = argv; 94 95 while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) { 96 switch (ch) { 97 case 'a': 98 aflag++; 99 break; 100 case 'b': 101 bflag++; 102 break; 103 case 'C': 104 opt = D_CONTEXT; 105 if (!isdigit(*optarg)) 106 usage(); 107 context = atoi(optarg); /* XXX - use strtol */ 108 break; 109 case 'c': 110 opt = D_CONTEXT; 111 context = 3; 112 break; 113 case 'D': 114 opt = D_IFDEF; 115 ifdefname = optarg; 116 break; 117 case 'e': 118 opt = D_EDIT; 119 break; 120 case 'f': 121 opt = D_REVERSE; 122 break; 123 case 'h': 124 hflag++; 125 break; 126 case 'i': 127 iflag++; 128 break; 129 case 'l': 130 lflag++; 131 break; 132 case 'n': 133 opt = D_NREVERSE; 134 break; 135 case 'r': 136 rflag++; 137 break; 138 case 'S': 139 start = optarg; 140 break; 141 case 's': 142 sflag++; 143 break; 144 case 't': 145 tflag++; 146 break; 147 case 'U': 148 opt = D_UNIFIED; 149 if (!isdigit(*optarg)) 150 usage(); 151 context = atoi(optarg); /* XXX - use strtol */ 152 break; 153 case 'u': 154 opt = D_UNIFIED; 155 context = 3; 156 break; 157 case 'w': 158 wflag++; 159 break; 160 default: 161 usage(); 162 break; 163 } 164 } 165 argc -= optind; 166 argv += optind; 167 168 if (argc != 2) 169 errorx("two filename arguments required"); 170 file1 = argv[0]; 171 file2 = argv[1]; 172 if (hflag && opt) 173 errorx("-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U"); 174 if (!strcmp(file1, "-")) 175 stb1.st_mode = S_IFREG; 176 else if (stat(file1, &stb1) < 0) 177 error("%s", file1); 178 if (!strcmp(file2, "-")) 179 stb2.st_mode = S_IFREG; 180 else if (stat(file2, &stb2) < 0) 181 error("%s", file2); 182 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) 183 diffdir(argv); 184 else 185 diffreg(); 186 done(0); 187 } 188 189 int 190 min(int a, int b) 191 { 192 193 return (a < b ? a : b); 194 } 195 196 int 197 max(int a, int b) 198 { 199 200 return (a > b ? a : b); 201 } 202 203 __dead void 204 done(int sig) 205 { 206 if (tempfiles[0] != NULL) 207 unlink(tempfiles[0]); 208 if (tempfiles[1] != NULL) 209 unlink(tempfiles[1]); 210 if (sig) 211 _exit(status); 212 exit(status); 213 } 214 215 void * 216 emalloc(size_t n) 217 { 218 void *p; 219 220 if ((p = malloc(n)) == NULL) 221 error("files too big, try -h"); 222 return (p); 223 } 224 225 void * 226 erealloc(void *p, size_t n) 227 { 228 void *q; 229 230 if ((q = realloc(p, n)) == NULL) 231 error("files too big, try -h"); 232 return (q); 233 } 234 235 __dead void 236 error(const char *fmt, ...) 237 { 238 va_list ap; 239 int sverrno = errno; 240 241 if (tempfiles[0] != NULL) 242 unlink(tempfiles[0]); 243 if (tempfiles[1] != NULL) 244 unlink(tempfiles[1]); 245 errno = sverrno; 246 va_start(ap, fmt); 247 verr(status, fmt, ap); 248 va_end(ap); 249 } 250 251 __dead void 252 errorx(const char *fmt, ...) 253 { 254 va_list ap; 255 256 if (tempfiles[0] != NULL) 257 unlink(tempfiles[0]); 258 if (tempfiles[1] != NULL) 259 unlink(tempfiles[1]); 260 va_start(ap, fmt); 261 verrx(status, fmt, ap); 262 va_end(ap); 263 } 264 265 __dead void 266 usage(void) 267 { 268 (void)fprintf(stderr, 269 "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n" 270 " diff [-bitw] -C number file1 file2\n" 271 " diff [-bitw] -D string file1 file2\n" 272 " diff [-bitw] -U number file1 file2\n" 273 " diff [-biwt] [-c | -e | -f | -h | -n | -u ] " 274 "[-l] [-r] [-s] [-S name]\n dir1 dir2\n"); 275 276 exit(2); 277 } 278