[BACK]Return to diffreg.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / diff

Annotation of src/usr.bin/diff/diffreg.c, Revision 1.3

1.3     ! tedu        1: /*     $OpenBSD: diffreg.c,v 1.2 2003/06/25 01:23:38 deraadt 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 <sys/types.h>
        !            38:
        !            39: #include <stdlib.h>
        !            40: #include <unistd.h>
        !            41: #include <fcntl.h>
        !            42: #include <string.h>
1.1       deraadt    43:
                     44: #include "diff.h"
                     45: #include "pathnames.h"
1.3     ! tedu       46:
        !            47: #if 0
        !            48: static char const sccsid[] = "@(#)diffreg.c 4.21 4/6/90";
        !            49: #endif
        !            50:
1.1       deraadt    51: /*
                     52:  * diff - compare two files.
                     53:  */
                     54:
                     55: /*
                     56:  *     Uses an algorithm due to Harold Stone, which finds
                     57:  *     a pair of longest identical subsequences in the two
                     58:  *     files.
                     59:  *
                     60:  *     The major goal is to generate the match vector J.
                     61:  *     J[i] is the index of the line in file1 corresponding
                     62:  *     to line i file0. J[i] = 0 if there is no
                     63:  *     such line in file1.
                     64:  *
                     65:  *     Lines are hashed so as to work in core. All potential
                     66:  *     matches are located by sorting the lines of each file
                     67:  *     on the hash (called ``value''). In particular, this
                     68:  *     collects the equivalence classes in file1 together.
                     69:  *     Subroutine equiv replaces the value of each line in
1.3     ! tedu       70:  *     file0 by the index of the first element of its
1.1       deraadt    71:  *     matching equivalence in (the reordered) file1.
                     72:  *     To save space equiv squeezes file1 into a single
                     73:  *     array member in which the equivalence classes
                     74:  *     are simply concatenated, except that their first
                     75:  *     members are flagged by changing sign.
                     76:  *
                     77:  *     Next the indices that point into member are unsorted into
                     78:  *     array class according to the original order of file0.
                     79:  *
                     80:  *     The cleverness lies in routine stone. This marches
                     81:  *     through the lines of file0, developing a vector klist
                     82:  *     of "k-candidates". At step i a k-candidate is a matched
                     83:  *     pair of lines x,y (x in file0 y in file1) such that
                     84:  *     there is a common subsequence of length k
1.3     ! tedu       85:  *     between the first i lines of file0 and the first y
1.1       deraadt    86:  *     lines of file1, but there is no such subsequence for
                     87:  *     any smaller y. x is the earliest possible mate to y
                     88:  *     that occurs in such a subsequence.
                     89:  *
                     90:  *     Whenever any of the members of the equivalence class of
1.3     ! tedu       91:  *     lines in file1 matable to a line in file0 has serial number
        !            92:  *     less than the y of some k-candidate, that k-candidate
        !            93:  *     with the smallest such y is replaced. The new
1.1       deraadt    94:  *     k-candidate is chained (via pred) to the current
                     95:  *     k-1 candidate so that the actual subsequence can
                     96:  *     be recovered. When a member has serial number greater
                     97:  *     that the y of all k-candidates, the klist is extended.
                     98:  *     At the end, the longest subsequence is pulled out
                     99:  *     and placed in the array J by unravel
                    100:  *
                    101:  *     With J in hand, the matches there recorded are
                    102:  *     check'ed against reality to assure that no spurious
                    103:  *     matches have crept in due to hashing. If they have,
                    104:  *     they are broken, and "jackpot" is recorded--a harmless
                    105:  *     matter except that a true match for a spuriously
                    106:  *     mated line may now be unnecessarily reported as a change.
                    107:  *
                    108:  *     Much of the complexity of the program comes simply
                    109:  *     from trying to minimize core utilization and
                    110:  *     maximize the range of doable problems by dynamically
                    111:  *     allocating what is needed and reusing what is not.
                    112:  *     The core requirements for problems larger than somewhat
                    113:  *     are (in words) 2*length(file0) + length(file1) +
                    114:  *     3*(number of k-candidates installed),  typically about
1.3     ! tedu      115:  *     6n words for files of length n.
1.1       deraadt   116:  */
                    117:
                    118: #define        prints(s)       fputs(s,stdout)
                    119:
1.3     ! tedu      120: FILE *input[2];
        !           121: FILE *fopen();
1.1       deraadt   122:
                    123: struct cand {
1.3     ! tedu      124:        int x;
        !           125:        int y;
        !           126:        int pred;
1.1       deraadt   127: } cand;
1.3     ! tedu      128:
1.1       deraadt   129: struct line {
1.3     ! tedu      130:        int serial;
        !           131:        int value;
1.1       deraadt   132: } *file[2], line;
1.3     ! tedu      133:
        !           134: int len[2];
        !           135: struct line *sfile[2];         /* shortened by pruning common prefix and
        !           136:                                 * suffix */
        !           137: int slen[2];
        !           138: int pref, suff;                        /* length of prefix and suffix */
        !           139: int *class;                    /* will be overlaid on file[0] */
        !           140: int *member;                   /* will be overlaid on file[1] */
        !           141: int *klist;                    /* will be overlaid on file[0] after class */
        !           142: struct cand *clist;            /* merely a free storage pot for candidates */
        !           143: int clen = 0;
        !           144: int *J;                                /* will be overlaid on class */
        !           145: long *ixold;                   /* will be overlaid on klist */
        !           146: long *ixnew;                   /* will be overlaid on file[1] */
        !           147: char *chrtran;                 /* translation table for case-folding */
        !           148:
        !           149: static void fetch(long *, int, int, FILE *, char *, int);
        !           150: static void output(void);
        !           151: static void check(void);
        !           152: static void range(int, int, char *);
        !           153: static void dump_context_vec(void);
        !           154: static void prepare(int, FILE *);
        !           155: static void prune(void);
        !           156: static void equiv(struct line *, int, struct line *, int, int *);
        !           157: static void unravel(int);
        !           158: static void unsort(struct line *, int, int *);
        !           159: static void change(int, int, int, int);
        !           160: static void sort(struct line *, int);
        !           161: static int newcand(int, int, int);
        !           162: static int search(int *, int, int);
        !           163: static int skipline(int);
        !           164: static int asciifile(FILE *);
        !           165: static int stone(int *, int, int *, int *);
        !           166: static int readhash(FILE *);
        !           167:
        !           168: /*
        !           169:  * chrtran points to one of 2 translation tables: cup2low if folding upper to
        !           170:  * lower case clow2low if not folding case
1.1       deraadt   171:  */
1.3     ! tedu      172: char clow2low[256] = {
        !           173:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
        !           174:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
        !           175:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
        !           176:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
        !           177:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
        !           178:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
        !           179:        0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
        !           180:        0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
        !           181:        0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
        !           182:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
        !           183:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
        !           184:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
        !           185:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
        !           186:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
        !           187:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
        !           188:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
        !           189:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
        !           190:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
        !           191:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
        !           192:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
        !           193:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
        !           194:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
        !           195:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
        !           196:        0xfd, 0xfe, 0xff
1.1       deraadt   197: };
                    198:
1.3     ! tedu      199: char cup2low[256] = {
        !           200:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
        !           201:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
        !           202:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
        !           203:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
        !           204:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
        !           205:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
        !           206:        0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
        !           207:        0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
        !           208:        0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
        !           209:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
        !           210:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
        !           211:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
        !           212:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
        !           213:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
        !           214:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
        !           215:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
        !           216:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
        !           217:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
        !           218:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
        !           219:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
        !           220:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
        !           221:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
        !           222:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
        !           223:        0xfd, 0xfe, 0xff
1.1       deraadt   224: };
                    225:
1.3     ! tedu      226: void
        !           227: diffreg(void)
1.1       deraadt   228: {
1.3     ! tedu      229:        int i, j;
1.1       deraadt   230:        FILE *f1, *f2;
                    231:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    232:
                    233:        if (hflag) {
                    234:                diffargv[0] = "diffh";
                    235:                execv(diffh, diffargv);
                    236:                fprintf(stderr, "diff: ");
                    237:                perror(diffh);
                    238:                done();
                    239:        }
1.3     ! tedu      240:        chrtran = (iflag ? cup2low : clow2low);
1.1       deraadt   241:        if ((stb1.st_mode & S_IFMT) == S_IFDIR) {
                    242:                file1 = splice(file1, file2);
                    243:                if (stat(file1, &stb1) < 0) {
                    244:                        fprintf(stderr, "diff: ");
                    245:                        perror(file1);
                    246:                        done();
                    247:                }
                    248:        } else if ((stb2.st_mode & S_IFMT) == S_IFDIR) {
                    249:                file2 = splice(file2, file1);
                    250:                if (stat(file2, &stb2) < 0) {
                    251:                        fprintf(stderr, "diff: ");
                    252:                        perror(file2);
                    253:                        done();
                    254:                }
                    255:        } else if ((stb1.st_mode & S_IFMT) != S_IFREG || !strcmp(file1, "-")) {
                    256:                if (!strcmp(file2, "-")) {
                    257:                        fprintf(stderr, "diff: can't specify - -\n");
                    258:                        done();
                    259:                }
                    260:                file1 = copytemp();
                    261:                if (stat(file1, &stb1) < 0) {
                    262:                        fprintf(stderr, "diff: ");
                    263:                        perror(file1);
                    264:                        done();
                    265:                }
                    266:        } else if ((stb2.st_mode & S_IFMT) != S_IFREG || !strcmp(file2, "-")) {
                    267:                file2 = copytemp();
                    268:                if (stat(file2, &stb2) < 0) {
                    269:                        fprintf(stderr, "diff: ");
                    270:                        perror(file2);
                    271:                        done();
                    272:                }
                    273:        }
                    274:        if ((f1 = fopen(file1, "r")) == NULL) {
                    275:                fprintf(stderr, "diff: ");
                    276:                perror(file1);
                    277:                done();
                    278:        }
                    279:        if ((f2 = fopen(file2, "r")) == NULL) {
                    280:                fprintf(stderr, "diff: ");
                    281:                perror(file2);
                    282:                fclose(f1);
                    283:                done();
                    284:        }
                    285:        if (stb1.st_size != stb2.st_size)
                    286:                goto notsame;
                    287:        for (;;) {
                    288:                i = fread(buf1, 1, BUFSIZ, f1);
                    289:                j = fread(buf2, 1, BUFSIZ, f2);
                    290:                if (i < 0 || j < 0 || i != j)
                    291:                        goto notsame;
                    292:                if (i == 0 && j == 0) {
                    293:                        fclose(f1);
                    294:                        fclose(f2);
1.3     ! tedu      295:                        status = 0;     /* files don't differ */
1.1       deraadt   296:                        goto same;
                    297:                }
                    298:                for (j = 0; j < i; j++)
                    299:                        if (buf1[j] != buf2[j])
                    300:                                goto notsame;
                    301:        }
                    302: notsame:
                    303:        /*
                    304:         *      Files certainly differ at this point; set status accordingly
                    305:         */
                    306:        status = 1;
                    307:        if (!asciifile(f1) || !asciifile(f2)) {
                    308:                printf("Binary files %s and %s differ\n", file1, file2);
                    309:                fclose(f1);
                    310:                fclose(f2);
                    311:                done();
                    312:        }
                    313:        prepare(0, f1);
                    314:        prepare(1, f2);
                    315:        fclose(f1);
                    316:        fclose(f2);
                    317:        prune();
1.3     ! tedu      318:        sort(sfile[0], slen[0]);
        !           319:        sort(sfile[1], slen[1]);
1.1       deraadt   320:
                    321:        member = (int *)file[1];
                    322:        equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.3     ! tedu      323:        member = ralloc((char *) member, (slen[1] + 2) * sizeof(int));
1.1       deraadt   324:
1.3     ! tedu      325:        class = (int *) file[0];
1.1       deraadt   326:        unsort(sfile[0], slen[0], class);
1.3     ! tedu      327:        class = ralloc((char *) class, (slen[0] + 2) * sizeof(int));
1.1       deraadt   328:
1.3     ! tedu      329:        klist = talloc((slen[0] + 2) * sizeof(int));
        !           330:        clist = talloc(sizeof(cand));
1.1       deraadt   331:        i = stone(class, slen[0], member, klist);
1.3     ! tedu      332:        free(member);
        !           333:        free(class);
1.1       deraadt   334:
1.3     ! tedu      335:        J = talloc((len[0] + 2) * sizeof(int));
1.1       deraadt   336:        unravel(klist[i]);
1.3     ! tedu      337:        free(clist);
        !           338:        free(klist);
1.1       deraadt   339:
1.3     ! tedu      340:        ixold = talloc((len[0] + 2) * sizeof(long));
        !           341:        ixnew = talloc((len[1] + 2) * sizeof(long));
1.1       deraadt   342:        check();
                    343:        output();
                    344:        status = anychange;
                    345: same:
                    346:        if (opt == D_CONTEXT && anychange == 0)
                    347:                printf("No differences encountered\n");
                    348:        done();
                    349: }
                    350:
                    351: char *tempfile = _PATH_TMP;
                    352:
                    353: char *
