[BACK]Return to diff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / diff

Annotation of src/usr.bin/diff/diff.c, Revision 1.12

1.12    ! tedu        1: /*     $OpenBSD: diff.c,v 1.11 2003/06/25 23:04:53 millert Exp $       */
1.2       deraadt     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:
1.3       tedu       37: #include <stdlib.h>
                     38: #include <unistd.h>
1.1       deraadt    39:
                     40: #include "diff.h"
                     41: #include "pathnames.h"
                     42:
1.3       tedu       43: #if 0
                     44: static char const sccsid[] = "@(#)diff.c 4.7 5/11/89";
                     45: #endif
                     46:
1.1       deraadt    47: /*
                     48:  * diff - driver and subroutines
                     49:  */
1.12    ! tedu       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;
1.1       deraadt    86:
1.3       tedu       87: char diff[] = _PATH_DIFF;
                     88: char diffh[] = _PATH_DIFFH;
                     89: char pr[] = _PATH_PR;
1.1       deraadt    90:
1.3       tedu       91: static void noroom(void);
1.6       millert    92: __dead void usage(void);
1.3       tedu       93:
                     94: int
                     95: main(int argc, char **argv)
1.1       deraadt    96: {
1.6       millert    97:        int ch;
1.1       deraadt    98:
1.3       tedu       99:        ifdef1 = "FILE1";
                    100:        ifdef2 = "FILE2";
1.1       deraadt   101:        status = 2;
                    102:        diffargv = argv;
1.6       millert   103:
1.12    ! tedu      104:        while ((ch = getopt(argc, argv, "abC:cD:efhilnrS:stU:uw")) != -1) {
1.6       millert   105:                switch (ch) {
1.12    ! tedu      106:                case 'a':
        !           107:                        aflag++;
        !           108:                        break;
1.6       millert   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;
1.9       millert   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;
1.6       millert   169:                case 'w':
                    170:                        wflag++;
                    171:                        break;
                    172:                default:
                    173:                        usage();
                    174:                        break;
                    175:                }
1.1       deraadt   176:        }
1.6       millert   177:        argc -= optind;
                    178:        argv += optind;
                    179:
                    180:        if (argc != 2)
                    181:                errx(1, "two filename arguments required");
1.1       deraadt   182:        file1 = argv[0];
                    183:        file2 = argv[1];
1.6       millert   184:        if (hflag && opt)
1.9       millert   185:                errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
1.1       deraadt   186:        if (!strcmp(file1, "-"))
                    187:                stb1.st_mode = S_IFREG;
1.6       millert   188:        else if (stat(file1, &stb1) < 0)
                    189:                err(1, "%s", file1);
1.1       deraadt   190:        if (!strcmp(file2, "-"))
                    191:                stb2.st_mode = S_IFREG;
1.6       millert   192:        else if (stat(file2, &stb2) < 0)
                    193:                err(1, "%s", file2);
1.8       millert   194:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
1.1       deraadt   195:                diffdir(argv);
1.8       millert   196:        else
1.1       deraadt   197:                diffreg();
1.4       deraadt   198:        done(0);
1.1       deraadt   199: }
                    200:
1.3       tedu      201: int
                    202: min(int a, int b)
1.1       deraadt   203: {
                    204:
                    205:        return (a < b ? a : b);
                    206: }
                    207:
1.3       tedu      208: int
                    209: max(int a, int b)
1.1       deraadt   210: {
                    211:
                    212:        return (a > b ? a : b);
                    213: }
                    214:
1.6       millert   215: __dead void
1.4       deraadt   216: done(int sig)
1.1       deraadt   217: {
                    218:        if (tempfile)
                    219:                unlink(tempfile);
1.4       deraadt   220:        if (sig)
                    221:                _exit(status);
1.1       deraadt   222:        exit(status);
1.3       tedu      223: }
1.1       deraadt   224:
1.3       tedu      225: void *
1.8       millert   226: emalloc(size_t n)
1.3       tedu      227: {
                    228:        void *p;
                    229:
                    230:        if ((p = malloc(n)) == NULL)
                    231:                noroom();
                    232:        return (p);
1.1       deraadt   233: }
                    234:
1.3       tedu      235: void *
1.8       millert   236: erealloc(void *p, size_t n)
1.1       deraadt   237: {
1.3       tedu      238:        void *q;
1.1       deraadt   239:
1.3       tedu      240:        if ((q = realloc(p, n)) == NULL)
1.1       deraadt   241:                noroom();
1.3       tedu      242:        return (q);
1.1       deraadt   243: }
                    244:
1.3       tedu      245: static void
                    246: noroom(void)
1.1       deraadt   247: {
1.6       millert   248:        warn("files too big, try -h");
1.4       deraadt   249:        done(0);
1.6       millert   250: }
                    251:
                    252: __dead void
                    253: usage(void)
                    254: {
1.9       millert   255:        (void)fprintf(stderr, "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n"
1.11      millert   256:            "       diff [-bitw] -C number file1 file2\n"
                    257:            "       diff [-bitw] -D string file1 file2\n"
                    258:            "       diff [-bitw] -U number file1 file2\n"
                    259:            "       diff [-biwt] [-c | -e | -f | -h | -n | -u ] [-l] [-r] [-s] [-S name]\n            dir1 dir2\n");
1.6       millert   260:
                    261:        exit(1);
1.1       deraadt   262: }