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

1.45    ! millert     1: /*     $OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $  */
1.2       deraadt     2:
                      3: /*
1.23      millert     4:  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.2       deraadt     5:  *
1.23      millert     6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.2       deraadt     9:  *
1.23      millert    10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  *
                     18:  * Sponsored in part by the Defense Advanced Research Projects
                     19:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     20:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.2       deraadt    21:  */
                     22:
1.23      millert    23: #ifndef lint
1.45    ! millert    24: static const char rcsid[] = "$OpenBSD: diff.c,v 1.44 2004/01/07 17:18:32 otto Exp $";
1.23      millert    25: #endif /* not lint */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
1.42      millert    30: #include <ctype.h>
1.23      millert    31: #include <err.h>
1.15      millert    32: #include <errno.h>
1.23      millert    33: #include <getopt.h>
1.28      millert    34: #include <signal.h>
1.3       tedu       35: #include <stdlib.h>
1.23      millert    36: #include <stdio.h>
1.15      millert    37: #include <stdarg.h>
1.18      david      38: #include <string.h>
1.3       tedu       39: #include <unistd.h>
1.1       deraadt    40:
                     41: #include "diff.h"
1.12      tedu       42:
1.44      otto       43: int     aflag, bflag, dflag, iflag, lflag, Nflag, Pflag, pflag, rflag;
1.37      deraadt    44: int     sflag, tflag, Tflag, wflag;
1.23      millert    45: int     format, context, status;
1.33      millert    46: char   *start, *ifdefname, *diffargs, *label;
1.23      millert    47: struct stat stb1, stb2;
                     48: struct excludes *excludes_list;
                     49:
1.44      otto       50: #define        OPTIONS "0123456789abC:cdD:efhiL:lnNPpqrS:sTtU:uwX:x:"
1.23      millert    51: static struct option longopts[] = {
                     52:        { "text",                       no_argument,            0,      'a' },
                     53:        { "ignore-space-change",        no_argument,            0,      'b' },
                     54:        { "context",                    optional_argument,      0,      'C' },
                     55:        { "ifdef",                      required_argument,      0,      'D' },
1.35      otto       56:        { "minimal",                    no_argument,            0,      'd' },
1.23      millert    57:        { "ed",                         no_argument,            0,      'e' },
                     58:        { "forward-ed",                 no_argument,            0,      'f' },
                     59:        { "ignore-case",                no_argument,            0,      'i' },
1.27      millert    60:        { "paginate",                   no_argument,            0,      'l' },
1.33      millert    61:        { "label",                      required_argument,      0,      'L' },
1.23      millert    62:        { "new-file",                   no_argument,            0,      'N' },
                     63:        { "rcs",                        no_argument,            0,      'n' },
1.24      millert    64:        { "unidirectional-new-file",    no_argument,            0,      'P' },
1.44      otto       65:        { "show-c-function",            no_argument,            0,      'p' },
1.25      millert    66:        { "brief",                      no_argument,            0,      'q' },
1.23      millert    67:        { "recursive",                  no_argument,            0,      'r' },
                     68:        { "report-identical-files",     no_argument,            0,      's' },
                     69:        { "starting-file",              required_argument,      0,      'S' },
                     70:        { "expand-tabs",                no_argument,            0,      't' },
1.38      david      71:        { "initial-tab",                no_argument,            0,      'T' },
1.23      millert    72:        { "unified",                    optional_argument,      0,      'U' },
                     73:        { "ignore-all-space",           no_argument,            0,      'w' },
                     74:        { "exclude",                    required_argument,      0,      'x' },
                     75:        { "exclude-from",               required_argument,      0,      'X' },
1.34      millert    76:        { NULL,                         0,                      0,      '\0'}
1.23      millert    77: };
1.1       deraadt    78:
1.6       millert    79: __dead void usage(void);
1.23      millert    80: void push_excludes(char *);
                     81: void read_excludes_file(char *file);
                     82: void set_argstr(char **, char **);
1.3       tedu       83:
                     84: int
                     85: main(int argc, char **argv)