1.3     ! tedu      354: copytemp(void)
1.1       deraadt   355: {
                    356:        char buf[BUFSIZ];
1.3     ! tedu      357:        int i, f;
1.1       deraadt   358:
1.3     ! tedu      359:        signal(SIGHUP, catchsig);
        !           360:        signal(SIGINT, catchsig);
        !           361:        signal(SIGPIPE, catchsig);
        !           362:        signal(SIGTERM, catchsig);
        !           363:        f = mkstemp(tempfile);
1.1       deraadt   364:        if (f < 0) {
                    365:                fprintf(stderr, "diff: ");
                    366:                perror(tempfile);
                    367:                done();
                    368:        }
1.3     ! tedu      369:        while ((i = read(0, buf, BUFSIZ)) > 0)
        !           370:                if (write(f, buf, i) != i) {
1.1       deraadt   371:                        fprintf(stderr, "diff: ");
                    372:                        perror(tempfile);
                    373:                        done();
                    374:                }
                    375:        close(f);
                    376:        return (tempfile);
                    377: }
                    378:
                    379: char *
1.3     ! tedu      380: splice(char *dir, char *file)
1.1       deraadt   381: {
                    382:        char *tail;
                    383:        char buf[BUFSIZ];
                    384:
                    385:        if (!strcmp(file, "-")) {
                    386:                fprintf(stderr, "diff: can't specify - with other arg directory\n");
                    387:                done();
                    388:        }
                    389:        tail = rindex(file, '/');
                    390:        if (tail == 0)
                    391:                tail = file;
                    392:        else
                    393:                tail++;
1.3     ! tedu      394:        snprintf(buf, sizeof buf, "%s/%s", dir, tail);
        !           395:        return (strdup(buf));
1.1       deraadt   396: }
                    397:
1.3     ! tedu      398: static void
        !           399: prepare(int i, FILE * fd)
1.1       deraadt   400: {
1.3     ! tedu      401:        struct line *p;
        !           402:        int j, h;
1.1       deraadt   403:
1.3     ! tedu      404:        fseek(fd, 0, 0);
        !           405:        p = talloc(3 * sizeof(line));
        !           406:        for (j = 0; (h = readhash(fd));) {
        !           407:                p = (struct line *) ralloc((char *) p, (++j + 3) * sizeof(line));
1.1       deraadt   408:                p[j].value = h;
                    409:        }
                    410:        len[i] = j;
                    411:        file[i] = p;
                    412: }
                    413:
1.3     ! tedu      414: static void
        !           415: prune(void)
1.1       deraadt   416: {
1.3     ! tedu      417:        int i, j;
        !           418:        for (pref = 0; pref < len[0] && pref < len[1] &&
        !           419:            file[0][pref + 1].value == file[1][pref + 1].value;
        !           420:            pref++);
        !           421:        for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
        !           422:            file[0][len[0] - suff].value == file[1][len[1] - suff].value;
        !           423:            suff++);
        !           424:        for (j = 0; j < 2; j++) {
        !           425:                sfile[j] = file[j] + pref;
        !           426:                slen[j] = len[j] - pref - suff;
        !           427:                for (i = 0; i <= slen[j]; i++)
1.1       deraadt   428:                        sfile[j][i].serial = i;
                    429:        }
                    430: }
                    431:
1.3     ! tedu      432: static void
        !           433: equiv(struct line * a, int n, struct line * b, int m, int *c)
1.1       deraadt   434: {
1.3     ! tedu      435:        int i, j;
1.1       deraadt   436:        i = j = 1;
1.3     ! tedu      437:        while (i <= n && j <= m) {
        !           438:                if (a[i].value < b[j].value)
1.1       deraadt   439:                        a[i++].value = 0;
1.3     ! tedu      440:                else if (a[i].value == b[j].value)
1.1       deraadt   441:                        a[i++].value = j;
                    442:                else
                    443:                        j++;
                    444:        }
1.3     ! tedu      445:        while (i <= n)
1.1       deraadt   446:                a[i++].value = 0;
1.3     ! tedu      447:        b[m + 1].value = 0;
1.1       deraadt   448:        j = 0;
1.3     ! tedu      449:        while (++j <= m) {
1.1       deraadt   450:                c[j] = -b[j].serial;
1.3     ! tedu      451:                while (b[j + 1].value == b[j].value) {
1.1       deraadt   452:                        j++;
                    453:                        c[j] = b[j].serial;
                    454:                }
                    455:        }
                    456:        c[j] = -1;
                    457: }
                    458:
1.3     ! tedu      459: static int
        !           460: stone(int *a, int n, int *b, int *c)
