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

1.8     ! millert     1: /*     $OpenBSD: diff.c,v 1.7 2003/06/25 19:56:57 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.7       millert    68:        while ((ch = getopt(argc, argv, "bC:cD:efhilnrS:stw")) != -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;
                    120:                case 'w':
                    121:                        wflag++;
                    122:                        break;
                    123:                default:
                    124:                        usage();
                    125:                        break;
                    126:                }
1.1       deraadt   127:        }
1.6       millert   128:        argc -= optind;
                    129:        argv += optind;
                    130:
                    131:        if (argc != 2)
                    132:                errx(1, "two filename arguments required");
1.1       deraadt   133:        file1 = argv[0];
                    134:        file2 = argv[1];
1.6       millert   135:        if (hflag && opt)
                    136:                errx(1, "-h doesn't support -D, -c, -C, -e, -f, -I or -n");
1.1       deraadt   137:        if (!strcmp(file1, "-"))
                    138:                stb1.st_mode = S_IFREG;
1.6       millert   139:        else if (stat(file1, &stb1) < 0)
                    140:                err(1, "%s", file1);
1.1       deraadt   141:        if (!strcmp(file2, "-"))
                    142:                stb2.st_mode = S_IFREG;
1.6       millert   143:        else if (stat(file2, &stb2) < 0)
                    144:                err(1, "%s", file2);
1.8     ! millert   145:        if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode))
1.1       deraadt   146:                diffdir(argv);
1.8     ! millert   147:        else
1.1       deraadt   148:                diffreg();
1.4       deraadt   149:        done(0);
1.1       deraadt   150: }
                    151:
1.3       tedu      152: int
                    153: min(int a, int b)
1.1       deraadt   154: {
                    155:
                    156:        return (a < b ? a : b);
                    157: }
                    158:
1.3       tedu      159: int
                    160: max(int a, int b)
1.1       deraadt   161: {
                    162:
                    163:        return (a > b ? a : b);
                    164: }
                    165:
1.6       millert   166: __dead void
1.4       deraadt   167: done(int sig)
1.1       deraadt   168: {
                    169:        if (tempfile)
                    170:                unlink(tempfile);
1.4       deraadt   171:        if (sig)
                    172:                _exit(status);
1.1       deraadt   173:        exit(status);
1.3       tedu      174: }
1.1       deraadt   175:
1.3       tedu      176: void *
1.8     ! millert   177: emalloc(size_t n)
1.3       tedu      178: {
                    179:        void *p;
                    180:
                    181:        if ((p = malloc(n)) == NULL)
                    182:                noroom();
                    183:        return (p);
1.1       deraadt   184: }
                    185:
1.3       tedu      186: void *
1.8     ! millert   187: erealloc(void *p, size_t n)
1.1       deraadt   188: {
1.3       tedu      189:        void *q;
1.1       deraadt   190:
1.3       tedu      191:        if ((q = realloc(p, n)) == NULL)
1.1       deraadt   192:                noroom();
1.3       tedu      193:        return (q);
1.1       deraadt   194: }
                    195:
1.3       tedu      196: static void
                    197: noroom(void)
1.1       deraadt   198: {
1.6       millert   199:        warn("files too big, try -h");
1.4       deraadt   200:        done(0);
1.6       millert   201: }
                    202:
                    203: __dead void
                    204: usage(void)
                    205: {
                    206:        (void)fprintf(stderr, "usage: diff [-c | -C lines | -e | -f | -h | -n ] [-biwt] file1 file2\n"
                    207:            "usage: diff [-Dstring] [-biw] file1 file2\n"
                    208:            "usage: diff [-l] [-r] [-s] [-c | -C lines | -e | -f | -h | -n ] [-biwt]\n            [-Sname] dir1 dir2\n");
                    209:
                    210:        exit(1);
1.1       deraadt   211: }