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