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