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

Annotation of src/usr.bin/cvs/diff.c, Revision 1.2

1.1       jfb         1: /*     $OpenBSD$       */
                      2: /*
                      3:  * Copyright (C) Caldera International Inc.  2001-2002.
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code and documentation must retain the above
                     10:  *    copyright notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  * 3. All advertising materials mentioning features or use of this software
                     15:  *    must display the following acknowledgement:
                     16:  *     This product includes software developed or owned by Caldera
                     17:  *     International, Inc.
                     18:  * 4. Neither the name of Caldera International, Inc. nor the names of other
                     19:  *    contributors may be used to endorse or promote products derived from
                     20:  *    this software without specific prior written permission.
                     21:  *
                     22:  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
                     23:  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
                     27:  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     28:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     29:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     31:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
                     32:  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     33:  * POSSIBILITY OF SUCH DAMAGE.
                     34:  */
                     35: /*-
                     36:  * Copyright (c) 1991, 1993
                     37:  *     The Regents of the University of California.  All rights reserved.
                     38:  * Copyright (c) 2004 Jean-Francois Brousseau.  All rights reserved.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
                     48:  * 3. Neither the name of the University nor the names of its contributors
                     49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  *
                     64:  *     @(#)diffreg.c   8.1 (Berkeley) 6/6/93
                     65:  */
                     66: /*
                     67:  *     Uses an algorithm due to Harold Stone, which finds
                     68:  *     a pair of longest identical subsequences in the two
                     69:  *     files.
                     70:  *
                     71:  *     The major goal is to generate the match vector J.
                     72:  *     J[i] is the index of the line in file1 corresponding
                     73:  *     to line i file0. J[i] = 0 if there is no
                     74:  *     such line in file1.
                     75:  *
                     76:  *     Lines are hashed so as to work in core. All potential
                     77:  *     matches are located by sorting the lines of each file
                     78:  *     on the hash (called ``value''). In particular, this
                     79:  *     collects the equivalence classes in file1 together.
                     80:  *     Subroutine equiv replaces the value of each line in
                     81:  *     file0 by the index of the first element of its
                     82:  *     matching equivalence in (the reordered) file1.
                     83:  *     To save space equiv squeezes file1 into a single
                     84:  *     array member in which the equivalence classes
                     85:  *     are simply concatenated, except that their first
                     86:  *     members are flagged by changing sign.
                     87:  *
                     88:  *     Next the indices that point into member are unsorted into
                     89:  *     array class according to the original order of file0.
                     90:  *
                     91:  *     The cleverness lies in routine stone. This marches
                     92:  *     through the lines of file0, developing a vector klist
                     93:  *     of "k-candidates". At step i a k-candidate is a matched
                     94:  *     pair of lines x,y (x in file0 y in file1) such that
                     95:  *     there is a common subsequence of length k
                     96:  *     between the first i lines of file0 and the first y
                     97:  *     lines of file1, but there is no such subsequence for
                     98:  *     any smaller y. x is the earliest possible mate to y
                     99:  *     that occurs in such a subsequence.
                    100:  *
                    101:  *     Whenever any of the members of the equivalence class of
                    102:  *     lines in file1 matable to a line in file0 has serial number
                    103:  *     less than the y of some k-candidate, that k-candidate
                    104:  *     with the smallest such y is replaced. The new
                    105:  *     k-candidate is chained (via pred) to the current
                    106:  *     k-1 candidate so that the actual subsequence can
                    107:  *     be recovered. When a member has serial number greater
                    108:  *     that the y of all k-candidates, the klist is extended.
                    109:  *     At the end, the longest subsequence is pulled out
                    110:  *     and placed in the array J by unravel
                    111:  *
                    112:  *     With J in hand, the matches there recorded are
                    113:  *     check'ed against reality to assure that no spurious
                    114:  *     matches have crept in due to hashing. If they have,
                    115:  *     they are broken, and "jackpot" is recorded--a harmless
                    116:  *     matter except that a true match for a spuriously
                    117:  *     mated line may now be unnecessarily reported as a change.
                    118:  *
                    119:  *     Much of the complexity of the program comes simply
                    120:  *     from trying to minimize core utilization and
                    121:  *     maximize the range of doable problems by dynamically
                    122:  *     allocating what is needed and reusing what is not.
                    123:  *     The core requirements for problems larger than somewhat
                    124:  *     are (in words) 2*length(file0) + length(file1) +
                    125:  *     3*(number of k-candidates installed),  typically about
                    126:  *     6n words for files of length n.
                    127:  */
                    128:
                    129: #include <sys/param.h>
                    130: #include <sys/stat.h>
                    131: #include <sys/wait.h>
                    132:
                    133: #include <errno.h>
                    134: #include <ctype.h>
                    135: #include <stdio.h>
                    136: #include <fcntl.h>
                    137: #include <paths.h>
                    138: #include <regex.h>
                    139: #include <dirent.h>
                    140: #include <stdlib.h>
                    141: #include <stddef.h>
                    142: #include <unistd.h>
                    143: #include <string.h>
                    144: #include <sysexits.h>
                    145:
                    146: #include "cvs.h"
                    147: #include "log.h"
                    148: #include "buf.h"
                    149:
                    150:
                    151: #define CVS_DIFF_DEFCTX    3   /* default context length */
                    152:
                    153:
                    154: /*
                    155:  * Output format options
                    156:  */
                    157: #define        D_NORMAL        0       /* Normal output */
                    158: #define        D_CONTEXT       1       /* Diff with context */
                    159: #define        D_UNIFIED       2       /* Unified context diff */
                    160: #define        D_IFDEF         3       /* Diff with merged #ifdef's */
                    161: #define        D_BRIEF         4       /* Say if the files differ */
                    162:
                    163: /*
                    164:  * Status values for print_status() and diffreg() return values
                    165:  */
                    166: #define        D_SAME          0       /* Files are the same */
                    167: #define        D_DIFFER        1       /* Files are different */
                    168: #define        D_BINARY        2       /* Binary files are different */
                    169: #define        D_COMMON        3       /* Subdirectory common to both dirs */
                    170: #define        D_ONLY          4       /* Only exists in one directory */
                    171: #define        D_MISMATCH1     5       /* path1 was a dir, path2 a file */
                    172: #define        D_MISMATCH2     6       /* path1 was a file, path2 a dir */
                    173: #define        D_ERROR         7       /* An error occurred */
                    174: #define        D_SKIPPED1      8       /* path1 was a special file */
                    175: #define        D_SKIPPED2      9       /* path2 was a special file */
                    176:
                    177: struct cand {
                    178:        int x;
                    179:        int y;
                    180:        int pred;
                    181: } cand;
                    182:
                    183: struct line {
                    184:        int serial;
                    185:        int value;
                    186: } *file[2];
                    187:
                    188: /*
                    189:  * The following struct is used to record change information when
                    190:  * doing a "context" or "unified" diff.  (see routine "change" to
                    191:  * understand the highly mnemonic field names)
                    192:  */
                    193: struct context_vec {
                    194:        int a;                  /* start line in old file */
                    195:        int b;                  /* end line in old file */
                    196:        int c;                  /* start line in new file */
                    197:        int d;                  /* end line in new file */
                    198: };
                    199:
                    200:
                    201: struct excludes {
                    202:        char *pattern;
                    203:        struct excludes *next;
                    204: };
                    205:
                    206:
                    207: char   *splice(char *, char *);
                    208: int  cvs_diffreg(const char *, const char *);
                    209: int  cvs_diff_file  (const char *, const char *, const char *);
                    210: int  cvs_diff_dir   (const char *, int);
                    211: static void output(const char *, FILE *, const char *, FILE *);
                    212: static void check(FILE *, FILE *);
                    213: static void range(int, int, char *);
                    214: static void uni_range(int, int);
                    215: static void dump_context_vec(FILE *, FILE *);
                    216: static void dump_unified_vec(FILE *, FILE *);
                    217: static void prepare(int, FILE *, off_t);
                    218: static void prune(void);
                    219: static void equiv(struct line *, int, struct line *, int, int *);
                    220: static void unravel(int);
                    221: static void unsort(struct line *, int, int *);
                    222: static void change(const char *, FILE *, const char *, FILE *, int, int, int, int);
                    223: static void sort(struct line *, int);
                    224: static int  ignoreline(char *);
                    225: static int  asciifile(FILE *);
                    226: static int  fetch(long *, int, int, FILE *, int, int);
                    227: static int  newcand(int, int, int);
                    228: static int  search(int *, int, int);
                    229: static int  skipline(FILE *);
                    230: static int  isqrt(int);
                    231: static int  stone(int *, int, int *, int *);
                    232: static int  readhash(FILE *);
                    233: static int  files_differ(FILE *, FILE *);
                    234: static __inline int min(int, int);
                    235: static __inline int max(int, int);
                    236: static char *match_function(const long *, int, FILE *);
                    237: static char *preadline(int, size_t, off_t);
                    238:
                    239:
                    240:
                    241: extern int cvs_client;
                    242: extern struct cvsroot *cvs_root;
                    243:
                    244:
                    245:
                    246: static int aflag, bflag, dflag, iflag, tflag, Tflag, wflag;
                    247: static int context, status;
                    248: static int format = D_NORMAL;
                    249: static struct stat stb1, stb2;
