1*c42aed39Smillert /* $OpenBSD: diff.c,v 1.6 2003/06/25 17:49:22 millert Exp $ */ 2d0c3f575Sderaadt 3d0c3f575Sderaadt /* 4d0c3f575Sderaadt * Copyright (C) Caldera International Inc. 2001-2002. 5d0c3f575Sderaadt * All rights reserved. 6d0c3f575Sderaadt * 7d0c3f575Sderaadt * Redistribution and use in source and binary forms, with or without 8d0c3f575Sderaadt * modification, are permitted provided that the following conditions 9d0c3f575Sderaadt * are met: 10d0c3f575Sderaadt * 1. Redistributions of source code and documentation must retain the above 11d0c3f575Sderaadt * copyright notice, this list of conditions and the following disclaimer. 12d0c3f575Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 13d0c3f575Sderaadt * notice, this list of conditions and the following disclaimer in the 14d0c3f575Sderaadt * documentation and/or other materials provided with the distribution. 15d0c3f575Sderaadt * 3. All advertising materials mentioning features or use of this software 16d0c3f575Sderaadt * must display the following acknowledgement: 17d0c3f575Sderaadt * This product includes software developed or owned by Caldera 18d0c3f575Sderaadt * International, Inc. 19d0c3f575Sderaadt * 4. Neither the name of Caldera International, Inc. nor the names of other 20d0c3f575Sderaadt * contributors may be used to endorse or promote products derived from 21d0c3f575Sderaadt * this software without specific prior written permission. 22d0c3f575Sderaadt * 23d0c3f575Sderaadt * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA 24d0c3f575Sderaadt * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 25d0c3f575Sderaadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26d0c3f575Sderaadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27d0c3f575Sderaadt * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, 28d0c3f575Sderaadt * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29d0c3f575Sderaadt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30d0c3f575Sderaadt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31d0c3f575Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32d0c3f575Sderaadt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33d0c3f575Sderaadt * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34d0c3f575Sderaadt * POSSIBILITY OF SUCH DAMAGE. 35d0c3f575Sderaadt */ 36d0c3f575Sderaadt 3726da422aStedu #include <stdlib.h> 3826da422aStedu #include <unistd.h> 39ae8d569bSderaadt 40ae8d569bSderaadt #include "diff.h" 41ae8d569bSderaadt #include "pathnames.h" 42ae8d569bSderaadt 4326da422aStedu #if 0 4426da422aStedu static char const sccsid[] = "@(#)diff.c 4.7 5/11/89"; 4526da422aStedu #endif 4626da422aStedu 47ae8d569bSderaadt /* 48ae8d569bSderaadt * diff - driver and subroutines 49ae8d569bSderaadt */ 50ae8d569bSderaadt 51ae8d569bSderaadt char diff[] = _PATH_DIFF; 52ae8d569bSderaadt char diffh[] = _PATH_DIFFH; 53ae8d569bSderaadt char pr[] = _PATH_PR; 54ae8d569bSderaadt 5526da422aStedu static void noroom(void); 56*c42aed39Smillert __dead void usage(void); 57ae8d569bSderaadt 5826da422aStedu int 5926da422aStedu main(int argc, char **argv) 6026da422aStedu { 61*c42aed39Smillert int ch; 6226da422aStedu 6326da422aStedu ifdef1 = "FILE1"; 6426da422aStedu ifdef2 = "FILE2"; 65ae8d569bSderaadt status = 2; 66ae8d569bSderaadt diffargv = argv; 67*c42aed39Smillert 68*c42aed39Smillert while ((ch = getopt(argc, argv, "bC:cDefhilnrS:stw")) != -1) { 69*c42aed39Smillert switch (ch) { 70ae8d569bSderaadt case 'b': 71*c42aed39Smillert bflag++; 72*c42aed39Smillert break; 73*c42aed39Smillert case 'C': 74*c42aed39Smillert opt = D_CONTEXT; 75*c42aed39Smillert if (!isdigit(*optarg)) 76*c42aed39Smillert usage(); 77*c42aed39Smillert context = atoi(optarg); /* XXX - use strtol */ 78*c42aed39Smillert break; 79ae8d569bSderaadt case 'c': 80ae8d569bSderaadt opt = D_CONTEXT; 81ae8d569bSderaadt context = 3; 82*c42aed39Smillert break; 83*c42aed39Smillert case 'D': 84*c42aed39Smillert /* -Dfoo = -E -1 -2foo */ 85*c42aed39Smillert opt = D_IFDEF; 86*c42aed39Smillert ifdef1 = ""; 87*c42aed39Smillert ifdef2 = optarg; 88*c42aed39Smillert wantelses++; 89*c42aed39Smillert break; 90*c42aed39Smillert case 'e': 91*c42aed39Smillert opt = D_EDIT; 92*c42aed39Smillert break; 93*c42aed39Smillert case 'f': 94*c42aed39Smillert opt = D_REVERSE; 95*c42aed39Smillert break; 96ae8d569bSderaadt case 'h': 97ae8d569bSderaadt hflag++; 98*c42aed39Smillert break; 99*c42aed39Smillert case 'i': 100*c42aed39Smillert iflag++; 101*c42aed39Smillert break; 102ae8d569bSderaadt case 'l': 103ae8d569bSderaadt lflag++; 104*c42aed39Smillert break; 105*c42aed39Smillert case 'n': 106*c42aed39Smillert opt = D_NREVERSE; 107*c42aed39Smillert break; 108*c42aed39Smillert case 'r': 109*c42aed39Smillert opt = D_REVERSE; 110*c42aed39Smillert break; 111*c42aed39Smillert case 'S': 112*c42aed39Smillert start = optarg; 113*c42aed39Smillert break; 114*c42aed39Smillert case 's': 115*c42aed39Smillert sflag++; 116*c42aed39Smillert break; 117*c42aed39Smillert case 't': 118*c42aed39Smillert tflag++; 119*c42aed39Smillert break; 120*c42aed39Smillert case 'w': 121*c42aed39Smillert wflag++; 122*c42aed39Smillert break; 123ae8d569bSderaadt default: 124*c42aed39Smillert usage(); 125*c42aed39Smillert break; 126ae8d569bSderaadt } 127ae8d569bSderaadt } 128*c42aed39Smillert argc -= optind; 129*c42aed39Smillert argv += optind; 130*c42aed39Smillert 131*c42aed39Smillert if (argc != 2) 132*c42aed39Smillert errx(1, "two filename arguments required"); 133ae8d569bSderaadt file1 = argv[0]; 134ae8d569bSderaadt file2 = argv[1]; 135*c42aed39Smillert if (hflag && opt) 136*c42aed39Smillert errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I or -n"); 137ae8d569bSderaadt if (!strcmp(file1, "-")) 138ae8d569bSderaadt stb1.st_mode = S_IFREG; 139*c42aed39Smillert else if (stat(file1, &stb1) < 0) 140*c42aed39Smillert err(1, "%s", file1); 141ae8d569bSderaadt if (!strcmp(file2, "-")) 142ae8d569bSderaadt stb2.st_mode = S_IFREG; 143*c42aed39Smillert else if (stat(file2, &stb2) < 0) 144*c42aed39Smillert err(1, "%s", file2); 145ae8d569bSderaadt if ((stb1.st_mode & S_IFMT) == S_IFDIR && 146ae8d569bSderaadt (stb2.st_mode & S_IFMT) == S_IFDIR) { 147ae8d569bSderaadt diffdir(argv); 148ae8d569bSderaadt } else 149ae8d569bSderaadt diffreg(); 150a6a14831Sderaadt done(0); 151ae8d569bSderaadt } 152ae8d569bSderaadt 15326da422aStedu int 15426da422aStedu min(int a, int b) 155ae8d569bSderaadt { 156ae8d569bSderaadt 157ae8d569bSderaadt return (a < b ? a : b); 158ae8d569bSderaadt } 159ae8d569bSderaadt 16026da422aStedu int 16126da422aStedu max(int a, int b) 162ae8d569bSderaadt { 163ae8d569bSderaadt 164ae8d569bSderaadt return (a > b ? a : b); 165ae8d569bSderaadt } 166ae8d569bSderaadt 167*c42aed39Smillert __dead void 168a6a14831Sderaadt done(int sig) 169ae8d569bSderaadt { 170ae8d569bSderaadt if (tempfile) 171ae8d569bSderaadt unlink(tempfile); 172a6a14831Sderaadt if (sig) 173a6a14831Sderaadt _exit(status); 174ae8d569bSderaadt exit(status); 175ae8d569bSderaadt } 176ae8d569bSderaadt 17726da422aStedu void * 17826da422aStedu talloc(size_t n) 179ae8d569bSderaadt { 18026da422aStedu void *p; 181ae8d569bSderaadt 18226da422aStedu if ((p = malloc(n)) == NULL) 18326da422aStedu noroom(); 18426da422aStedu return (p); 18526da422aStedu } 18626da422aStedu 18726da422aStedu void * 18826da422aStedu ralloc(void *p, size_t n) 18926da422aStedu { 19026da422aStedu void *q; 19126da422aStedu 19226da422aStedu if ((q = realloc(p, n)) == NULL) 193ae8d569bSderaadt noroom(); 194ae8d569bSderaadt return (q); 195ae8d569bSderaadt } 196ae8d569bSderaadt 19726da422aStedu static void 19826da422aStedu noroom(void) 199ae8d569bSderaadt { 200*c42aed39Smillert warn("files too big, try -h"); 201a6a14831Sderaadt done(0); 202ae8d569bSderaadt } 203*c42aed39Smillert 204*c42aed39Smillert __dead void 205*c42aed39Smillert usage(void) 206*c42aed39Smillert { 207*c42aed39Smillert (void)fprintf(stderr, "usage: diff [-c | -C lines | -e | -f | -h | -n ] [-biwt] file1 file2\n" 208*c42aed39Smillert "usage: diff [-Dstring] [-biw] file1 file2\n" 209*c42aed39Smillert "usage: diff [-l] [-r] [-s] [-c | -C lines | -e | -f | -h | -n ] [-biwt]\n [-Sname] dir1 dir2\n"); 210*c42aed39Smillert 211*c42aed39Smillert exit(1); 212*c42aed39Smillert } 213