1.1       deraadt   461: {
1.3     ! tedu      462:        int i, k, y;
1.1       deraadt   463:        int j, l;
                    464:        int oldc, tc;
                    465:        int oldl;
                    466:        k = 0;
1.3     ! tedu      467:        c[0] = newcand(0, 0, 0);
        !           468:        for (i = 1; i <= n; i++) {
1.1       deraadt   469:                j = a[i];
1.3     ! tedu      470:                if (j == 0)
1.1       deraadt   471:                        continue;
                    472:                y = -b[j];
                    473:                oldl = 0;
                    474:                oldc = c[0];
                    475:                do {
1.3     ! tedu      476:                        if (y <= clist[oldc].y)
1.1       deraadt   477:                                continue;
                    478:                        l = search(c, k, y);
1.3     ! tedu      479:                        if (l != oldl + 1)
        !           480:                                oldc = c[l - 1];
        !           481:                        if (l <= k) {
        !           482:                                if (clist[c[l]].y <= y)
1.1       deraadt   483:                                        continue;
                    484:                                tc = c[l];
1.3     ! tedu      485:                                c[l] = newcand(i, y, oldc);
1.1       deraadt   486:                                oldc = tc;
                    487:                                oldl = l;
                    488:                        } else {
1.3     ! tedu      489:                                c[l] = newcand(i, y, oldc);
1.1       deraadt   490:                                k++;
                    491:                                break;
                    492:                        }
1.3     ! tedu      493:                } while ((y = b[++j]) > 0);
1.1       deraadt   494:        }
1.3     ! tedu      495:        return (k);
1.1       deraadt   496: }
                    497:
1.3     ! tedu      498: static int
        !           499: newcand(int x, int y, int pred)
1.1       deraadt   500: {
1.3     ! tedu      501:        struct cand *q;
        !           502:
        !           503:        clist = (struct cand *) ralloc((char *) clist, ++clen * sizeof(cand));
        !           504:        q = clist + clen - 1;
1.1       deraadt   505:        q->x = x;
                    506:        q->y = y;
                    507:        q->pred = pred;
1.3     ! tedu      508:        return (clen - 1);
1.1       deraadt   509: }
                    510:
1.3     ! tedu      511: static int
        !           512: search(int *c, int k, int y)
1.1       deraadt   513: {
1.3     ! tedu      514:        int i, j, l;
1.1       deraadt   515:        int t;
1.3     ! tedu      516:        if (clist[c[k]].y < y)  /* quick look for typical case */
        !           517:                return (k + 1);
1.1       deraadt   518:        i = 0;
1.3     ! tedu      519:        j = k + 1;
1.1       deraadt   520:        while (1) {
                    521:                l = i + j;
1.3     ! tedu      522:                if ((l >>= 1) <= i)
1.1       deraadt   523:                        break;
                    524:                t = clist[c[l]].y;
1.3     ! tedu      525:                if (t > y)
1.1       deraadt   526:                        j = l;
1.3     ! tedu      527:                else if (t < y)
1.1       deraadt   528:                        i = l;
                    529:                else
1.3     ! tedu      530:                        return (l);
1.1       deraadt   531:        }
1.3     ! tedu      532:        return (l + 1);
1.1       deraadt   533: }
                    534:
1.3     ! tedu      535: static void
        !           536: unravel(int p)
1.1       deraadt   537: {
1.3     ! tedu      538:        int i;
        !           539:        struct cand *q;
        !           540:        for (i = 0; i <= len[0]; i++)
        !           541:                J[i] = i <= pref ? i :
        !           542:                    i > len[0] - suff ? i + len[1] - len[0] :
        !           543:                    0;
        !           544:        for (q = clist + p; q->y != 0; q = clist + q->pred)
        !           545:                J[q->x + pref] = q->y + pref;
1.1       deraadt   546: }
                    547:
1.3     ! tedu      548: /*
        !           549:  * check does double duty: 1.  ferret out any fortuitous correspondences due
        !           550:  * to confounding by hashing (which result in "jackpot") 2.  collect random
        !           551:  * access indexes to the two files
        !           552:  */
1.1       deraadt   553:
1.3     ! tedu      554: static void
        !           555: check(void)