1.1       deraadt    86: {
1.23      millert    87:        char *ep, **oargv;
                     88:        long  l;
1.42      millert    89:        int   ch, lastch, gotstdin, prevoptind, newarg;
1.1       deraadt    90:
1.23      millert    91:        oargv = argv;
                     92:        gotstdin = 0;
1.6       millert    93:
1.42      millert    94:        lastch = '\0';
                     95:        prevoptind = 1;
                     96:        newarg = 1;
1.23      millert    97:        while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
1.6       millert    98:                switch (ch) {
1.40      tedu       99:                case '0': case '1': case '2': case '3': case '4':
                    100:                case '5': case '6': case '7': case '8': case '9':
1.42      millert   101:                        if (newarg)
                    102:                                usage();        /* disallow -[0-9]+ */
                    103:                        else if (lastch == 'c' || lastch == 'u')
                    104:                                context = 0;
                    105:                        else if (!isdigit(lastch) || context > INT_MAX / 10)
1.40      tedu      106:                                usage();
1.42      millert   107:                        context = (context * 10) + (ch - '0');
1.40      tedu      108:                        break;
1.12      tedu      109:                case 'a':
1.23      millert   110:                        aflag = 1;
1.12      tedu      111:                        break;
1.6       millert   112:                case 'b':
1.23      millert   113:                        bflag = 1;
1.6       millert   114:                        break;
                    115:                case 'C':
                    116:                case 'c':
1.23      millert   117:                        format = D_CONTEXT;
                    118:                        if (optarg != NULL) {
                    119:                                l = strtol(optarg, &ep, 10);
                    120:                                if (*ep != '\0' || l < 0 || l >= INT_MAX)
                    121:                                        usage();
                    122:                                context = (int)l;
                    123:                        } else
                    124:                                context = 3;
1.6       millert   125:                        break;
1.35      otto      126:                case 'd':
                    127:                        dflag = 1;
                    128:                        break;
1.6       millert   129:                case 'D':
1.23      millert   130:                        format = D_IFDEF;
1.17      millert   131:                        ifdefname = optarg;
1.6       millert   132:                        break;
                    133:                case 'e':
1.23      millert   134:                        format = D_EDIT;
1.6       millert   135:                        break;
                    136:                case 'f':
1.23      millert   137:                        format = D_REVERSE;
1.6       millert   138:                        break;
1.22      millert   139:                case 'h':
                    140:                        /* silently ignore for backwards compatibility */
                    141:                        break;
1.6       millert   142:                case 'i':
1.23      millert   143:                        iflag = 1;
                    144:                        break;
1.33      millert   145:                case 'L':
                    146:                        label = optarg;
                    147:                        break;
1.27      millert   148:                case 'l':
                    149:                        lflag = 1;
1.28      millert   150:                        signal(SIGPIPE, SIG_IGN);
1.27      millert   151:                        break;
1.23      millert   152:                case 'N':
                    153:                        Nflag = 1;
1.6       millert   154:                        break;
                    155:                case 'n':
1.23      millert   156:                        format = D_NREVERSE;
1.6       millert   157:                        break;
1.44      otto      158:                case 'p':
                    159:                        pflag = 1;
                    160:                        break;
1.24      millert   161:                case 'P':
                    162:                        Pflag = 1;
                    163:                        break;
1.6       millert   164:                case 'r':
1.23      millert   165:                        rflag = 1;
1.6       millert   166:                        break;
1.25      millert   167:                case 'q':
                    168:                        format = D_BRIEF;
                    169:                        break;
1.6       millert   170:                case 'S':
                    171:                        start = optarg;
                    172:                        break;
                    173:                case 's':
1.23      millert   174:                        sflag = 1;
1.6       millert   175:                        break;
1.33      millert   176:                case 'T':
                    177:                        Tflag = 1;
                    178:                        break;
1.6       millert   179:                case 't':
1.23      millert   180:                        tflag = 1;
1.6       millert   181:                        break;
1.9       millert   182:                case 'U':
                    183:                case 'u':
1.23      millert   184:                        format = D_UNIFIED;
                    185:                        if (optarg != NULL) {
                    186:                                l = strtol(optarg, &ep, 10);
                    187:                                if (*ep != '\0' || l < 0 || l >= INT_MAX)
                    188:                                        usage();
                    189:                                context = (int)l;
                    190:                        } else
                    191:                                context = 3;
1.9       millert   192:                        break;
1.6       millert   193:                case 'w':
1.23      millert   194:                        wflag = 1;
                    195:                        break;
                    196:                case 'X':
                    197:                        read_excludes_file(optarg);
                    198:                        break;
                    199:                case 'x':
                    200:                        push_excludes(optarg);
1.6       millert   201:                        break;
                    202:                default:
                    203:                        usage();
                    204:                        break;
                    205:                }
1.40      tedu      206:                lastch = ch;
1.42      millert   207:                newarg = optind != prevoptind;
                    208:                prevoptind = optind;
1.1       deraadt   209:        }
1.6       millert   210:        argc -= optind;
                    211:        argv += optind;
                    212:
1.23      millert   213:        /*
                    214:         * Do sanity checks, fill in stb1 and stb2 and call the appropriate
                    215:         * driver routine.  Both drivers use the contents of stb1 and stb2.
                    216:         */
1.6       millert   217:        if (argc != 2)
1.23      millert   218:                usage();
                    219:        if (strcmp(argv[0], "-") == 0) {
1.26      millert   220:                fstat(STDIN_FILENO, &stb1);
1.23      millert   221:                gotstdin = 1;
                    222:        } else if (stat(argv[0], &stb1) != 0)
1.28      millert   223:                err(2, "%s", argv[0]);
1.23      millert   224:        if (strcmp(argv[1], "-") == 0) {
1.26      millert   225:                fstat(STDIN_FILENO, &stb2);
1.23      millert   226:                gotstdin = 1;
                    227:        } else if (stat(argv[1], &stb2) != 0)
1.28      millert   228:                err(2, "%s", argv[1]);
1.23      millert   229:        if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
1.28      millert   230:                errx(2, "can't compare - to a directory");
1.36      millert   231:        set_argstr(oargv + 1, argv);
1.23      millert   232:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
                    233:                if (format == D_IFDEF)
1.28      millert   234:                        errx(2, "-D option not supported with directories");
1.23      millert   235:                diffdir(argv[0], argv[1]);
1.27      millert   236:        } else {
1.28      millert   237:                if (S_ISDIR(stb1.st_mode)) {
                    238:                        argv[0] = splice(argv[0], argv[1]);
                    239:                        if (stat(argv[0], &stb1) < 0)
                    240:                                err(2, "%s", argv[0]);
                    241:                }
                    242:                if (S_ISDIR(stb2.st_mode)) {
                    243:                        argv[1] = splice(argv[1], argv[0]);
                    244:                        if (stat(argv[1], &stb2) < 0)
                    245:                                err(2, "%s", argv[1]);
                    246:                }
1.27      millert   247:                print_status(diffreg(argv[0], argv[1], 0), argv[0], argv[1],
                    248:                    NULL);
                    249:        }
1.23      millert   250:        exit(status);
1.1       deraadt   251: }
                    252:
1.3       tedu      253: void *
1.8       millert   254: emalloc(size_t n)
1.3       tedu      255: {
                    256:        void *p;
                    257:
                    258:        if ((p = malloc(n)) == NULL)
1.28      millert   259:                err(2, NULL);
1.3       tedu      260:        return (p);
1.1       deraadt   261: }
                    262:
1.3       tedu      263: void *
1.8       millert   264: erealloc(void *p, size_t n)
1.1       deraadt   265: {
1.3       tedu      266:        void *q;
1.1       deraadt   267:
1.3       tedu      268:        if ((q = realloc(p, n)) == NULL)
1.28      millert   269:                err(2, NULL);
1.3       tedu      270:        return (q);
1.1       deraadt   271: }
                    272:
1.27      millert   273: int
                    274: easprintf(char **ret, const char *fmt, ...)
                    275: {
                    276:        int len;
                    277:        va_list ap;
                    278:
                    279:        va_start(ap, fmt);
                    280:        len = vasprintf(ret, fmt, ap);
                    281:        va_end(ap);
                    282:
                    283:        if (len == -1)
1.28      millert   284:                err(2, NULL);
1.30      henning   285:        return (len);
1.27      millert   286: }
                    287:
