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

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