1.1       deraadt   556: {
1.3     ! tedu      557:        int i, j;
1.1       deraadt   558:        int jackpot;
                    559:        long ctold, ctnew;
1.3     ! tedu      560:        int c, d;
1.1       deraadt   561:
1.3     ! tedu      562:        if ((input[0] = fopen(file1, "r")) == NULL) {
1.1       deraadt   563:                perror(file1);
                    564:                done();
                    565:        }
1.3     ! tedu      566:        if ((input[1] = fopen(file2, "r")) == NULL) {
1.1       deraadt   567:                perror(file2);
                    568:                done();
                    569:        }
                    570:        j = 1;
                    571:        ixold[0] = ixnew[0] = 0;
                    572:        jackpot = 0;
                    573:        ctold = ctnew = 0;
1.3     ! tedu      574:        for (i = 1; i <= len[0]; i++) {
        !           575:                if (J[i] == 0) {
1.1       deraadt   576:                        ixold[i] = ctold += skipline(0);
                    577:                        continue;
                    578:                }
1.3     ! tedu      579:                while (j < J[i]) {
1.1       deraadt   580:                        ixnew[j] = ctnew += skipline(1);
                    581:                        j++;
                    582:                }
1.3     ! tedu      583:                if (bflag || wflag || iflag) {
        !           584:                        for (;;) {
1.1       deraadt   585:                                c = getc(input[0]);
                    586:                                d = getc(input[1]);
                    587:                                ctold++;
                    588:                                ctnew++;
1.3     ! tedu      589:                                if (bflag && isspace(c) && isspace(d)) {
1.1       deraadt   590:                                        do {
1.3     ! tedu      591:                                                if (c == '\n')
1.1       deraadt   592:                                                        break;
                    593:                                                ctold++;
1.3     ! tedu      594:                                        } while (isspace(c = getc(input[0])));
1.1       deraadt   595:                                        do {
1.3     ! tedu      596:                                                if (d == '\n')
1.1       deraadt   597:                                                        break;
                    598:                                                ctnew++;
1.3     ! tedu      599:                                        } while (isspace(d = getc(input[1])));
        !           600:                                } else if (wflag) {
        !           601:                                        while (isspace(c) && c != '\n') {
        !           602:                                                c = getc(input[0]);
1.1       deraadt   603:                                                ctold++;
                    604:                                        }
1.3     ! tedu      605:                                        while (isspace(d) && d != '\n') {
        !           606:                                                d = getc(input[1]);
1.1       deraadt   607:                                                ctnew++;
                    608:                                        }
                    609:                                }
1.3     ! tedu      610:                                if (chrtran[c] != chrtran[d]) {
1.1       deraadt   611:                                        jackpot++;
                    612:                                        J[i] = 0;
1.3     ! tedu      613:                                        if (c != '\n')
1.1       deraadt   614:                                                ctold += skipline(0);
1.3     ! tedu      615:                                        if (d != '\n')
1.1       deraadt   616:                                                ctnew += skipline(1);
                    617:                                        break;
                    618:                                }
1.3     ! tedu      619:                                if (c == '\n')
1.1       deraadt   620:                                        break;
                    621:                        }
                    622:                } else {
1.3     ! tedu      623:                        for (;;) {
1.1       deraadt   624:                                ctold++;
                    625:                                ctnew++;
1.3     ! tedu      626:                                if ((c = getc(input[0])) != (d = getc(input[1]))) {
1.1       deraadt   627:                                        /* jackpot++; */
                    628:                                        J[i] = 0;
1.3     ! tedu      629:                                        if (c != '\n')
1.1       deraadt   630:                                                ctold += skipline(0);
1.3     ! tedu      631:                                        if (d != '\n')
1.1       deraadt   632:                                                ctnew += skipline(1);
                    633:                                        break;
                    634:                                }
1.3     ! tedu      635:                                if (c == '\n')
1.1       deraadt   636:                                        break;
                    637:                        }
                    638:                }
                    639:                ixold[i] = ctold;
                    640:                ixnew[j] = ctnew;
                    641:                j++;
                    642:        }
1.3     ! tedu      643:        for (; j <= len[1]; j++) {
1.1       deraadt   644:                ixnew[j] = ctnew += skipline(1);
                    645:        }
                    646:        fclose(input[0]);
                    647:        fclose(input[1]);
1.3     ! tedu      648:        /*
        !           649:                if(jackpot)
        !           650:                        fprintf(stderr, "jackpot\n");
        !           651:        */
1.1       deraadt   652: }
                    653:
1.3     ! tedu      654: static void
        !           655: sort(struct line * a, int n)
        !           656: {                              /* shellsort CACM #201 */
1.1       deraadt   657:        struct line w;
1.3     ! tedu      658:        int j, m = 0;           /* gcc */
1.1       deraadt   659:        struct line *ai;
1.3     ! tedu      660:        struct line *aim;
1.1       deraadt   661:        int k;
                    662:
                    663:        if (n == 0)
                    664:                return;
1.3     ! tedu      665:        for (j = 1; j <= n; j *= 2)
        !           666:                m = 2 * j - 1;
        !           667:        for (m /= 2; m != 0; m /= 2) {
        !           668:                k = n - m;
        !           669:                for (j = 1; j <= k; j++) {
        !           670:                        for (ai = &a[j]; ai > a; ai -= m) {
1.1       deraadt   671:                                aim = &ai[m];
1.3     ! tedu      672:                                if (aim < ai)
        !           673:                                        break;  /* wraparound */
        !           674:                                if (aim->value > ai[0].value ||
        !           675:                                    (aim->value == ai[0].value &&
        !           676:                                        aim->serial > ai[0].serial))
1.1       deraadt   677:                                        break;
                    678:                                w.value = ai[0].value;
                    679:                                ai[0].value = aim->value;
                    680:                                aim->value = w.value;
                    681:                                w.serial = ai[0].serial;
                    682:                                ai[0].serial = aim->serial;
                    683:                                aim->serial = w.serial;
                    684:                        }
                    685:                }
                    686:        }
                    687: }
                    688:
1.3     ! tedu      689: static void
        !           690: unsort(struct line * f, int l, int *b)
1.1       deraadt   691: {
1.3     ! tedu      692:        int *a;
        !           693:        int i;
        !           694:
        !           695:        a = talloc((l + 1) * sizeof(int));
        !           696:        for (i = 1; i <= l; i++)
1.1       deraadt   697:                a[f[i].serial] = f[i].value;
1.3     ! tedu      698:        for (i = 1; i <= l; i++)
1.1       deraadt   699:                b[i] = a[i];
1.3     ! tedu      700:        free(a);
1.1       deraadt   701: }
                    702:
1.3     ! tedu      703: static int
        !           704: skipline(int f)
1.1       deraadt   705: {
1.3     ! tedu      706:        int i, c;
1.1       deraadt   707:
1.3     ! tedu      708:        for (i = 1; (c = getc(input[f])) != '\n'; i++)
1.1       deraadt   709:                if (c < 0)
1.3     ! tedu      710:                        return (i);
        !           711:        return (i);
1.1       deraadt   712: }
                    713:
1.3     ! tedu      714: static void
        !           715: output(void)
