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

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