1.23      millert   288: void
                    289: set_argstr(char **av, char **ave)
                    290: {
                    291:        size_t argsize;
                    292:        char **ap;
                    293:
1.36      millert   294:        argsize = 4 + *ave - *av + 1;
1.23      millert   295:        diffargs = emalloc(argsize);
                    296:        strlcpy(diffargs, "diff", argsize);
                    297:        for (ap = av + 1; ap < ave; ap++) {
                    298:                if (strcmp(*ap, "--") != 0) {
                    299:                        strlcat(diffargs, " ", argsize);
                    300:                        strlcat(diffargs, *ap, argsize);
                    301:                }
                    302:        }
                    303: }
                    304:
                    305: /*
                    306:  * Read in an excludes file and push each line.
                    307:  */
                    308: void
                    309: read_excludes_file(char *file)
                    310: {
                    311:        FILE *fp;
                    312:        char *buf, *pattern;
                    313:        size_t len;
                    314:
                    315:        if (strcmp(file, "-") == 0)
                    316:                fp = stdin;
                    317:        else if ((fp = fopen(file, "r")) == NULL)
1.28      millert   318:                err(2, "%s", file);
1.23      millert   319:        while ((buf = fgetln(fp, &len)) != NULL) {
                    320:                if (buf[len - 1] == '\n')
                    321:                        len--;
                    322:                pattern = emalloc(len + 1);
                    323:                memcpy(pattern, buf, len);
                    324:                pattern[len] = '\0';
                    325:                push_excludes(pattern);
                    326:        }
                    327:        if (strcmp(file, "-") != 0)
                    328:                fclose(fp);
                    329: }
                    330:
                    331: /*
                    332:  * Push a pattern onto the excludes list.
                    333:  */
                    334: void
                    335: push_excludes(char *pattern)
                    336: {
                    337:        struct excludes *entry;
                    338:
                    339:        entry = emalloc(sizeof(*entry));
                    340:        entry->pattern = pattern;
                    341:        entry->next = excludes_list;
                    342:        excludes_list = entry;
1.27      millert   343: }
                    344:
                    345: void
1.43      millert   346: print_only(const char *path, size_t dirlen, const char *entry)
                    347: {
                    348:        if (dirlen > 1)
                    349:                dirlen--;
                    350:        printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
                    351: }
                    352:
                    353: void
1.27      millert   354: print_status(int val, char *path1, char *path2, char *entry)
                    355: {
                    356:        switch (val) {
                    357:        case D_ONLY:
1.43      millert   358:                print_only(path1, strlen(path1), entry);
1.27      millert   359:                break;
                    360:        case D_COMMON:
                    361:                printf("Common subdirectories: %s%s and %s%s\n",
                    362:                    path1, entry ? entry : "", path2, entry ? entry : "");
                    363:                break;
                    364:        case D_BINARY:
                    365:                printf("Binary files %s%s and %s%s differ\n",
                    366:                    path1, entry ? entry : "", path2, entry ? entry : "");
                    367:                break;
                    368:        case D_DIFFER:
                    369:                if (format == D_BRIEF)
                    370:                        printf("Files %s%s and %s%s differ\n",
                    371:                            path1, entry ? entry : "",
                    372:                            path2, entry ? entry : "");
                    373:                break;
                    374:        case D_SAME:
                    375:                if (sflag)
                    376:                        printf("Files %s%s and %s%s are identical\n",
                    377:                            path1, entry ? entry : "",
                    378:                            path2, entry ? entry : "");
1.28      millert   379:                break;
1.29      millert   380:        case D_MISMATCH1:
1.31      millert   381:                printf("File %s%s is a directory while file %s%s is a regular file\n",
1.29      millert   382:                    path1, entry ? entry : "", path2, entry ? entry : "");
                    383:                break;
                    384:        case D_MISMATCH2:
1.31      millert   385:                printf("File %s%s is a regular file while file %s%s is a directory\n",
1.28      millert   386:                    path1, entry ? entry : "", path2, entry ? entry : "");
1.45    ! millert   387:                break;
        !           388:        case D_SKIPPED1:
        !           389:                printf("File %s%s is not a regular file or directory and was skipped\n",
        !           390:                    path1, entry ? entry : "");
        !           391:                break;
        !           392:        case D_SKIPPED2:
        !           393:                printf("File %s%s is not a regular file or directory and was skipped\n",
        !           394:                    path2, entry ? entry : "");
1.27      millert   395:                break;
                    396:        }
1.23      millert   397: }
                    398:
1.6       millert   399: __dead void
                    400: usage(void)
                    401: {
1.14      deraadt   402:        (void)fprintf(stderr,
1.44      otto      403:            "usage: diff [-abdilpqtTw] [-c | -e | -f | -n | -u] [-L label] file1 file2\n"
                    404:            "       diff [-abdilpqtTw] [-L label] -C number file1 file2\n"
1.41      jmc       405:            "       diff [-abdilqtw] -D string file1 file2\n"
1.44      otto      406:            "       diff [-abdilpqtTw] [-L label] -U number file1 file2\n"
                    407:            "       diff [-abdilNPpqtTw] [-c | -e | -f | -n | -u ] [-L label] [-r] [-s]\n"
1.41      jmc       408:            "            [-S name] [-X file] [-x pattern] dir1 dir2\n");
1.6       millert   409:
1.15      millert   410:        exit(2);
1.1       deraadt   411: }