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

1.9     ! millert     1: /*     $OpenBSD: diff.c,v 1.8 2003/06/25 21:43:49 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:  */
                     50:
1.3       tedu       51: char diff[] = _PATH_DIFF;
                     52: char diffh[] = _PATH_DIFFH;
                     53: char pr[] = _PATH_PR;
1.1       deraadt    54:
1.3       tedu       55: static void noroom(void);
1.6       millert    56: __dead void usage(void);
1.3       tedu       57:
                     58: int
                     59: main(int argc, char **argv)
1.1       deraadt    60: {
1.6       millert    61:        int ch;
1.1       deraadt    62:
1.3       tedu       63:        ifdef1 = "FILE1";
                     64:        ifdef2 = "FILE2";
1.1       deraadt    65:        status = 2;
                     66:        diffargv = argv;
1.6       millert    67:
1.9     ! millert    68:        while ((ch = getopt(argc, argv, "bC:cD:efhilnrS:stU:uw")) != -1) {
1.6       millert    69:                switch (ch) {
                     70:                case 'b':
                     71:                        bflag++;
                     72:                        break;
                     73:                case 'C':
                     74:                        opt = D_CONTEXT;
                     75:                        if (!isdigit(*optarg))
                     76:                                usage();
                     77:                        context = atoi(optarg); /* XXX - use strtol */
                     78:                        break;
                     79:                case 'c':
                     80:                        opt = D_CONTEXT;
                     81:                        context = 3;
                     82:                        break;
                     83:                case 'D':
                     84:                        /* -Dfoo = -E -1 -2foo */
                     85:                        opt = D_IFDEF;
                     86:                        ifdef1 = "";
                     87:                        ifdef2 = optarg;
                     88:                        wantelses++;
                     89:                        break;
                     90:                case 'e':
                     91:                        opt = D_EDIT;
                     92:                        break;
                     93:                case 'f':
                     94:                        opt = D_REVERSE;
                     95:                        break;
                     96:                case 'h':
                     97:                        hflag++;
                     98:                        break;
                     99:                case 'i':
                    100:                        iflag++;
                    101:                        break;
                    102:                case 'l':
                    103:                        lflag++;
                    104:                        break;
                    105:                case 'n':
                    106:                        opt = D_NREVERSE;
                    107:                        break;
                    108:                case 'r':
                    109:                        opt = D_REVERSE;
                    110:                        break;
                    111:                case 'S':
                    112:                        start = optarg;
                    113:                        break;
                    114:                case 's':
                    115:                        sflag++;
                    116:                        break;
                    117:                case 't':
                    118:                        tflag++;
                    119:                        break;
1.9     ! millert   120:                case 'U':
        !           121:                        opt = D_UNIFIED;
        !           122:                        if (!isdigit(*optarg))
        !           123:                                usage();
        !           124:                        context = atoi(optarg); /* XXX - use strtol */
        !           125:                        break;
        !           126:                case 'u':
        !           127:                        opt = D_UNIFIED;
        !           128:                        context = 3;
        !           129:                        break;
1.6       millert   130:                case 'w':
                    131:                        wflag++;
                    132:                        break;
                    133:                default:
                    134:                        usage();
                    135:                        break;
                    136:                }
1.1       deraadt   137:        }
1.6       millert   138:        argc -= optind;
                    139:        argv += optind;
                    140:
                    141:        if (argc != 2)
                    142:                errx(1, "two filename arguments required");
1.1       deraadt   143:        file1 = argv[0];
                    144:        file2 = argv[1];
1.6       millert   145:        if (hflag && opt)
1.9     ! millert   146:                errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I, -n, -u or -U");
1.1       deraadt   147:        if (!strcmp(file1, "-"))
                    148:                stb1.st_mode = S_IFREG;
1.6       millert   149:        else if (stat(file1, &stb1) < 0)
                    150:                err(1, "%s", file1);
1.1       deraadt   151:        if (!strcmp(file2, "-"))
                    152:                stb2.st_mode = S_IFREG;
1.6       millert   153:        else if (stat(file2, &stb2) < 0)
                    154:                err(1, "%s", file2);
1.8       millert   155:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
1.1       deraadt   156:                diffdir(argv);
1.8       millert   157:        else
1.1       deraadt   158:                diffreg();
1.4       deraadt   159:        done(0);
1.1       deraadt   160: }
                    161:
1.3       tedu      162: int
                    163: min(int a, int b)
1.1       deraadt   164: {
                    165:
                    166:        return (a < b ? a : b);
                    167: }
                    168:
1.3       tedu      169: int
                    170: max(int a, int b)
1.1       deraadt   171: {
                    172:
                    173:        return (a > b ? a : b);
                    174: }
                    175:
1.6       millert   176: __dead void
1.4       deraadt   177: done(int sig)
1.1       deraadt   178: {
                    179:        if (tempfile)
                    180:                unlink(tempfile);
1.4       deraadt   181:        if (sig)
                    182:                _exit(status);
1.1       deraadt   183:        exit(status);
1.3       tedu      184: }
1.1       deraadt   185:
1.3       tedu      186: void *
1.8       millert   187: emalloc(size_t n)
1.3       tedu      188: {
                    189:        void *p;
                    190:
                    191:        if ((p = malloc(n)) == NULL)
                    192:                noroom();
                    193:        return (p);
1.1       deraadt   194: }
                    195:
1.3       tedu      196: void *
1.8       millert   197: erealloc(void *p, size_t n)
1.1       deraadt   198: {
1.3       tedu      199:        void *q;
1.1       deraadt   200:
1.3       tedu      201:        if ((q = realloc(p, n)) == NULL)
1.1       deraadt   202:                noroom();
1.3       tedu      203:        return (q);
1.1       deraadt   204: }
                    205:
1.3       tedu      206: static void
                    207: noroom(void)
1.1       deraadt   208: {
1.6       millert   209:        warn("files too big, try -h");
1.4       deraadt   210:        done(0);
1.6       millert   211: }
                    212:
                    213: __dead void
                    214: usage(void)
                    215: {
1.9     ! millert   216:        (void)fprintf(stderr, "usage: diff [-bitw] [-c | -e | -f | -h | -n | -u ] file1 file2\n"
        !           217:            "       diff [-biw] -Dstring file1 file2\n"
        !           218:            "       diff [-biwt] [-c | -e | -f | -h | -n | -u ] [-l] [-r] [-s] [-Sname]\n            dir1 dir2\n"
        !           219:            "       diff [-bitw] -Cnumber [file1 file2 | dir1 dir2]\n"
        !           220:            "       diff [-bitw] -Unumber [file1 file2 | dir1 dir2]\n");
1.6       millert   221:
                    222:        exit(1);
1.1       deraadt   223: }