1.1       deraadt   716: {
                    717:        int m;
1.3     ! tedu      718:        int i0, i1, j1;
1.1       deraadt   719:        int j0;
1.3     ! tedu      720:        input[0] = fopen(file1, "r");
        !           721:        input[1] = fopen(file2, "r");
1.1       deraadt   722:        m = len[0];
                    723:        J[0] = 0;
1.3     ! tedu      724:        J[m + 1] = len[1] + 1;
        !           725:        if (opt != D_EDIT) {
        !           726:                for (i0 = 1; i0 <= m; i0 = i1 + 1) {
        !           727:                        while (i0 <= m && J[i0] == J[i0 - 1] + 1)
        !           728:                                i0++;
        !           729:                        j0 = J[i0 - 1] + 1;
        !           730:                        i1 = i0 - 1;
        !           731:                        while (i1 < m && J[i1 + 1] == 0)
        !           732:                                i1++;
        !           733:                        j1 = J[i1 + 1] - 1;
        !           734:                        J[i1] = j1;
        !           735:                        change(i0, i1, j0, j1);
        !           736:                }
        !           737:        } else {
        !           738:                for (i0 = m; i0 >= 1; i0 = i1 - 1) {
        !           739:                        while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
        !           740:                                i0--;
        !           741:                        j0 = J[i0 + 1] - 1;
        !           742:                        i1 = i0 + 1;
        !           743:                        while (i1 > 1 && J[i1 - 1] == 0)
        !           744:                                i1--;
        !           745:                        j1 = J[i1 - 1] + 1;
        !           746:                        J[i1] = j1;
        !           747:                        change(i1, i0, j1, j0);
        !           748:                }
        !           749:        }
        !           750:        if (m == 0)
        !           751:                change(1, 0, 1, len[1]);
        !           752:        if (opt == D_IFDEF) {
1.1       deraadt   753:                for (;;) {
                    754: #define        c i0
                    755:                        c = getc(input[0]);
                    756:                        if (c < 0)
                    757:                                return;
                    758:                        putchar(c);
                    759:                }
                    760: #undef c
                    761:        }
                    762:        if (anychange && opt == D_CONTEXT)
                    763:                dump_context_vec();
                    764: }
                    765:
                    766: /*
                    767:  * The following struct is used to record change information when
                    768:  * doing a "context" diff.  (see routine "change" to understand the
                    769:  * highly mneumonic field names)
                    770:  */
                    771: struct context_vec {
1.3     ! tedu      772:        int a;                  /* start line in old file */
        !           773:        int b;                  /* end line in old file */
        !           774:        int c;                  /* start line in new file */
        !           775:        int d;                  /* end line in new file */
1.1       deraadt   776: };
                    777:
1.3     ! tedu      778: struct context_vec *context_vec_start, *context_vec_end, *context_vec_ptr;
1.1       deraadt   779:
                    780: #define        MAX_CONTEXT     128
                    781:
1.3     ! tedu      782: /*
        !           783:  * indicate that there is a difference between lines a and b of the from file
        !           784:  * to get to lines c to d of the to file. If a is greater then b then there
        !           785:  * are no lines in the from file involved and this means that there were
        !           786:  * lines appended (beginning at b). If c is greater than d then there are
        !           787:  * lines missing from the to file.
        !           788:  */
        !           789: static void
        !           790: change(int a, int b, int c, int d)
1.1       deraadt   791: {
                    792:        struct stat stbuf;
                    793:
1.3     ! tedu      794:        if (opt != D_IFDEF && a > b && c > d)
1.1       deraadt   795:                return;
                    796:        if (anychange == 0) {
                    797:                anychange = 1;
1.3     ! tedu      798:                if (opt == D_CONTEXT) {
1.1       deraadt   799:                        printf("*** %s  ", file1);
                    800:                        stat(file1, &stbuf);
                    801:                        printf("%s--- %s        ",
                    802:                            ctime(&stbuf.st_mtime), file2);
                    803:                        stat(file2, &stbuf);
                    804:                        printf("%s", ctime(&stbuf.st_mtime));
                    805:
1.3     ! tedu      806:                        context_vec_start = talloc(MAX_CONTEXT *
        !           807:                            sizeof(struct context_vec));
1.1       deraadt   808:                        context_vec_end = context_vec_start + MAX_CONTEXT;
                    809:                        context_vec_ptr = context_vec_start - 1;
                    810:                }
                    811:        }
1.3     ! tedu      812:        if (opt == D_CONTEXT) {
1.1       deraadt   813:                /*
                    814:                 * if this new change is within 'context' lines of
                    815:                 * the previous change, just add it to the change
                    816:                 * record.  If the record is full or if this
                    817:                 * change is more than 'context' lines from the previous
                    818:                 * change, dump the record, reset it & add the new change.
                    819:                 */
1.3     ! tedu      820:                if (context_vec_ptr >= context_vec_end ||
        !           821:                    (context_vec_ptr >= context_vec_start &&
        !           822:                        a > (context_vec_ptr->b + 2 * context) &&
        !           823:                        c > (context_vec_ptr->d + 2 * context)))
1.1       deraadt   824:                        dump_context_vec();
                    825:
                    826:                context_vec_ptr++;
                    827:                context_vec_ptr->a = a;
                    828:                context_vec_ptr->b = b;
                    829:                context_vec_ptr->c = c;
                    830:                context_vec_ptr->d = d;
                    831:                return;
                    832:        }
                    833:        switch (opt) {
                    834:
                    835:        case D_NORMAL:
                    836:        case D_EDIT:
1.3     ! tedu      837:                range(a, b, ",");
        !           838:                putchar(a > b ? 'a' : c > d ? 'd' : 'c');
        !           839:                if (opt == D_NORMAL)
        !           840:                        range(c, d, ",");
1.1       deraadt   841:                putchar('\n');
                    842:                break;
                    843:        case D_REVERSE:
1.3     ! tedu      844:                putchar(a > b ? 'a' : c > d ? 'd' : 'c');
        !           845:                range(a, b, " ");
1.1       deraadt   846:                putchar('\n');
                    847:                break;
1.3     ! tedu      848:        case D_NREVERSE:
        !           849:                if (a > b)
        !           850:                        printf("a%d %d\n", b, d - c + 1);
        !           851:                else {
        !           852:                        printf("d%d %d\n", a, b - a + 1);
        !           853:                        if (!(c > d))
        !           854:                                /* add changed lines */
        !           855:                                printf("a%d %d\n", b, d - c + 1);
        !           856:                }
        !           857:                break;
        !           858:        }
        !           859:        if (opt == D_NORMAL || opt == D_IFDEF) {
        !           860:                fetch(ixold, a, b, input[0], "< ", 1);
        !           861:                if (a <= b && c <= d && opt == D_NORMAL)
1.1       deraadt   862:                        prints("---\n");
                    863:        }
1.3     ! tedu      864:        fetch(ixnew, c, d, input[1], opt == D_NORMAL ? "> " : "", 0);
        !           865:        if ((opt == D_EDIT || opt == D_REVERSE) && c <= d)
1.1       deraadt   866:                prints(".\n");
                    867:        if (inifdef) {
                    868:                fprintf(stdout, "#endif /* %s */\n", endifname);
                    869:                inifdef = 0;
                    870:        }
                    871: }
                    872:
