[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.17

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