1.2     ! jfb       250: static char *ifdefname, *ignore_pats, diffargs[128];
        !           251: static const char *diff_file;
1.1       jfb       252: regex_t ignore_re;
                    253:
                    254: static int  *J;                        /* will be overlaid on class */
                    255: static int  *class;            /* will be overlaid on file[0] */
                    256: static int  *klist;            /* will be overlaid on file[0] after class */
                    257: static int  *member;           /* will be overlaid on file[1] */
                    258: static int   clen;
                    259: static int   inifdef;          /* whether or not we are in a #ifdef block */
                    260: static int   len[2];
                    261: static int   pref, suff;       /* length of prefix and suffix */
                    262: static int   slen[2];
                    263: static int   anychange;
                    264: static long *ixnew;            /* will be overlaid on file[1] */
                    265: static long *ixold;            /* will be overlaid on klist */
                    266: static struct cand *clist;     /* merely a free storage pot for candidates */
                    267: static int   clistlen;         /* the length of clist */
                    268: static struct line *sfile[2];  /* shortened by pruning common prefix/suffix */
                    269: static u_char *chrtran;                /* translation table for case-folding */
                    270: static struct context_vec *context_vec_start;
                    271: static struct context_vec *context_vec_end;
                    272: static struct context_vec *context_vec_ptr;
                    273:
                    274: #define FUNCTION_CONTEXT_SIZE  41
                    275: static char lastbuf[FUNCTION_CONTEXT_SIZE];
                    276: static int lastline;
                    277: static int lastmatchline;
                    278:
                    279:
                    280:
                    281:
                    282: /*
                    283:  * chrtran points to one of 2 translation tables: cup2low if folding upper to
                    284:  * lower case clow2low if not folding case
                    285:  */
                    286: u_char clow2low[256] = {
                    287:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    288:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    289:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    290:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    291:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    292:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
                    293:        0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                    294:        0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
                    295:        0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
                    296:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    297:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    298:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    299:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    300:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    301:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    302:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    303:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    304:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    305:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    306:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    307:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    308:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    309:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    310:        0xfd, 0xfe, 0xff
                    311: };
                    312:
                    313: u_char cup2low[256] = {
                    314:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    315:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    316:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    317:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    318:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    319:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
                    320:        0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
                    321:        0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
                    322:        0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
                    323:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    324:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    325:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    326:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    327:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    328:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    329:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    330:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    331:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    332:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    333:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    334:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    335:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    336:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    337:        0xfd, 0xfe, 0xff
                    338: };
                    339:
                    340:
                    341:
                    342: /*
                    343:  * cvs_diff()
                    344:  *
                    345:  * Handler for the `cvs diff' command.
                    346:  *
                    347:  * SYNOPSIS: cvs [args] diff [-clipu] [-D date] [-r rev]
                    348:  */
                    349:
                    350: int
                    351: cvs_diff(int argc, char **argv)
                    352: {
                    353:        int i, ch, recurse;
                    354:        size_t dalen;
                    355:        char dir[MAXPATHLEN], file[MAXPATHLEN], *d1, *d2, *r1, *r2;
                    356:
                    357:        context = CVS_DIFF_DEFCTX;
1.2     ! jfb       358:        strlcpy(diffargs, argv[0], sizeof(diffargs));
1.1       jfb       359:
                    360:        d1 = d2 = NULL;
                    361:        r1 = r2 = NULL;
                    362:        recurse = 1;
                    363:
                    364:        while ((ch = getopt(argc, argv, "cD:lir:u")) != -1) {
                    365:                switch (ch) {
                    366:                case 'c':
1.2     ! jfb       367:                        strlcat(diffargs, " -c", sizeof(diffargs));
1.1       jfb       368:                        format = D_CONTEXT;
                    369:                        break;
                    370:                case 'D':
                    371:                        if (d1 == NULL && r1 == NULL)
                    372:                                d1 = optarg;
                    373:                        else if (d2 == NULL && r2 == NULL)
                    374:                                d2 = optarg;
                    375:                        else {
                    376:                                cvs_log(LP_ERR,
                    377:                                    "no more than two revisions/dates can "
                    378:                                    "be specified");
                    379:                        }
                    380:                        break;
                    381:                case 'l':
1.2     ! jfb       382:                        strlcat(diffargs, " -l", sizeof(diffargs));
1.1       jfb       383:                        recurse = 0;
                    384:                        break;
                    385:                case 'i':
1.2     ! jfb       386:                        strlcat(diffargs, " -i", sizeof(diffargs));
1.1       jfb       387:                        iflag = 1;
                    388:                        break;
                    389:                case 'r':
                    390:                        if ((r1 == NULL) && (d1 == NULL))
                    391:                                r1 = optarg;
                    392:                        else if ((r2 == NULL) && (d2 == NULL))
                    393:                                r2 = optarg;
                    394:                        else {
                    395:                                cvs_log(LP_ERR,
                    396:                                    "no more than two revisions/dates can "
                    397:                                    "be specified");
                    398:                                return (EX_USAGE);
                    399:                        }
                    400:                        break;
                    401:                case 'u':
1.2     ! jfb       402:                        strlcat(diffargs, " -u", sizeof(diffargs));
1.1       jfb       403:                        format = D_UNIFIED;
                    404:                        break;
                    405:                default:
                    406:                        return (EX_USAGE);
                    407:                }
                    408:        }
                    409:
                    410:        argc -= optind;
                    411:        argv += optind;
                    412:
                    413:        if (argc == 0) {
                    414:                /* get the CVSROOT from current dir */
                    415:                strlcpy(dir, ".", sizeof(dir));
                    416:
                    417:                cvs_root = cvsroot_get(dir);
                    418:                if (cvs_root == NULL)
                    419:                        return (EX_USAGE);
                    420:
                    421:                if (cvs_root->cr_method != CVS_METHOD_LOCAL) {
                    422:                        cvs_client_connect();
                    423:
                    424:                        /* send the flags */
                    425:                        if (format == D_CONTEXT)
                    426:                                cvs_client_sendarg("-c", 0);
                    427:                        else if (format == D_UNIFIED)
                    428:                                cvs_client_sendarg("-u", 0);
                    429:
                    430:                        if (r1 != NULL) {
                    431:                                cvs_client_sendarg("-r", 0);
                    432:                                cvs_client_sendarg(r1, 1);
                    433:                        }
                    434:                        else if (d1 != NULL) {
                    435:                                cvs_client_sendarg("-D", 0);
                    436:                                cvs_client_sendarg(d1, 1);
                    437:                        }
                    438:                        if (r2 != NULL) {
                    439:                                cvs_client_sendarg("-r", 0);
                    440:                                cvs_client_sendarg(r2, 1);
                    441:                        }
                    442:                        else if (d2 != NULL) {
                    443:                                cvs_client_sendarg("-D", 0);
                    444:                                cvs_client_sendarg(d2, 1);
                    445:                        }
                    446:                }
                    447:
                    448:                cvs_diff_dir(dir, recurse);
                    449:        }
                    450:        else {
                    451:                for (i = 0; i < argc; i++) {
                    452:                        cvs_splitpath(argv[i], dir, sizeof(dir),
                    453:                            file, sizeof(file));
                    454:                        cvs_root = cvsroot_get(dir);
                    455:                        if (cvs_root == NULL)
                    456:                                return (EX_USAGE);
                    457:
                    458:                        if (cvs_root->cr_method != CVS_METHOD_LOCAL) {
                    459:                                cvs_client_connect();
                    460:
                    461:                                if (i == 0) {
                    462:                                        /* send the flags */
                    463:                                        if (format == D_CONTEXT)
                    464:                                                cvs_client_sendarg("-c", 0);
                    465:                                        else if (format == D_UNIFIED)
                    466:                                                cvs_client_sendarg("-u", 0);
                    467:                                }
                    468:                        }
                    469:
                    470:                        cvs_diff_file(argv[i], r1, r2);
                    471:                }
                    472:        }
                    473:
                    474:        if (cvs_root->cr_method != CVS_METHOD_LOCAL)
                    475:                cvs_client_sendreq(CVS_REQ_DIFF, NULL, 1);
                    476:
                    477:        return (0);
                    478: }
                    479:
                    480:
                    481: /*
                    482:  * cvs_diff_file()
                    483:  *
                    484:  * Diff a single file.
                    485:  */
                    486:
                    487: int
                    488: cvs_diff_file(const char *path, const char *rev1, const char *rev2)
                    489: {
                    490:        int modif;
                    491:        char dir[MAXPATHLEN], file[MAXPATHLEN], rcspath[MAXPATHLEN];
                    492:        char repo[MAXPATHLEN], buf[64];
                    493:        time_t tsec;
                    494:        BUF *b1, *b2;
                    495:        RCSNUM *r1, *r2;
                    496:        RCSFILE *rf;
                    497:        CVSENTRIES *entf;
                    498:        struct tm tmstamp;
                    499:        struct stat fst;
                    500:        struct cvs_ent *entp;
                    501:
                    502:        rf = NULL;
                    503:        diff_file = path;
                    504:
                    505:        if (stat(path, &fst) == -1) {
                    506:                cvs_log(LP_ERRNO, "cannot find %s", path);
                    507:                return (-1);
                    508:        }
                    509:
                    510:        cvs_splitpath(path, dir, sizeof(dir), file, sizeof(file));
                    511:        cvs_readrepo(dir, repo, sizeof(repo));
                    512:
                    513:        entf = cvs_ent_open(dir);
                    514:        if (entf == NULL) {
                    515:                cvs_log(LP_ERR, "no CVS/Entries file in `%s'", dir);
                    516:                return (-1);
                    517:        }
                    518:
                    519:        entp = cvs_ent_get(entf, file);
                    520:        if ((entp == NULL) && (cvs_root->cr_method == CVS_METHOD_LOCAL)) {
                    521:                cvs_log(LP_WARN, "I know nothing about %s", path);
                    522:                return (-1);
                    523:        }
                    524:
                    525:        if (cvs_root->cr_method != CVS_METHOD_LOCAL) {
                    526:                if (cvs_client_senddir(dir) < 0)
                    527:                        return (-1);
                    528:        }
                    529:
                    530:        tsec = (time_t)fst.st_mtimespec.tv_sec;
                    531:
                    532:        if ((gmtime_r(&tsec, &tmstamp) == NULL) ||
                    533:            (asctime_r(&tmstamp, buf) == NULL)) {
                    534:                cvs_log(LP_ERR, "failed to generate file timestamp");
                    535:                return (-1);
                    536:        }
                    537:        modif = (strcmp(buf, entp->ce_timestamp) == 0) ? 0 : 1;
                    538:
                    539:        if (cvs_root->cr_method != CVS_METHOD_LOCAL)
                    540:                cvs_client_sendentry(entp);
                    541:
                    542:        if (!modif) {
                    543:                if (cvs_root->cr_method != CVS_METHOD_LOCAL)
                    544:                        cvs_client_sendreq(CVS_REQ_UNCHANGED, file, 0);
                    545:                cvs_ent_close(entf);
                    546:                return (0);
                    547:        }
                    548:
                    549:        /* at this point, the file is modified */
                    550:        if (cvs_root->cr_method != CVS_METHOD_LOCAL) {
                    551:                cvs_client_sendreq(CVS_REQ_MODIFIED, file, 0);
                    552:                cvs_sendfile(path);
                    553:        }
                    554:        else {
                    555:                snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
                    556:                    cvs_root->cr_dir, repo, path, RCS_FILE_EXT);
                    557:
                    558:                rf = rcs_open(rcspath, RCS_MODE_READ);
                    559:                if (rf == NULL)
                    560:                        return (-1);
                    561:
                    562:                printf("Index: %s\n%s\nRCS file: %s\n", path,
                    563:                    RCS_DIFF_DIV, rcspath);
                    564:
                    565:                if (rev1 == NULL)
                    566:                        r1 = entp->ce_rev;
                    567:                else {
                    568:                        r1 = rcsnum_alloc();
                    569:                        rcsnum_aton(rev1, NULL, r1);
                    570:                }
                    571:
                    572:                printf("retrieving revision %s\n",
                    573:                    rcsnum_tostr(r1, buf, sizeof(buf)));
                    574:                b1 = rcs_getrev(rf, r1);
                    575:
                    576:                if (rev2 != NULL) {
                    577:                        printf("retrieving revision %s\n", rev2);
                    578:                        r2 = rcsnum_alloc();
                    579:                        rcsnum_aton(rev2, NULL, r2);
                    580:                        b2 = rcs_getrev(rf, r2);
                    581:                }
                    582:                else {
                    583:                        b2 = cvs_buf_load(path, BUF_AUTOEXT);
                    584:                }
                    585:
1.2     ! jfb       586:                printf("%s", diffargs);
        !           587:                printf(" -r%s", buf);
        !           588:                if (rev2 != NULL)
        !           589:                        printf(" -r%s", rev2);
        !           590:                printf(" %s\n", path);
1.1       jfb       591:                cvs_buf_write(b1, "/tmp/diff1", 0600);
                    592:                cvs_buf_write(b2, "/tmp/diff2", 0600);
                    593:                cvs_diffreg("/tmp/diff1", "/tmp/diff2");
                    594:        }
                    595:
                    596:        cvs_ent_close(entf);
                    597:
                    598:        return (0);
                    599: }
                    600:
                    601:
                    602: /*
                    603:  * cvs_diff_dir()
                    604:  *
                    605:  */
                    606:
                    607: int
                    608: cvs_diff_dir(const char *dir, int recurse)
                    609: {
                    610:        char path[MAXPATHLEN];
                    611:        DIR *dirp;
                    612:        CVSENTRIES *entf;
                    613:        struct dirent *dentp;
                    614:        struct cvs_ent *entp;
                    615:
                    616:        printf("cvs_diff_dir(%s)\n", dir);
                    617:
                    618:        dirp = opendir(dir);
                    619:        if (dirp == NULL) {
                    620:                cvs_log(LP_ERRNO, "failed to open directory `%s'", dir);
                    621:                return (-1);
                    622:        }
                    623:
                    624:        entf = cvs_ent_open(dir);
                    625:        if (entf == NULL) {
                    626:                cvs_log(LP_ERR, "no CVS/Entries file in `%s'", dir);
                    627:                (void)closedir(dirp);
                    628:                return (-1);
                    629:        }
                    630:
                    631:        while ((dentp = readdir(dirp)) != NULL) {
                    632:                if ((strcmp(dentp->d_name, "CVS") == 0) ||
                    633:                    (dentp->d_name[0] == '.'))
                    634:                        continue;
                    635:
                    636:                if (strcmp(dir, ".") != 0) {
                    637:                        strlcpy(path, dir, sizeof(path));
                    638:                        strlcat(path, "/", sizeof(path));
                    639:                }
                    640:                else
                    641:                        path[0] = '\0';
                    642:                strlcat(path, dentp->d_name, sizeof(path));
                    643:                if (dentp->d_type == DT_DIR) {
                    644:                        if (!recurse)
                    645:                                continue;
                    646:                        cvs_diff_dir(path, recurse);
                    647:                }
                    648:                else {
                    649:                        entp = cvs_ent_get(entf, dentp->d_name);
                    650:                        if (entp == NULL) {
                    651:                                cvs_client_sendreq(CVS_REQ_QUESTIONABLE, path,
                    652:                                    0);
                    653:                        }
                    654:                        else {
                    655: #if 0
                    656:                                cvs_diff_file(path);
                    657: #endif
                    658:                        }
                    659:                }
                    660:        }
                    661:
                    662:        return (0);
                    663: }
                    664:
                    665:
                    666:
                    667:
                    668: int
                    669: cvs_diffreg(const char *file1, const char *file2)
                    670: {
                    671:        FILE *f1, *f2;
                    672:        int i, rval;
                    673:
                    674:        f1 = f2 = NULL;
                    675:        rval = D_SAME;
                    676:        anychange = 0;
                    677:        lastline = 0;
                    678:        lastmatchline = 0;
                    679:        context_vec_ptr = context_vec_start - 1;
                    680:        chrtran = (iflag ? cup2low : clow2low);
                    681:
                    682:        f1 = fopen(file1, "r");
                    683:        if (f1 == NULL) {
                    684:                cvs_log(LP_ERRNO, "%s", file1);
                    685:                status |= 2;
                    686:                goto closem;
                    687:        }
                    688:
                    689:        f2 = fopen(file2, "r");
                    690:        if (f2 == NULL) {
                    691:                cvs_log(LP_ERRNO, "%s", file2);
                    692:                status |= 2;
                    693:                goto closem;
                    694:        }
                    695:
                    696:        switch (files_differ(f1, f2)) {
                    697:        case 0:
                    698:                goto closem;
                    699:        case 1:
                    700:                break;
                    701:        default:
                    702:                /* error */
                    703:                status |= 2;
                    704:                goto closem;
                    705:        }
                    706:
                    707:        if (!asciifile(f1) || !asciifile(f2)) {
                    708:                rval = D_BINARY;
                    709:                status |= 1;
                    710:                goto closem;
                    711:        }
                    712:        prepare(0, f1, stb1.st_size);
                    713:        prepare(1, f2, stb2.st_size);
                    714:        prune();
                    715:        sort(sfile[0], slen[0]);
                    716:        sort(sfile[1], slen[1]);
                    717:
                    718:        member = (int *)file[1];
                    719:        equiv(sfile[0], slen[0], sfile[1], slen[1], member);
                    720:        member = realloc(member, (slen[1] + 2) * sizeof(int));
                    721:
                    722:        class = (int *)file[0];
                    723:        unsort(sfile[0], slen[0], class);
                    724:        class = realloc(class, (slen[0] + 2) * sizeof(int));
                    725:
                    726:        klist = malloc((slen[0] + 2) * sizeof(int));
                    727:        clen = 0;
                    728:        clistlen = 100;
                    729:        clist = malloc(clistlen * sizeof(cand));
                    730:        i = stone(class, slen[0], member, klist);
                    731:        free(member);
                    732:        free(class);
                    733:
                    734:        J = realloc(J, (len[0] + 2) * sizeof(int));
                    735:        unravel(klist[i]);
                    736:        free(clist);
                    737:        free(klist);
                    738:
                    739:        ixold = realloc(ixold, (len[0] + 2) * sizeof(long));
                    740:        ixnew = realloc(ixnew, (len[1] + 2) * sizeof(long));
                    741:        check(f1, f2);
                    742:        output(file1, f1, file2, f2);
                    743:
                    744: closem:
                    745:        if (anychange) {
                    746:                status |= 1;
                    747:                if (rval == D_SAME)
                    748:                        rval = D_DIFFER;
                    749:        }
                    750:        if (f1 != NULL)
                    751:                fclose(f1);
                    752:        if (f2 != NULL)
                    753:                fclose(f2);
                    754:        return (rval);
                    755: }
                    756:
                    757: /*
                    758:  * Check to see if the given files differ.
                    759:  * Returns 0 if they are the same, 1 if different, and -1 on error.
                    760:  * XXX - could use code from cmp(1) [faster]
                    761:  */
                    762: static int
                    763: files_differ(FILE *f1, FILE *f2)
                    764: {
                    765:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    766:        size_t i, j;
                    767:
                    768:        if (stb1.st_size != stb2.st_size)
                    769:                return (1);
                    770:        for (;;) {
                    771:                i = fread(buf1, 1, sizeof(buf1), f1);
                    772:                j = fread(buf2, 1, sizeof(buf2), f2);
                    773:                if (i != j)
                    774:                        return (1);
                    775:                if (i == 0 && j == 0) {
                    776:                        if (ferror(f1) || ferror(f2))
                    777:                                return (1);
                    778:                        return (0);
                    779:                }
                    780:                if (memcmp(buf1, buf2, i) != 0)
                    781:                        return (1);
                    782:        }
                    783: }
                    784:
                    785:
                    786: char *
                    787: splice(char *dir, char *file)
                    788: {
                    789:        char *tail, *buf;
                    790:
                    791:        if ((tail = strrchr(file, '/')) == NULL)
                    792:                tail = file;
                    793:        else
                    794:                tail++;
                    795:        asprintf(&buf, "%s/%s", dir, tail);
                    796:        return (buf);
                    797: }
                    798:
                    799: static void
                    800: prepare(int i, FILE *fd, off_t filesize)
                    801: {
                    802:        struct line *p;
                    803:        int j, h;
                    804:        size_t sz;
                    805:
                    806:        rewind(fd);
                    807:
                    808:        sz = (filesize <= SIZE_MAX ? filesize : SIZE_MAX) / 25;
                    809:        if (sz < 100)
                    810:                sz = 100;
                    811:
                    812:        p = malloc((sz + 3) * sizeof(struct line));
                    813:        for (j = 0; (h = readhash(fd));) {
                    814:                if (j == (int)sz) {
                    815:                        sz = sz * 3 / 2;
                    816:                        p = realloc(p, (sz + 3) * sizeof(struct line));
                    817:                }
                    818:                p[++j].value = h;
                    819:        }
                    820:        len[i] = j;
                    821:        file[i] = p;
                    822: }
                    823:
                    824: static void
                    825: prune(void)
                    826: {
                    827:        int i, j;
                    828:
                    829:        for (pref = 0; pref < len[0] && pref < len[1] &&
                    830:            file[0][pref + 1].value == file[1][pref + 1].value;
                    831:            pref++)
                    832:                ;
                    833:        for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
                    834:            file[0][len[0] - suff].value == file[1][len[1] - suff].value;
                    835:            suff++)
                    836:                ;
                    837:        for (j = 0; j < 2; j++) {
                    838:                sfile[j] = file[j] + pref;
                    839:                slen[j] = len[j] - pref - suff;
                    840:                for (i = 0; i <= slen[j]; i++)
                    841:                        sfile[j][i].serial = i;
                    842:        }
                    843: }
                    844:
                    845: static void
                    846: equiv(struct line *a, int n, struct line *b, int m, int *c)
                    847: {
                    848:        int i, j;
                    849:
                    850:        i = j = 1;
                    851:        while (i <= n && j <= m) {
                    852:                if (a[i].value < b[j].value)
                    853:                        a[i++].value = 0;
                    854:                else if (a[i].value == b[j].value)
                    855:                        a[i++].value = j;
                    856:                else
                    857:                        j++;
                    858:        }
                    859:        while (i <= n)
                    860:                a[i++].value = 0;
                    861:        b[m + 1].value = 0;
                    862:        j = 0;
                    863:        while (++j <= m) {
                    864:                c[j] = -b[j].serial;
                    865:                while (b[j + 1].value == b[j].value) {
                    866:                        j++;
                    867:                        c[j] = b[j].serial;
                    868:                }
                    869:        }
                    870:        c[j] = -1;
                    871: }
                    872:
                    873: /* Code taken from ping.c */
                    874: static int
                    875: isqrt(int n)
                    876: {
                    877:        int y, x = 1;
                    878:
                    879:        if (n == 0)
                    880:                return(0);
                    881:
                    882:        do { /* newton was a stinker */
                    883:                y = x;
                    884:                x = n / x;
                    885:                x += y;
                    886:                x /= 2;
                    887:        } while ((x - y) > 1 || (x - y) < -1);
                    888:
                    889:        return (x);
                    890: }
                    891:
                    892: static int
                    893: stone(int *a, int n, int *b, int *c)
                    894: {
                    895:        int i, k, y, j, l;
                    896:        int oldc, tc, oldl;
                    897:        u_int numtries;
                    898:
                    899:        const u_int bound = dflag ? UINT_MAX : max(256, isqrt(n));
                    900:
                    901:        k = 0;
                    902:        c[0] = newcand(0, 0, 0);
                    903:        for (i = 1; i <= n; i++) {
                    904:                j = a[i];
                    905:                if (j == 0)
                    906:                        continue;
                    907:                y = -b[j];
                    908:                oldl = 0;
                    909:                oldc = c[0];
                    910:                numtries = 0;
                    911:                do {
                    912:                        if (y <= clist[oldc].y)
                    913:                                continue;
                    914:                        l = search(c, k, y);
                    915:                        if (l != oldl + 1)
                    916:                                oldc = c[l - 1];
                    917:                        if (l <= k) {
                    918:                                if (clist[c[l]].y <= y)
                    919:                                        continue;
                    920:                                tc = c[l];
                    921:                                c[l] = newcand(i, y, oldc);
                    922:                                oldc = tc;
                    923:                                oldl = l;
                    924:                                numtries++;
                    925:                        } else {
                    926:                                c[l] = newcand(i, y, oldc);
                    927:                                k++;
                    928:                                break;
                    929:                        }
                    930:                } while ((y = b[++j]) > 0 && numtries < bound);
                    931:        }
                    932:        return (k);
                    933: }
                    934:
                    935: static int
                    936: newcand(int x, int y, int pred)
                    937: {
                    938:        struct cand *q;
                    939:
                    940:        if (clen == clistlen) {
                    941:                clistlen = clistlen * 11 / 10;
                    942:                clist = realloc(clist, clistlen * sizeof(cand));
                    943:        }
                    944:        q = clist + clen;
                    945:        q->x = x;
                    946:        q->y = y;
                    947:        q->pred = pred;
                    948:        return (clen++);
                    949: }
                    950:
                    951: static int
                    952: search(int *c, int k, int y)
                    953: {
                    954:        int i, j, l, t;
                    955:
                    956:        if (clist[c[k]].y < y)  /* quick look for typical case */
                    957:                return (k + 1);
                    958:        i = 0;
                    959:        j = k + 1;
                    960:        while (1) {
                    961:                l = i + j;
                    962:                if ((l >>= 1) <= i)
                    963:                        break;
                    964:                t = clist[c[l]].y;
                    965:                if (t > y)
                    966:                        j = l;
                    967:                else if (t < y)
                    968:                        i = l;
                    969:                else
                    970:                        return (l);
                    971:        }
                    972:        return (l + 1);
                    973: }
                    974:
                    975: static void
                    976: unravel(int p)
                    977: {
                    978:        struct cand *q;
                    979:        int i;
                    980:
                    981:        for (i = 0; i <= len[0]; i++)
                    982:                J[i] = i <= pref ? i :
                    983:                    i > len[0] - suff ? i + len[1] - len[0] : 0;
                    984:        for (q = clist + p; q->y != 0; q = clist + q->pred)
                    985:                J[q->x + pref] = q->y + pref;
                    986: }
                    987:
                    988: /*
                    989:  * Check does double duty:
                    990:  *  1. ferret out any fortuitous correspondences due
                    991:  *     to confounding by hashing (which result in "jackpot")
                    992:  *  2.  collect random access indexes to the two files
                    993:  */
                    994: static void
                    995: check(FILE *f1, FILE *f2)
                    996: {
                    997:        int i, j, jackpot, c, d;
                    998:        long ctold, ctnew;
                    999:
                   1000:        rewind(f1);
                   1001:        rewind(f2);
                   1002:        j = 1;
                   1003:        ixold[0] = ixnew[0] = 0;
                   1004:        jackpot = 0;
                   1005:        ctold = ctnew = 0;
                   1006:        for (i = 1; i <= len[0]; i++) {
                   1007:                if (J[i] == 0) {
                   1008:                        ixold[i] = ctold += skipline(f1);
                   1009:                        continue;
                   1010:                }
                   1011:                while (j < J[i]) {
                   1012:                        ixnew[j] = ctnew += skipline(f2);
                   1013:                        j++;
                   1014:                }
                   1015:                if (bflag || wflag || iflag) {
                   1016:                        for (;;) {
                   1017:                                c = getc(f1);
                   1018:                                d = getc(f2);
                   1019:                                /*
                   1020:                                 * GNU diff ignores a missing newline
                   1021:                                 * in one file if bflag || wflag.
                   1022:                                 */
                   1023:                                if ((bflag || wflag) &&
                   1024:                                    ((c == EOF && d == '\n') ||
                   1025:                                    (c == '\n' && d == EOF))) {
                   1026:                                        break;
                   1027:                                }
                   1028:                                ctold++;
                   1029:                                ctnew++;
                   1030:                                if (bflag && isspace(c) && isspace(d)) {
                   1031:                                        do {
                   1032:                                                if (c == '\n')
                   1033:                                                        break;
                   1034:                                                ctold++;
                   1035:                                        } while (isspace(c = getc(f1)));
                   1036:                                        do {
                   1037:                                                if (d == '\n')
                   1038:                                                        break;
                   1039:                                                ctnew++;
                   1040:                                        } while (isspace(d = getc(f2)));
                   1041:                                } else if (wflag) {
                   1042:                                        while (isspace(c) && c != '\n') {
                   1043:                                                c = getc(f1);
                   1044:                                                ctold++;
                   1045:                                        }
                   1046:                                        while (isspace(d) && d != '\n') {
                   1047:                                                d = getc(f2);
                   1048:                                                ctnew++;
                   1049:                                        }
                   1050:                                }
                   1051:                                if (chrtran[c] != chrtran[d]) {
                   1052:                                        jackpot++;
                   1053:                                        J[i] = 0;
                   1054:                                        if (c != '\n' && c != EOF)
                   1055:                                                ctold += skipline(f1);
                   1056:                                        if (d != '\n' && c != EOF)
                   1057:                                                ctnew += skipline(f2);
                   1058:                                        break;
                   1059:                                }
                   1060:                                if (c == '\n' || c == EOF)
                   1061:                                        break;
                   1062:                        }
                   1063:                } else {
                   1064:                        for (;;) {
                   1065:                                ctold++;
                   1066:                                ctnew++;
                   1067:                                if ((c = getc(f1)) != (d = getc(f2))) {
                   1068:                                        /* jackpot++; */
                   1069:                                        J[i] = 0;
                   1070:                                        if (c != '\n' && c != EOF)
                   1071:                                                ctold += skipline(f1);
                   1072:                                        if (d != '\n' && c != EOF)
                   1073:                                                ctnew += skipline(f2);
                   1074:                                        break;
                   1075:                                }
                   1076:                                if (c == '\n' || c == EOF)
                   1077:                                        break;
                   1078:                        }
                   1079:                }
                   1080:                ixold[i] = ctold;
                   1081:                ixnew[j] = ctnew;
                   1082:                j++;
                   1083:        }
                   1084:        for (; j <= len[1]; j++)
                   1085:                ixnew[j] = ctnew += skipline(f2);
                   1086:        /*
                   1087:         * if (jackpot)
                   1088:         *      fprintf(stderr, "jackpot\n");
                   1089:         */
                   1090: }
                   1091:
                   1092: /* shellsort CACM #201 */
                   1093: static void
                   1094: sort(struct line *a, int n)
                   1095: {
                   1096:        struct line *ai, *aim, w;
                   1097:        int j, m = 0, k;
                   1098:
                   1099:        if (n == 0)
                   1100:                return;
                   1101:        for (j = 1; j <= n; j *= 2)
                   1102:                m = 2 * j - 1;
                   1103:        for (m /= 2; m != 0; m /= 2) {
                   1104:                k = n - m;
                   1105:                for (j = 1; j <= k; j++) {
                   1106:                        for (ai = &a[j]; ai > a; ai -= m) {
                   1107:                                aim = &ai[m];
                   1108:                                if (aim < ai)
                   1109:                                        break;  /* wraparound */
                   1110:                                if (aim->value > ai[0].value ||
                   1111:                                    (aim->value == ai[0].value &&
                   1112:                                        aim->serial > ai[0].serial))
                   1113:                                        break;
                   1114:                                w.value = ai[0].value;
                   1115:                                ai[0].value = aim->value;
                   1116:                                aim->value = w.value;
                   1117:                                w.serial = ai[0].serial;
                   1118:                                ai[0].serial = aim->serial;
                   1119:                                aim->serial = w.serial;
                   1120:                        }
                   1121:                }
                   1122:        }
                   1123: }
                   1124:
                   1125: static void
                   1126: unsort(struct line *f, int l, int *b)
                   1127: {
                   1128:        int *a, i;
                   1129:
                   1130:        a = malloc((l + 1) * sizeof(int));
                   1131:        for (i = 1; i <= l; i++)
                   1132:                a[f[i].serial] = f[i].value;
                   1133:        for (i = 1; i <= l; i++)
                   1134:                b[i] = a[i];
                   1135:        free(a);
                   1136: }
                   1137:
                   1138: static int
                   1139: skipline(FILE *f)
                   1140: {
                   1141:        int i, c;
                   1142:
                   1143:        for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
                   1144:                continue;
                   1145:        return (i);
                   1146: }
                   1147:
                   1148: static void
                   1149: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
                   1150: {
                   1151:        int m, i0, i1, j0, j1;
                   1152:
                   1153:        rewind(f1);
                   1154:        rewind(f2);
                   1155:        m = len[0];
                   1156:        J[0] = 0;
                   1157:        J[m + 1] = len[1] + 1;
                   1158:        for (i0 = 1; i0 <= m; i0 = i1 + 1) {
                   1159:                while (i0 <= m && J[i0] == J[i0 - 1] + 1)
                   1160:                        i0++;
                   1161:                j0 = J[i0 - 1] + 1;
                   1162:                i1 = i0 - 1;
                   1163:                while (i1 < m && J[i1 + 1] == 0)
                   1164:                        i1++;
                   1165:                j1 = J[i1 + 1] - 1;
                   1166:                J[i1] = j1;
                   1167:                change(file1, f1, file2, f2, i0, i1, j0, j1);
                   1168:        }
                   1169:        if (m == 0)
                   1170:                change(file1, f1, file2, f2, 1, 0, 1, len[1]);
                   1171:        if (format == D_IFDEF) {
                   1172:                for (;;) {
                   1173: #define        c i0
                   1174:                        if ((c = getc(f1)) == EOF)
                   1175:                                return;
                   1176:                        putchar(c);
                   1177:                }
                   1178: #undef c
                   1179:        }
                   1180:        if (anychange != 0) {
                   1181:                if (format == D_CONTEXT)
                   1182:                        dump_context_vec(f1, f2);
                   1183:                else if (format == D_UNIFIED)
                   1184:                        dump_unified_vec(f1, f2);
                   1185:        }
                   1186: }
                   1187:
                   1188: static __inline void
                   1189: range(int a, int b, char *separator)
                   1190: {
                   1191:        printf("%d", a > b ? b : a);
                   1192:        if (a < b)
                   1193:                printf("%s%d", separator, b);
                   1194: }
                   1195:
                   1196: static __inline void
                   1197: uni_range(int a, int b)
                   1198: {
                   1199:        if (a < b)
                   1200:                printf("%d,%d", a, b - a + 1);
                   1201:        else if (a == b)
                   1202:                printf("%d", b);
                   1203:        else
                   1204:                printf("%d,0", b);
                   1205: }
                   1206:
                   1207: static char *
                   1208: preadline(int fd, size_t len, off_t off)
                   1209: {
                   1210:        char *line;
                   1211:        ssize_t nr;
                   1212:
                   1213:        line = malloc(len + 1);
                   1214:        if (line == NULL) {
                   1215:                cvs_log(LP_ERRNO, "failed to allocate line");
                   1216:                return (NULL);
                   1217:        }
                   1218:        if ((nr = pread(fd, line, len, off)) < 0) {
                   1219:                cvs_log(LP_ERRNO, "preadline failed");
                   1220:                return (NULL);
                   1221:        }
                   1222:        line[nr] = '\0';
                   1223:        return (line);
                   1224: }
                   1225:
                   1226: static int
                   1227: ignoreline(char *line)
                   1228: {
                   1229:        int ret;
                   1230:
                   1231:        ret = regexec(&ignore_re, line, 0, NULL, 0);
                   1232:        free(line);
                   1233:        return (ret == 0);      /* if it matched, it should be ignored. */
                   1234: }
                   1235:
                   1236: /*
                   1237:  * Indicate that there is a difference between lines a and b of the from file
                   1238:  * to get to lines c to d of the to file.  If a is greater then b then there
                   1239:  * are no lines in the from file involved and this means that there were
                   1240:  * lines appended (beginning at b).  If c is greater than d then there are
                   1241:  * lines missing from the to file.
                   1242:  */
                   1243: static void
                   1244: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
                   1245:        int a, int b, int c, int d)
                   1246: {
                   1247:        static size_t max_context = 64;
                   1248:        int i;
                   1249:
                   1250:        if (format != D_IFDEF && a > b && c > d)
                   1251:                return;
                   1252:        if (ignore_pats != NULL) {
                   1253:                char *line;
                   1254:                /*
                   1255:                 * All lines in the change, insert, or delete must
                   1256:                 * match an ignore pattern for the change to be
                   1257:                 * ignored.
                   1258:                 */
                   1259:                if (a <= b) {           /* Changes and deletes. */
                   1260:                        for (i = a; i <= b; i++) {
                   1261:                                line = preadline(fileno(f1),
                   1262:                                    ixold[i] - ixold[i - 1], ixold[i - 1]);
                   1263:                                if (!ignoreline(line))
                   1264:                                        goto proceed;
                   1265:                        }
                   1266:                }
                   1267:                if (a > b || c <= d) {  /* Changes and inserts. */
                   1268:                        for (i = c; i <= d; i++) {
                   1269:                                line = preadline(fileno(f2),
                   1270:                                    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
                   1271:                                if (!ignoreline(line))
                   1272:                                        goto proceed;
                   1273:                        }
                   1274:                }
                   1275:                return;
                   1276:        }
                   1277: proceed:
                   1278:        if (format == D_CONTEXT || format == D_UNIFIED) {
                   1279:                /*
                   1280:                 * Allocate change records as needed.
                   1281:                 */
                   1282:                if (context_vec_ptr == context_vec_end - 1) {
                   1283:                        ptrdiff_t offset = context_vec_ptr - context_vec_start;
                   1284:                        max_context <<= 1;
                   1285:                        context_vec_start = realloc(context_vec_start,
                   1286:                            max_context * sizeof(struct context_vec));
                   1287:                        context_vec_end = context_vec_start + max_context;
                   1288:                        context_vec_ptr = context_vec_start + offset;
                   1289:                }
                   1290:                if (anychange == 0) {
                   1291:                        /*
                   1292:                         * Print the context/unidiff header first time through.
                   1293:                         */
                   1294:                        printf("%s %s   %s",
                   1295:                            format == D_CONTEXT ? "***" : "---", diff_file,
                   1296:                            ctime(&stb1.st_mtime));
                   1297:                        printf("%s %s   %s",
                   1298:                            format == D_CONTEXT ? "---" : "+++", diff_file,
                   1299:                            ctime(&stb2.st_mtime));
                   1300:                        anychange = 1;
                   1301:                } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
                   1302:                    c > context_vec_ptr->d + (2 * context) + 1) {
                   1303:                        /*
                   1304:                         * If this change is more than 'context' lines from the
                   1305:                         * previous change, dump the record and reset it.
                   1306:                         */
                   1307:                        if (format == D_CONTEXT)
                   1308:                                dump_context_vec(f1, f2);
                   1309:                        else
                   1310:                                dump_unified_vec(f1, f2);
                   1311:                }
                   1312:                context_vec_ptr++;
                   1313:                context_vec_ptr->a = a;
                   1314:                context_vec_ptr->b = b;
                   1315:                context_vec_ptr->c = c;
                   1316:                context_vec_ptr->d = d;
                   1317:                return;
                   1318:        }
                   1319:        if (anychange == 0)
                   1320:                anychange = 1;
                   1321:        switch (format) {
                   1322:        case D_BRIEF:
                   1323:                return;
                   1324:        case D_NORMAL:
                   1325:                range(a, b, ",");
                   1326:                putchar(a > b ? 'a' : c > d ? 'd' : 'c');
                   1327:                if (format == D_NORMAL)
                   1328:                        range(c, d, ",");
                   1329:                putchar('\n');
                   1330:                break;
                   1331:        }
                   1332:        if (format == D_NORMAL || format == D_IFDEF) {
                   1333:                fetch(ixold, a, b, f1, '<', 1);
                   1334:                if (a <= b && c <= d && format == D_NORMAL)
                   1335:                        puts("---");
                   1336:        }
                   1337:        i = fetch(ixnew, c, d, f2, format == D_NORMAL ? '>' : '\0', 0);
                   1338:        if (inifdef) {
                   1339:                printf("#endif /* %s */\n", ifdefname);
                   1340:                inifdef = 0;
                   1341:        }
                   1342: }
                   1343:
                   1344: static int
                   1345: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
                   1346: {
                   1347:        int i, j, c, lastc, col, nc;
                   1348:
                   1349:        /*
                   1350:         * When doing #ifdef's, copy down to current line
                   1351:         * if this is the first file, so that stuff makes it to output.
                   1352:         */
                   1353:        if (format == D_IFDEF && oldfile) {
                   1354:                long curpos = ftell(lb);
                   1355:                /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
                   1356:                nc = f[a > b ? b : a - 1] - curpos;
                   1357:                for (i = 0; i < nc; i++)
                   1358:                        putchar(getc(lb));
                   1359:        }
                   1360:        if (a > b)
                   1361:                return (0);
                   1362:        if (format == D_IFDEF) {
                   1363:                if (inifdef) {
                   1364:                        printf("#else /* %s%s */\n",
                   1365:                            oldfile == 1 ? "!" : "", ifdefname);
                   1366:                } else {
                   1367:                        if (oldfile)
                   1368:                                printf("#ifndef %s\n", ifdefname);
                   1369:                        else
                   1370:                                printf("#ifdef %s\n", ifdefname);
                   1371:                }
                   1372:                inifdef = 1 + oldfile;
                   1373:        }
                   1374:        for (i = a; i <= b; i++) {
                   1375:                fseek(lb, f[i - 1], SEEK_SET);
                   1376:                nc = f[i] - f[i - 1];
                   1377:                if (format != D_IFDEF && ch != '\0') {
                   1378:                        putchar(ch);
                   1379:                        if (Tflag && (format == D_NORMAL || format == D_CONTEXT
                   1380:                            || format == D_UNIFIED))
                   1381:                                putchar('\t');
                   1382:                        else if (format != D_UNIFIED)
                   1383:                                putchar(' ');
                   1384:                }
                   1385:                col = 0;
                   1386:                for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
                   1387:                        if ((c = getc(lb)) == EOF) {
                   1388:                                puts("\n\\ No newline at end of file");
                   1389:                                return (0);
                   1390:                        }
                   1391:                        if (c == '\t' && tflag) {
                   1392:                                do {
                   1393:                                        putchar(' ');
                   1394:                                } while (++col & 7);
                   1395:                        } else {
                   1396:                                putchar(c);
                   1397:                                col++;
                   1398:                        }
                   1399:                }
                   1400:        }
                   1401:        return (0);
                   1402: }
                   1403:
                   1404: /*
                   1405:  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
                   1406:  */
                   1407: static int
                   1408: readhash(FILE *f)
                   1409: {
                   1410:        int i, t, space;
                   1411:        int sum;
                   1412:
                   1413:        sum = 1;
                   1414:        space = 0;
                   1415:        if (!bflag && !wflag) {
                   1416:                if (iflag)
                   1417:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1418:                                if (t == EOF) {
                   1419:                                        if (i == 0)
                   1420:                                                return (0);
                   1421:                                        break;
                   1422:                                }
                   1423:                                sum = sum * 127 + chrtran[t];
                   1424:                        }
                   1425:                else
                   1426:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1427:                                if (t == EOF) {
                   1428:                                        if (i == 0)
                   1429:                                                return (0);
                   1430:                                        break;
                   1431:                                }
                   1432:                                sum = sum * 127 + t;
                   1433:                        }
                   1434:        } else {
                   1435:                for (i = 0;;) {
                   1436:                        switch (t = getc(f)) {
                   1437:                        case '\t':
                   1438:                        case ' ':
                   1439:                                space++;
                   1440:                                continue;
                   1441:                        default:
                   1442:                                if (space && !wflag) {
                   1443:                                        i++;
                   1444:                                        space = 0;
                   1445:                                }
                   1446:                                sum = sum * 127 + chrtran[t];
                   1447:                                i++;
                   1448:                                continue;
                   1449:                        case EOF:
                   1450:                                if (i == 0)
                   1451:                                        return (0);
                   1452:                                /* FALLTHROUGH */
                   1453:                        case '\n':
                   1454:                                break;
                   1455:                        }
                   1456:                        break;
                   1457:                }
                   1458:        }
                   1459:        /*
                   1460:         * There is a remote possibility that we end up with a zero sum.
                   1461:         * Zero is used as an EOF marker, so return 1 instead.
                   1462:         */
                   1463:        return (sum == 0 ? 1 : sum);
                   1464: }
                   1465:
                   1466: static int
                   1467: asciifile(FILE *f)
                   1468: {
                   1469:        char buf[BUFSIZ];
                   1470:        int i, cnt;
                   1471:
                   1472:        if (aflag || f == NULL)
                   1473:                return (1);
                   1474:
                   1475:        rewind(f);
                   1476:        cnt = fread(buf, 1, sizeof(buf), f);
                   1477:        for (i = 0; i < cnt; i++)
                   1478:                if (!isprint(buf[i]) && !isspace(buf[i]))
                   1479:                        return (0);
                   1480:        return (1);
                   1481: }
                   1482:
                   1483: static __inline int min(int a, int b)
                   1484: {
                   1485:        return (a < b ? a : b);
                   1486: }
                   1487:
                   1488: static __inline int max(int a, int b)
                   1489: {
                   1490:        return (a > b ? a : b);
                   1491: }
                   1492:
                   1493: static char *
                   1494: match_function(const long *f, int pos, FILE *file)
                   1495: {
                   1496:        char buf[FUNCTION_CONTEXT_SIZE];
                   1497:        size_t nc;
                   1498:        int last = lastline;
                   1499:        char *p;
                   1500:
                   1501:        lastline = pos;
                   1502:        while (pos > last) {
                   1503:                fseek(file, f[pos - 1], SEEK_SET);
                   1504:                nc = f[pos] - f[pos - 1];
                   1505:                if (nc >= sizeof(buf))
                   1506:                        nc = sizeof(buf) - 1;
                   1507:                nc = fread(buf, 1, nc, file);
                   1508:                if (nc > 0) {
                   1509:                        buf[nc] = '\0';
                   1510:                        p = strchr(buf, '\n');
                   1511:                        if (p != NULL)
                   1512:                                *p = '\0';
                   1513:                        if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
                   1514:                                strlcpy(lastbuf, buf, sizeof lastbuf);
                   1515:                                lastmatchline = pos;
                   1516:                                return lastbuf;
                   1517:                        }
                   1518:                }
                   1519:                pos--;
                   1520:        }
                   1521:        return lastmatchline > 0 ? lastbuf : NULL;
                   1522: }
                   1523:
                   1524: /* dump accumulated "context" diff changes */
                   1525: static void
                   1526: dump_context_vec(FILE *f1, FILE *f2)
                   1527: {
                   1528:        struct context_vec *cvp = context_vec_start;
                   1529:        int lowa, upb, lowc, upd, do_output;
                   1530:        int a, b, c, d;
                   1531:        char ch, *f;
                   1532:
                   1533:        if (context_vec_start > context_vec_ptr)
                   1534:                return;
                   1535:
                   1536:        b = d = 0;              /* gcc */
                   1537:        lowa = max(1, cvp->a - context);
                   1538:        upb = min(len[0], context_vec_ptr->b + context);
                   1539:        lowc = max(1, cvp->c - context);
                   1540:        upd = min(len[1], context_vec_ptr->d + context);
                   1541:
                   1542:        printf("***************");
                   1543:        printf("\n*** ");
                   1544:        range(lowa, upb, ",");
                   1545:        printf(" ****\n");
                   1546:
                   1547:        /*
                   1548:         * Output changes to the "old" file.  The first loop suppresses
                   1549:         * output if there were no changes to the "old" file (we'll see
                   1550:         * the "old" lines as context in the "new" list).
                   1551:         */
                   1552:        do_output = 0;
                   1553:        for (; cvp <= context_vec_ptr; cvp++)
                   1554:                if (cvp->a <= cvp->b) {
                   1555:                        cvp = context_vec_start;
                   1556:                        do_output++;
                   1557:                        break;
                   1558:                }
                   1559:        if (do_output) {
                   1560:                while (cvp <= context_vec_ptr) {
                   1561:                        a = cvp->a;
                   1562:                        b = cvp->b;
                   1563:                        c = cvp->c;
                   1564:                        d = cvp->d;
                   1565:
                   1566:                        if (a <= b && c <= d)
                   1567:                                ch = 'c';
                   1568:                        else
                   1569:                                ch = (a <= b) ? 'd' : 'a';
                   1570:
                   1571:                        if (ch == 'a')
                   1572:                                fetch(ixold, lowa, b, f1, ' ', 0);
                   1573:                        else {
                   1574:                                fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1575:                                fetch(ixold, a, b, f1,
                   1576:                                    ch == 'c' ? '!' : '-', 0);
                   1577:                        }
                   1578:                        lowa = b + 1;
                   1579:                        cvp++;
                   1580:                }
                   1581:                fetch(ixold, b + 1, upb, f1, ' ', 0);
                   1582:        }
                   1583:        /* output changes to the "new" file */
                   1584:        printf("--- ");
                   1585:        range(lowc, upd, ",");
                   1586:        printf(" ----\n");
                   1587:
                   1588:        do_output = 0;
                   1589:        for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
                   1590:                if (cvp->c <= cvp->d) {
                   1591:                        cvp = context_vec_start;
                   1592:                        do_output++;
                   1593:                        break;
                   1594:                }
                   1595:        if (do_output) {
                   1596:                while (cvp <= context_vec_ptr) {
                   1597:                        a = cvp->a;
                   1598:                        b = cvp->b;
                   1599:                        c = cvp->c;
                   1600:                        d = cvp->d;
                   1601:
                   1602:                        if (a <= b && c <= d)
                   1603:                                ch = 'c';
                   1604:                        else
                   1605:                                ch = (a <= b) ? 'd' : 'a';
                   1606:
                   1607:                        if (ch == 'd')
                   1608:                                fetch(ixnew, lowc, d, f2, ' ', 0);
                   1609:                        else {
                   1610:                                fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1611:                                fetch(ixnew, c, d, f2,
                   1612:                                    ch == 'c' ? '!' : '+', 0);
                   1613:                        }
                   1614:                        lowc = d + 1;
                   1615:                        cvp++;
                   1616:                }
                   1617:                fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1618:        }
                   1619:        context_vec_ptr = context_vec_start - 1;
                   1620: }
                   1621:
                   1622: /* dump accumulated "unified" diff changes */
                   1623: static void
                   1624: dump_unified_vec(FILE *f1, FILE *f2)
                   1625: {
                   1626:        struct context_vec *cvp = context_vec_start;
                   1627:        int lowa, upb, lowc, upd;
                   1628:        int a, b, c, d;
                   1629:        char ch, *f;
                   1630:
                   1631:        if (context_vec_start > context_vec_ptr)
                   1632:                return;
                   1633:
                   1634:        b = d = 0;              /* gcc */
                   1635:        lowa = max(1, cvp->a - context);
                   1636:        upb = min(len[0], context_vec_ptr->b + context);
                   1637:        lowc = max(1, cvp->c - context);
                   1638:        upd = min(len[1], context_vec_ptr->d + context);
                   1639:
                   1640:        fputs("@@ -", stdout);
                   1641:        uni_range(lowa, upb);
                   1642:        fputs(" +", stdout);
                   1643:        uni_range(lowc, upd);
                   1644:        fputs(" @@", stdout);
                   1645:        putchar('\n');
                   1646:
                   1647:        /*
                   1648:         * Output changes in "unified" diff format--the old and new lines
                   1649:         * are printed together.
                   1650:         */
                   1651:        for (; cvp <= context_vec_ptr; cvp++) {
                   1652:                a = cvp->a;
                   1653:                b = cvp->b;
                   1654:                c = cvp->c;
                   1655:                d = cvp->d;
                   1656:
                   1657:                /*
                   1658:                 * c: both new and old changes
                   1659:                 * d: only changes in the old file
                   1660:                 * a: only changes in the new file
                   1661:                 */
                   1662:                if (a <= b && c <= d)
                   1663:                        ch = 'c';
                   1664:                else
                   1665:                        ch = (a <= b) ? 'd' : 'a';
                   1666:
                   1667:                switch (ch) {
                   1668:                case 'c':
                   1669:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1670:                        fetch(ixold, a, b, f1, '-', 0);
                   1671:                        fetch(ixnew, c, d, f2, '+', 0);
                   1672:                        break;
                   1673:                case 'd':
                   1674:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1675:                        fetch(ixold, a, b, f1, '-', 0);
                   1676:                        break;
                   1677:                case 'a':
                   1678:                        fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1679:                        fetch(ixnew, c, d, f2, '+', 0);
                   1680:                        break;
                   1681:                }
                   1682:                lowa = b + 1;
                   1683:                lowc = d + 1;
                   1684:        }
                   1685:        fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1686:
                   1687:        context_vec_ptr = context_vec_start - 1;
                   1688: }