1.3     ! tedu      873: static void
        !           874: range(int a, int b, char *separator)
1.1       deraadt   875: {
1.3     ! tedu      876:        printf("%d", a > b ? b : a);
        !           877:        if (a < b) {
1.1       deraadt   878:                printf("%s%d", separator, b);
                    879:        }
                    880: }
                    881:
1.3     ! tedu      882: static void
        !           883: fetch(long *f, int a, int b, FILE * lb, char *s, int oldfile)
1.1       deraadt   884: {
1.3     ! tedu      885:        int i, j;
        !           886:        int c;
        !           887:        int col;
        !           888:        int nc;
        !           889:        int oneflag = (*ifdef1 != '\0') != (*ifdef2 != '\0');
1.1       deraadt   890:
                    891:        /*
                    892:         * When doing #ifdef's, copy down to current line
                    893:         * if this is the first file, so that stuff makes it to output.
                    894:         */
1.3     ! tedu      895:        if (opt == D_IFDEF && oldfile) {
1.1       deraadt   896:                long curpos = ftell(lb);
                    897:                /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1.3     ! tedu      898:                nc = f[a > b ? b : a - 1] - curpos;
1.1       deraadt   899:                for (i = 0; i < nc; i++)
                    900:                        putchar(getc(lb));
                    901:        }
                    902:        if (a > b)
                    903:                return;
                    904:        if (opt == D_IFDEF) {
                    905:                if (inifdef)
1.3     ! tedu      906:                        fprintf(stdout, "#else /* %s%s */\n", oneflag && oldfile == 1 ? "!" : "", ifdef2);
1.1       deraadt   907:                else {
                    908:                        if (oneflag) {
                    909:                                /* There was only one ifdef given */
                    910:                                endifname = ifdef2;
                    911:                                if (oldfile)
                    912:                                        fprintf(stdout, "#ifndef %s\n", endifname);
                    913:                                else
                    914:                                        fprintf(stdout, "#ifdef %s\n", endifname);
1.3     ! tedu      915:                        } else {
1.1       deraadt   916:                                endifname = oldfile ? ifdef1 : ifdef2;
                    917:                                fprintf(stdout, "#ifdef %s\n", endifname);
                    918:                        }
                    919:                }
1.3     ! tedu      920:                inifdef = 1 + oldfile;
1.1       deraadt   921:        }
1.3     ! tedu      922:        for (i = a; i <= b; i++) {
        !           923:                fseek(lb, f[i - 1], 0);
        !           924:                nc = f[i] - f[i - 1];
1.1       deraadt   925:                if (opt != D_IFDEF)
                    926:                        prints(s);
                    927:                col = 0;
1.3     ! tedu      928:                for (j = 0; j < nc; j++) {
1.1       deraadt   929:                        c = getc(lb);
                    930:                        if (c == '\t' && tflag)
                    931:                                do
                    932:                                        putchar(' ');
                    933:                                while (++col & 7);
                    934:                        else {
                    935:                                putchar(c);
                    936:                                col++;
                    937:                        }
                    938:                }
                    939:        }
                    940:
                    941:        if (inifdef && !wantelses) {
                    942:                fprintf(stdout, "#endif /* %s */\n", endifname);
                    943:                inifdef = 0;
                    944:        }
                    945: }
                    946:
                    947: #define POW2                   /* define only if HALFLONG is 2**n */
                    948: #define HALFLONG 16
                    949: #define low(x) (x&((1L<<HALFLONG)-1))
                    950: #define high(x)        (x>>HALFLONG)
                    951:
                    952: /*
                    953:  * hashing has the effect of
                    954:  * arranging line in 7-bit bytes and then
1.3     ! tedu      955:  * summing 1-s complement in 16-bit hunks
1.1       deraadt   956:  */
1.3     ! tedu      957: static int
        !           958: readhash(FILE * f)
1.1       deraadt   959: {
1.3     ! tedu      960:        long sum;
        !           961:        unsigned shift;
        !           962:        int t;
        !           963:        int space;
1.1       deraadt   964:
                    965:        sum = 1;
                    966:        space = 0;
1.3     ! tedu      967:        if (!bflag && !wflag) {
        !           968:                if (iflag)
        !           969:                        for (shift = 0; (t = getc(f)) != '\n'; shift += 7) {
        !           970:                                if (t == -1)
        !           971:                                        return (0);
1.1       deraadt   972:                                sum += (long)chrtran[t] << (shift
                    973: #ifdef POW2
                    974:                                    &= HALFLONG - 1);
                    975: #else
                    976:                                    %= HALFLONG);
                    977: #endif
                    978:                        }
                    979:                else
1.3     ! tedu      980:                        for (shift = 0; (t = getc(f)) != '\n'; shift += 7) {
        !           981:                                if (t == -1)
        !           982:                                        return (0);
1.1       deraadt   983:                                sum += (long)t << (shift
                    984: #ifdef POW2
                    985:                                    &= HALFLONG - 1);
                    986: #else
                    987:                                    %= HALFLONG);
                    988: #endif
                    989:                        }
                    990:        } else {
1.3     ! tedu      991:                for (shift = 0;;) {
        !           992:                        switch (t = getc(f)) {
1.1       deraadt   993:                        case -1:
1.3     ! tedu      994:                                return (0);
1.1       deraadt   995:                        case '\t':
                    996:                        case ' ':
                    997:                                space++;
                    998:                                continue;
                    999:                        default:
1.3     ! tedu     1000:                                if (space && !wflag) {
1.1       deraadt  1001:                                        shift += 7;
                   1002:                                        space = 0;
                   1003:                                }
                   1004:                                sum += (long)chrtran[t] << (shift
                   1005: #ifdef POW2
                   1006:                                    &= HALFLONG - 1);
                   1007: #else
                   1008:                                    %= HALFLONG);
                   1009: #endif
                   1010:                                shift += 7;
                   1011:                                continue;
                   1012:                        case '\n':
                   1013:                                break;
                   1014:                        }
                   1015:                        break;
                   1016:                }
                   1017:        }
                   1018:        sum = low(sum) + high(sum);
