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

1.6     ! millert     1: /*     $OpenBSD: diff.c,v 1.5 2003/06/25 07:26:59 tedu 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:
        !            68:        while ((ch = getopt(argc, argv, "bC:cDefhilnrS:stw")) != -1) {
        !            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.1       deraadt   145:        if ((stb1.st_mode & S_IFMT) == S_IFDIR &&
                    146:            (stb2.st_mode & S_IFMT) == S_IFDIR) {
                    147:                diffdir(argv);
                    148:        } else
                    149:                diffreg();
1.4       deraadt   150:        done(0);
1.1       deraadt   151: }
                    152:
1.3       tedu      153: int
                    154: min(int a, int b)
1.1       deraadt   155: {
                    156:
                    157:        return (a < b ? a : b);
                    158: }
                    159:
1.3       tedu      160: int
                    161: max(int a, int b)
1.1       deraadt   162: {
                    163:
                    164:        return (a > b ? a : b);
                    165: }
                    166:
1.6     ! millert   167: __dead void
1.4       deraadt   168: done(int sig)
1.1       deraadt   169: {
                    170:        if (tempfile)
                    171:                unlink(tempfile);
1.4       deraadt   172:        if (sig)
                    173:                _exit(status);
1.1       deraadt   174:        exit(status);
1.3       tedu      175: }
1.1       deraadt   176:
1.3       tedu      177: void *
                    178: talloc(size_t n)
                    179: {
                    180:        void *p;
                    181:
                    182:        if ((p = malloc(n)) == NULL)
                    183:                noroom();
                    184:        return (p);
1.1       deraadt   185: }
                    186:
1.3       tedu      187: void *
                    188: ralloc(void *p, size_t n)
1.1       deraadt   189: {
1.3       tedu      190:        void *q;
1.1       deraadt   191:
1.3       tedu      192:        if ((q = realloc(p, n)) == NULL)
1.1       deraadt   193:                noroom();
1.3       tedu      194:        return (q);
1.1       deraadt   195: }
                    196:
1.3       tedu      197: static void
                    198: noroom(void)
1.1       deraadt   199: {
1.6     ! millert   200:        warn("files too big, try -h");
1.4       deraadt   201:        done(0);
1.6     ! millert   202: }
        !           203:
        !           204: __dead void
        !           205: usage(void)
        !           206: {
        !           207:        (void)fprintf(stderr, "usage: diff [-c | -C lines | -e | -f | -h | -n ] [-biwt] file1 file2\n"
        !           208:            "usage: diff [-Dstring] [-biw] file1 file2\n"
        !           209:            "usage: diff [-l] [-r] [-s] [-c | -C lines | -e | -f | -h | -n ] [-biwt]\n            [-Sname] dir1 dir2\n");
        !           210:
        !           211:        exit(1);
1.1       deraadt   212: }