1.3     ! tedu     1019:        return ((short) low(sum) + (short) high(sum));
1.1       deraadt  1020: }
                   1021:
1.3     ! tedu     1022: static int
        !          1023: asciifile(FILE * f)
1.1       deraadt  1024: {
                   1025:        char buf[BUFSIZ];
1.3     ! tedu     1026:        int cnt;
        !          1027:        char *cp;
1.1       deraadt  1028:
1.3     ! tedu     1029:        fseek(f, 0, 0);
1.1       deraadt  1030:        cnt = fread(buf, 1, BUFSIZ, f);
                   1031:        cp = buf;
                   1032:        while (--cnt >= 0)
                   1033:                if (*cp++ & 0200)
                   1034:                        return (0);
                   1035:        return (1);
                   1036: }
                   1037:
                   1038:
                   1039: /* dump accumulated "context" diff changes */
1.3     ! tedu     1040: static void
        !          1041: dump_context_vec(void)
1.1       deraadt  1042: {
1.3     ! tedu     1043:        int a, b, c, d;
        !          1044:        char ch;
        !          1045:        struct context_vec *cvp = context_vec_start;
        !          1046:        int lowa, upb, lowc, upd;
        !          1047:        int do_output;
1.1       deraadt  1048:
1.3     ! tedu     1049:        if (cvp > context_vec_ptr)
1.1       deraadt  1050:                return;
                   1051:
1.3     ! tedu     1052:        b = d = 0;              /* gcc */
1.1       deraadt  1053:        lowa = max(1, cvp->a - context);
1.3     ! tedu     1054:        upb = min(len[0], context_vec_ptr->b + context);
1.1       deraadt  1055:        lowc = max(1, cvp->c - context);
1.3     ! tedu     1056:        upd = min(len[1], context_vec_ptr->d + context);
1.1       deraadt  1057:
                   1058:        printf("***************\n*** ");
1.3     ! tedu     1059:        range(lowa, upb, ",");
1.1       deraadt  1060:        printf(" ****\n");
                   1061:
                   1062:        /*
                   1063:         * output changes to the "old" file.  The first loop suppresses
                   1064:         * output if there were no changes to the "old" file (we'll see
                   1065:         * the "old" lines as context in the "new" list).
                   1066:         */
                   1067:        do_output = 0;
1.3     ! tedu     1068:        for (; cvp <= context_vec_ptr; cvp++)
1.1       deraadt  1069:                if (cvp->a <= cvp->b) {
                   1070:                        cvp = context_vec_start;
                   1071:                        do_output++;
                   1072:                        break;
                   1073:                }
1.3     ! tedu     1074:        if (do_output) {
1.1       deraadt  1075:                while (cvp <= context_vec_ptr) {
1.3     ! tedu     1076:                        a = cvp->a;
        !          1077:                        b = cvp->b;
        !          1078:                        c = cvp->c;
        !          1079:                        d = cvp->d;
1.1       deraadt  1080:
                   1081:                        if (a <= b && c <= d)
                   1082:                                ch = 'c';
                   1083:                        else
                   1084:                                ch = (a <= b) ? 'd' : 'a';
                   1085:
                   1086:                        if (ch == 'a')
1.3     ! tedu     1087:                                fetch(ixold, lowa, b, input[0], "  ", 0);
1.1       deraadt  1088:                        else {
1.3     ! tedu     1089:                                fetch(ixold, lowa, a - 1, input[0], "  ", 0);
        !          1090:                                fetch(ixold, a, b, input[0], ch == 'c' ? "! " : "- ", 0);
1.1       deraadt  1091:                        }
                   1092:                        lowa = b + 1;
                   1093:                        cvp++;
                   1094:                }
1.3     ! tedu     1095:                fetch(ixold, b + 1, upb, input[0], "  ", 0);
1.1       deraadt  1096:        }
                   1097:        /* output changes to the "new" file */
                   1098:        printf("--- ");
1.3     ! tedu     1099:        range(lowc, upd, ",");
1.1       deraadt  1100:        printf(" ----\n");
                   1101:
                   1102:        do_output = 0;
                   1103:        for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
                   1104:                if (cvp->c <= cvp->d) {
                   1105:                        cvp = context_vec_start;
                   1106:                        do_output++;
                   1107:                        break;
                   1108:                }
                   1109:        if (do_output) {
                   1110:                while (cvp <= context_vec_ptr) {
1.3     ! tedu     1111:                        a = cvp->a;
        !          1112:                        b = cvp->b;
        !          1113:                        c = cvp->c;
        !          1114:                        d = cvp->d;
1.1       deraadt  1115:
                   1116:                        if (a <= b && c <= d)
                   1117:                                ch = 'c';
                   1118:                        else
                   1119:                                ch = (a <= b) ? 'd' : 'a';
                   1120:
                   1121:                        if (ch == 'd')
1.3     ! tedu     1122:                                fetch(ixnew, lowc, d, input[1], "  ", 0);
1.1       deraadt  1123:                        else {
1.3     ! tedu     1124:                                fetch(ixnew, lowc, c - 1, input[1], "  ", 0);
        !          1125:                                fetch(ixnew, c, d, input[1], ch == 'c' ? "! " : "+ ", 0);
1.1       deraadt  1126:                        }
                   1127:                        lowc = d + 1;
                   1128:                        cvp++;
                   1129:                }
1.3     ! tedu     1130:                fetch(ixnew, d + 1, upd, input[1], "  ", 0);
1.1       deraadt  1131:        }
                   1132:        context_vec_ptr = context_vec_start - 1;
                   1133: }