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

1.78    ! xsa         1: /*     $OpenBSD: diff.c,v 1.77 2006/01/25 11:13:18 xsa Exp $   */
1.1       jfb         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:
1.75      xsa       129: #include "includes.h"
1.1       jfb       130:
1.41      xsa       131: #include "buf.h"
1.1       jfb       132: #include "cvs.h"
1.56      niallo    133: #include "diff.h"
1.1       jfb       134: #include "log.h"
1.4       jfb       135: #include "proto.h"
1.1       jfb       136:
                    137: struct cand {
1.53      xsa       138:        int     x;
                    139:        int     y;
                    140:        int     pred;
1.1       jfb       141: } cand;
                    142:
                    143: struct line {
1.53      xsa       144:        int     serial;
                    145:        int     value;
1.1       jfb       146: } *file[2];
                    147:
                    148: /*
1.58      niallo    149:  * The following struct is used to record change in formation when
1.1       jfb       150:  * doing a "context" or "unified" diff.  (see routine "change" to
                    151:  * understand the highly mnemonic field names)
                    152:  */
                    153: struct context_vec {
1.53      xsa       154:        int     a;      /* start line in old file */
                    155:        int     b;      /* end line in old file */
                    156:        int     c;      /* start line in new file */
                    157:        int     d;      /* end line in new file */
1.1       jfb       158: };
                    159:
1.4       jfb       160: struct diff_arg {
1.53      xsa       161:        char    *rev1;
                    162:        char    *rev2;
                    163:        char    *date1;
                    164:        char    *date2;
1.4       jfb       165: };
1.46      xsa       166:
1.56      niallo    167: #if !defined(RCSPROG)
1.64      xsa       168: static int     cvs_diff_init(struct cvs_cmd *, int, char **, int *);
1.53      xsa       169: static int     cvs_diff_remote(CVSFILE *, void *);
                    170: static int     cvs_diff_local(CVSFILE *, void *);
                    171: static int     cvs_diff_pre_exec(struct cvsroot *);
                    172: static int     cvs_diff_cleanup(void);
1.56      niallo    173: #endif
1.53      xsa       174:
                    175: static void     output(const char *, FILE *, const char *, FILE *);
                    176: static void     check(FILE *, FILE *);
                    177: static void     range(int, int, char *);
                    178: static void     uni_range(int, int);
                    179: static void     dump_context_vec(FILE *, FILE *);
                    180: static void     dump_unified_vec(FILE *, FILE *);
                    181: static int      prepare(int, FILE *, off_t);
                    182: static void     prune(void);
                    183: static void     equiv(struct line *, int, struct line *, int, int *);
                    184: static void     unravel(int);
                    185: static void     unsort(struct line *, int, int *);
                    186: static void     change(const char *, FILE *, const char *, FILE *, int,
                    187:                    int, int, int);
                    188: static void     sort(struct line *, int);
                    189: static int      ignoreline(char *);
                    190: static int      asciifile(FILE *);
                    191: static int      fetch(long *, int, int, FILE *, int, int);
                    192: static int      newcand(int, int, int);
                    193: static int      search(int *, int, int);
                    194: static int      skipline(FILE *);
                    195: static int      isqrt(int);
                    196: static int      stone(int *, int, int *, int *);
                    197: static int      readhash(FILE *);
                    198: static int      files_differ(FILE *, FILE *);
                    199: static char    *match_function(const long *, int, FILE *);
                    200: static char    *preadline(int, size_t, off_t);
1.1       jfb       201:
                    202:
1.56      niallo    203: #if !defined(RCSPROG)
                    204: static int Nflag;
                    205: #endif
                    206: static int aflag, bflag, dflag, iflag, pflag, tflag, Tflag, wflag;
1.68      niallo    207: static int context = 3;
1.58      niallo    208: int diff_format = D_NORMAL;
1.61      joris     209: char *diff_file = NULL;
1.66      xsa       210: char diffargs[128];
1.1       jfb       211: static struct stat stb1, stb2;
1.56      niallo    212: static char *ifdefname, *ignore_pats;
1.1       jfb       213: regex_t ignore_re;
                    214:
                    215: static int  *J;                        /* will be overlaid on class */
                    216: static int  *class;            /* will be overlaid on file[0] */
                    217: static int  *klist;            /* will be overlaid on file[0] after class */
                    218: static int  *member;           /* will be overlaid on file[1] */
                    219: static int   clen;
                    220: static int   inifdef;          /* whether or not we are in a #ifdef block */
1.36      jfb       221: static int   diff_len[2];
1.1       jfb       222: static int   pref, suff;       /* length of prefix and suffix */
                    223: static int   slen[2];
                    224: static int   anychange;
                    225: static long *ixnew;            /* will be overlaid on file[1] */
                    226: static long *ixold;            /* will be overlaid on klist */
                    227: static struct cand *clist;     /* merely a free storage pot for candidates */
                    228: static int   clistlen;         /* the length of clist */
                    229: static struct line *sfile[2];  /* shortened by pruning common prefix/suffix */
                    230: static u_char *chrtran;                /* translation table for case-folding */
                    231: static struct context_vec *context_vec_start;
                    232: static struct context_vec *context_vec_end;
                    233: static struct context_vec *context_vec_ptr;
                    234:
                    235: #define FUNCTION_CONTEXT_SIZE  41
1.17      jfb       236: static char lastbuf[FUNCTION_CONTEXT_SIZE];
                    237: static int  lastline;
                    238: static int  lastmatchline;
1.63      joris     239: BUF  *diffbuf = NULL;
1.58      niallo    240:
1.1       jfb       241: /*
                    242:  * chrtran points to one of 2 translation tables: cup2low if folding upper to
                    243:  * lower case clow2low if not folding case
                    244:  */
                    245: u_char clow2low[256] = {
                    246:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    247:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    248:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    249:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    250:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    251:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
                    252:        0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                    253:        0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
                    254:        0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
                    255:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    256:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    257:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    258:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    259:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    260:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    261:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    262:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    263:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    264:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    265:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    266:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    267:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    268:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    269:        0xfd, 0xfe, 0xff
                    270: };
                    271:
                    272: u_char cup2low[256] = {
                    273:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    274:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    275:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    276:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    277:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    278:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
                    279:        0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
                    280:        0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
                    281:        0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
                    282:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    283:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    284:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    285:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    286:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    287:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    288:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    289:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    290:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    291:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    292:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    293:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    294:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    295:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    296:        0xfd, 0xfe, 0xff
                    297: };
                    298:
1.56      niallo    299: #if !defined(RCSPROG)
1.36      jfb       300: struct cvs_cmd cvs_cmd_diff = {
                    301:        CVS_OP_DIFF, CVS_REQ_DIFF, "diff",
                    302:        { "di", "dif" },
                    303:        "Show differences between revisions",
1.43      xsa       304:        "[-cilNnpu] [[-D date] [-r rev] [-D date2 | -r rev2]] "
                    305:        "[-k mode] [file ...]",
                    306:        "cD:iklNnpr:Ru",
1.36      jfb       307:        NULL,
                    308:        CF_RECURSE | CF_IGNORE | CF_SORT | CF_KNOWN,
                    309:        cvs_diff_init,
                    310:        cvs_diff_pre_exec,
                    311:        cvs_diff_remote,
                    312:        cvs_diff_local,
                    313:        NULL,
                    314:        cvs_diff_cleanup,
                    315:        CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
                    316: };
                    317:
                    318:
                    319: struct cvs_cmd cvs_cmd_rdiff = {
                    320:        CVS_OP_RDIFF, CVS_REQ_DIFF, "rdiff",
1.43      xsa       321:        { "pa", "patch" },
1.36      jfb       322:        "Create 'patch' format diffs between releases",
1.43      xsa       323:        "[-flR] [-c | -u] [-s | -t] [-V ver] -D date | -r rev "
                    324:        "[-D date2 | -rev2] module ...",
                    325:        "cD:flRr:stuV:",
1.23      joris     326:        NULL,
                    327:        CF_RECURSE | CF_IGNORE | CF_SORT | CF_KNOWN,
1.36      jfb       328:        cvs_diff_init,
                    329:        cvs_diff_pre_exec,
                    330:        cvs_diff_remote,
                    331:        cvs_diff_local,
                    332:        NULL,
                    333:        cvs_diff_cleanup,
1.29      joris     334:        CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
1.23      joris     335: };
1.56      niallo    336: #endif
1.23      joris     337:
1.56      niallo    338: #if !defined(RCSPROG)
1.23      joris     339: static struct diff_arg *dap = NULL;
                    340: static int recurse;
1.1       jfb       341:
1.36      jfb       342: static int
                    343: cvs_diff_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb       344: {
1.23      joris     345:        int ch;
1.1       jfb       346:
1.71      joris     347:        dap = (struct diff_arg *)xmalloc(sizeof(*dap));
1.23      joris     348:        dap->date1 = dap->date2 = dap->rev1 = dap->rev2 = NULL;
1.2       jfb       349:        strlcpy(diffargs, argv[0], sizeof(diffargs));
1.1       jfb       350:
1.36      jfb       351:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb       352:                switch (ch) {
                    353:                case 'c':
1.2       jfb       354:                        strlcat(diffargs, " -c", sizeof(diffargs));
1.58      niallo    355:                        diff_format = D_CONTEXT;
1.1       jfb       356:                        break;
                    357:                case 'D':
1.23      joris     358:                        if (dap->date1 == NULL && dap->rev1 == NULL) {
                    359:                                dap->date1 = optarg;
                    360:                        } else if (dap->date2 == NULL && dap->rev2 == NULL) {
                    361:                                dap->date2 = optarg;
                    362:                        } else {
1.1       jfb       363:                                cvs_log(LP_ERR,
                    364:                                    "no more than two revisions/dates can "
                    365:                                    "be specified");
                    366:                        }
                    367:                        break;
                    368:                case 'l':
1.2       jfb       369:                        strlcat(diffargs, " -l", sizeof(diffargs));
1.1       jfb       370:                        recurse = 0;
1.36      jfb       371:                        cvs_cmd_diff.file_flags &= ~CF_RECURSE;
1.1       jfb       372:                        break;
                    373:                case 'i':
1.2       jfb       374:                        strlcat(diffargs, " -i", sizeof(diffargs));
1.1       jfb       375:                        iflag = 1;
                    376:                        break;
1.8       jfb       377:                case 'N':
                    378:                        strlcat(diffargs, " -N", sizeof(diffargs));
                    379:                        Nflag = 1;
                    380:                        break;
1.33      jfb       381:                case 'n':
                    382:                        strlcat(diffargs, " -n", sizeof(diffargs));
1.58      niallo    383:                        diff_format = D_RCSDIFF;
1.33      jfb       384:                        break;
1.17      jfb       385:                case 'p':
                    386:                        strlcat(diffargs, " -p", sizeof(diffargs));
                    387:                        pflag = 1;
                    388:                        break;
1.1       jfb       389:                case 'r':
1.23      joris     390:                        if ((dap->rev1 == NULL) && (dap->date1 == NULL)) {
                    391:                                dap->rev1 = optarg;
                    392:                        } else if ((dap->rev2 == NULL) &&
                    393:                            (dap->date2 == NULL)) {
                    394:                                dap->rev2 = optarg;
                    395:                        } else {
1.1       jfb       396:                                cvs_log(LP_ERR,
                    397:                                    "no more than two revisions/dates can "
                    398:                                    "be specified");
1.28      joris     399:                                return (CVS_EX_USAGE);
1.1       jfb       400:                        }
1.32      joris     401:                        break;
                    402:                case 'R':
1.36      jfb       403:                        cvs_cmd_diff.file_flags |= CF_RECURSE;
1.1       jfb       404:                        break;
                    405:                case 'u':
1.2       jfb       406:                        strlcat(diffargs, " -u", sizeof(diffargs));
1.58      niallo    407:                        diff_format = D_UNIFIED;
1.1       jfb       408:                        break;
                    409:                default:
1.28      joris     410:                        return (CVS_EX_USAGE);
1.1       jfb       411:                }
                    412:        }
                    413:
1.23      joris     414:        *arg = optind;
                    415:        return (0);
                    416: }
1.1       jfb       417:
1.23      joris     418: int
                    419: cvs_diff_cleanup(void)
                    420: {
1.36      jfb       421:        if (dap != NULL) {
1.71      joris     422:                xfree(dap);
1.36      jfb       423:                dap = NULL;
                    424:        }
1.4       jfb       425:        return (0);
                    426: }
1.1       jfb       427:
1.4       jfb       428: /*
1.36      jfb       429:  * cvs_diff_pre_exec()
1.4       jfb       430:  *
                    431:  */
                    432: int
1.36      jfb       433: cvs_diff_pre_exec(struct cvsroot *root)
1.4       jfb       434: {
1.36      jfb       435:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    436:                /* send the flags */
1.74      joris     437:                if (Nflag == 1)
                    438:                        cvs_sendarg(root, "-N", 0);
                    439:                if (pflag == 1)
                    440:                        cvs_sendarg(root, "-p", 0);
                    441:
                    442:                if (diff_format == D_CONTEXT)
                    443:                        cvs_sendarg(root, "-c", 0);
                    444:                else if (diff_format == D_UNIFIED)
                    445:                        cvs_sendarg(root, "-u", 0);
1.17      jfb       446:
1.36      jfb       447:                if (dap->rev1 != NULL) {
1.74      joris     448:                        cvs_sendarg(root, "-r", 0);
                    449:                        cvs_sendarg(root, dap->rev1, 0);
1.36      jfb       450:                } else if (dap->date1 != NULL) {
1.74      joris     451:                        cvs_sendarg(root, "-D", 0);
                    452:                        cvs_sendarg(root, dap->date1, 0);
1.36      jfb       453:                }
                    454:                if (dap->rev2 != NULL) {
1.74      joris     455:                        cvs_sendarg(root, "-r", 0);
                    456:                        cvs_sendarg(root, dap->rev2, 0);
1.36      jfb       457:                } else if (dap->date2 != NULL) {
1.74      joris     458:                        cvs_sendarg(root, "-D", 0);
                    459:                        cvs_sendarg(root, dap->date2, 0);
1.36      jfb       460:                }
1.1       jfb       461:        }
                    462:
                    463:        return (0);
                    464: }
                    465:
                    466:
                    467: /*
                    468:  * cvs_diff_file()
                    469:  *
                    470:  * Diff a single file.
                    471:  */
1.36      jfb       472: static int
                    473: cvs_diff_remote(struct cvs_file *cfp, void *arg)
1.1       jfb       474: {
1.36      jfb       475:        char *dir, *repo;
                    476:        char fpath[MAXPATHLEN], dfpath[MAXPATHLEN];
1.4       jfb       477:        struct cvsroot *root;
1.1       jfb       478:
1.4       jfb       479:        if (cfp->cf_type == DT_DIR) {
1.7       jfb       480:                if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.31      jfb       481:                        root = cfp->cf_parent->cf_root;
1.52      joris     482:                        cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.10      deraadt   483:                } else {
1.31      jfb       484:                        root = cfp->cf_root;
1.23      joris     485: #if 0
1.7       jfb       486:                        if ((cfp->cf_parent == NULL) ||
1.31      jfb       487:                            (root != cfp->cf_parent->cf_root)) {
1.7       jfb       488:                                cvs_connect(root);
1.36      jfb       489:                                cvs_diff_pre_exec(root);
1.7       jfb       490:                        }
1.23      joris     491: #endif
1.7       jfb       492:
                    493:                        cvs_senddir(root, cfp);
1.4       jfb       494:                }
1.1       jfb       495:
1.13      jfb       496:                return (0);
                    497:        }
                    498:
                    499:        if (cfp->cf_cvstat == CVS_FST_LOST) {
1.52      joris     500:                cvs_log(LP_WARN, "cannot find file %s", cfp->cf_name);
1.4       jfb       501:                return (0);
1.1       jfb       502:        }
                    503:
1.8       jfb       504:        diff_file = cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.7       jfb       505:
1.4       jfb       506:        if (cfp->cf_parent != NULL) {
1.8       jfb       507:                dir = cvs_file_getpath(cfp->cf_parent, dfpath, sizeof(dfpath));
1.31      jfb       508:                root = cfp->cf_parent->cf_root;
                    509:                repo = cfp->cf_parent->cf_repo;
1.10      deraadt   510:        } else {
1.4       jfb       511:                dir = ".";
1.7       jfb       512:                root = NULL;
1.4       jfb       513:                repo = NULL;
1.1       jfb       514:        }
                    515:
1.4       jfb       516:        if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.36      jfb       517:                cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.4       jfb       518:                return (0);
1.1       jfb       519:        }
                    520:
1.74      joris     521:        cvs_sendentry(root, cfp);
1.1       jfb       522:
1.4       jfb       523:        if (cfp->cf_cvstat == CVS_FST_UPTODATE) {
1.36      jfb       524:                cvs_sendreq(root, CVS_REQ_UNCHANGED, cfp->cf_name);
1.1       jfb       525:                return (0);
                    526:        }
                    527:
                    528:        /* at this point, the file is modified */
1.74      joris     529:        cvs_sendreq(root, CVS_REQ_MODIFIED, cfp->cf_name);
                    530:        cvs_sendfile(root, diff_file);
1.36      jfb       531:
                    532:        return (0);
                    533: }
                    534:
                    535: static int
                    536: cvs_diff_local(CVSFILE *cf, void *arg)
                    537: {
                    538:        char *repo, buf[64];
                    539:        char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
                    540:        char path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
                    541:        BUF *b1, *b2;
                    542:        RCSNUM *r1, *r2;
                    543:        RCSFILE *rf;
                    544:        struct cvsroot *root;
1.69      niallo    545:        struct timeval tv[2], tv2[2];
                    546:
                    547:        memset(&tv, 0, sizeof(tv));
                    548:        memset(&tv2, 0, sizeof(tv2));
1.1       jfb       549:
1.36      jfb       550:        rf = NULL;
                    551:        root = CVS_DIR_ROOT(cf);
                    552:        repo = CVS_DIR_REPO(cf);
                    553:        diff_file = cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       jfb       554:
1.36      jfb       555:        if (cf->cf_type == DT_DIR) {
1.46      xsa       556:                if (verbosity > 1)
1.54      xsa       557:                        cvs_log(LP_NOTICE, "Diffing %s", fpath);
1.36      jfb       558:                return (0);
                    559:        }
1.1       jfb       560:
1.36      jfb       561:        if (cf->cf_cvstat == CVS_FST_LOST) {
                    562:                cvs_log(LP_WARN, "cannot find file %s", cf->cf_name);
                    563:                return (0);
                    564:        }
1.1       jfb       565:
1.36      jfb       566:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
                    567:                cvs_log(LP_WARN, "I know nothing about %s", diff_file);
                    568:                return (0);
1.45      niallo    569:        } else if (cf->cf_cvstat == CVS_FST_UPTODATE)
1.36      jfb       570:                return (0);
1.1       jfb       571:
1.36      jfb       572:        /* at this point, the file is modified */
1.77      xsa       573:        cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.20      jfb       574:
1.78    ! xsa       575:        if ((rf = rcs_open(rcspath, RCS_READ)) == NULL)
        !           576:                fatal("cvs_diff_local: rcs_open `%s': %s", rcspath,
        !           577:                    strerror(rcs_errno));
1.1       jfb       578:
1.36      jfb       579:        cvs_printf("Index: %s\n%s\nRCS file: %s\n", diff_file,
                    580:            RCS_DIFF_DIV, rcspath);
1.4       jfb       581:
1.36      jfb       582:        if (dap->rev1 == NULL)
                    583:                r1 = cf->cf_lrev;
                    584:        else {
1.78    ! xsa       585:                if ((r1 = rcsnum_parse(dap->rev1)) == NULL)
        !           586:                        fatal("cvs_diff_local: rcsnum_parse failed");
1.36      jfb       587:        }
                    588:
                    589:        cvs_printf("retrieving revision %s\n",
                    590:            rcsnum_tostr(r1, buf, sizeof(buf)));
                    591:        b1 = rcs_getrev(rf, r1);
                    592:
1.39      joris     593:        if (b1 == NULL) {
1.40      xsa       594:                cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39      joris     595:                    rcsnum_tostr(r1, buf, sizeof(buf)));
                    596:                if (r1 != cf->cf_lrev)
                    597:                        rcsnum_free(r1);
1.62      joris     598:                rcs_close(rf);
1.39      joris     599:                return (CVS_EX_DATA);
                    600:        }
1.69      niallo    601:        tv[0].tv_sec = (long)rcs_rev_getdate(rf, r1);
                    602:        tv[1].tv_sec = tv[0].tv_sec;
1.39      joris     603:
1.36      jfb       604:        if (r1 != cf->cf_lrev)
                    605:                rcsnum_free(r1);
1.20      jfb       606:
1.36      jfb       607:        if (dap->rev2 != NULL) {
                    608:                cvs_printf("retrieving revision %s\n", dap->rev2);
                    609:                if ((r2 = rcsnum_parse(dap->rev2)) == NULL) {
1.62      joris     610:                        rcs_close(rf);
1.28      joris     611:                        return (CVS_EX_DATA);
1.12      djm       612:                }
1.36      jfb       613:                b2 = rcs_getrev(rf, r2);
1.70      niallo    614:                tv2[0].tv_sec = (long)rcs_rev_getdate(rf, r2);
                    615:                tv2[1].tv_sec = tv2[0].tv_sec;
1.36      jfb       616:                rcsnum_free(r2);
                    617:        } else {
1.70      niallo    618:                struct stat st;
                    619:                if (stat(diff_file, &st) < 0) {
                    620:                        cvs_log(LP_ERR, "failed to retrieve revision %s\n",
                    621:                            dap->rev2);
                    622:                        cvs_buf_free(b1);
                    623:                        return (CVS_EX_DATA);
                    624:                }
1.36      jfb       625:                b2 = cvs_buf_load(diff_file, BUF_AUTOEXT);
1.70      niallo    626:                tv2[0].tv_sec = st.st_mtime;
                    627:                tv2[1].tv_sec = st.st_mtime;
1.36      jfb       628:        }
                    629:
                    630:        rcs_close(rf);
1.39      joris     631:
                    632:        if (b2 == NULL) {
1.40      xsa       633:                cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39      joris     634:                    dap->rev2);
                    635:                cvs_buf_free(b1);
                    636:                return (CVS_EX_DATA);
                    637:        }
1.36      jfb       638:
1.42      joris     639:        cvs_printf("%s", diffargs);
                    640:        cvs_printf(" -r%s", buf);
1.36      jfb       641:        if (dap->rev2 != NULL)
1.42      joris     642:                cvs_printf(" -r%s", dap->rev2);
                    643:        cvs_printf(" %s\n", diff_file);
1.67      xsa       644:        strlcpy(path_tmp1, cvs_tmpdir, sizeof(path_tmp1));
                    645:        strlcat(path_tmp1, "/diff1.XXXXXXXXXX", sizeof(path_tmp1));
1.72      xsa       646:        cvs_buf_write_stmp(b1, path_tmp1, 0600);
1.36      jfb       647:        cvs_buf_free(b1);
1.69      niallo    648:        if (utimes(path_tmp1, (const struct timeval *)&tv) < 0)
                    649:                cvs_log(LP_ERRNO, "error setting utimes");
1.20      jfb       650:
1.67      xsa       651:        strlcpy(path_tmp2, cvs_tmpdir, sizeof(path_tmp2));
                    652:        strlcat(path_tmp2, "/diff2.XXXXXXXXXX", sizeof(path_tmp2));
1.73      xsa       653:        cvs_buf_write_stmp(b2, path_tmp2, 0600);
1.36      jfb       654:        cvs_buf_free(b2);
1.69      niallo    655:        if (utimes(path_tmp2, (const struct timeval *)&tv2) < 0)
                    656:                cvs_log(LP_ERRNO, "error setting utimes");
1.36      jfb       657:
1.58      niallo    658:        cvs_diffreg(path_tmp1, path_tmp2, NULL);
1.36      jfb       659:        (void)unlink(path_tmp1);
                    660:        (void)unlink(path_tmp2);
1.1       jfb       661:
                    662:        return (0);
                    663: }
1.56      niallo    664: #endif
1.1       jfb       665:
                    666:
                    667: int
1.58      niallo    668: cvs_diffreg(const char *file1, const char *file2, BUF *out)
1.1       jfb       669: {
                    670:        FILE *f1, *f2;
                    671:        int i, rval;
1.20      jfb       672:        void *tmp;
1.1       jfb       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);
1.58      niallo    681:        if (out != NULL)
                    682:                diffbuf = out;
1.1       jfb       683:
                    684:        f1 = fopen(file1, "r");
                    685:        if (f1 == NULL) {
                    686:                cvs_log(LP_ERRNO, "%s", file1);
                    687:                goto closem;
                    688:        }
                    689:
                    690:        f2 = fopen(file2, "r");
                    691:        if (f2 == NULL) {
                    692:                cvs_log(LP_ERRNO, "%s", file2);
                    693:                goto closem;
                    694:        }
                    695:
1.69      niallo    696:        if (stat(file1, &stb1) < 0) {
                    697:                cvs_log(LP_ERRNO, "%s", file1);
                    698:                goto closem;
                    699:        }
                    700:        if (stat(file2, &stb2) < 0) {
                    701:                cvs_log(LP_ERRNO, "%s", file2);
                    702:                goto closem;
                    703:        }
1.1       jfb       704:        switch (files_differ(f1, f2)) {
                    705:        case 0:
                    706:                goto closem;
                    707:        case 1:
                    708:                break;
                    709:        default:
                    710:                /* error */
                    711:                goto closem;
                    712:        }
                    713:
                    714:        if (!asciifile(f1) || !asciifile(f2)) {
                    715:                rval = D_BINARY;
                    716:                goto closem;
                    717:        }
1.20      jfb       718:        if ((prepare(0, f1, stb1.st_size) < 0) ||
                    719:            (prepare(1, f2, stb2.st_size) < 0)) {
                    720:                goto closem;
                    721:        }
1.1       jfb       722:        prune();
                    723:        sort(sfile[0], slen[0]);
                    724:        sort(sfile[1], slen[1]);
                    725:
                    726:        member = (int *)file[1];
                    727:        equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.71      joris     728:        tmp = xrealloc(member, (slen[1] + 2) * sizeof(int));
1.20      jfb       729:        member = (int *)tmp;
1.1       jfb       730:
                    731:        class = (int *)file[0];
                    732:        unsort(sfile[0], slen[0], class);
1.71      joris     733:        tmp = xrealloc(class, (slen[0] + 2) * sizeof(int));
1.20      jfb       734:        class = (int *)tmp;
1.1       jfb       735:
1.71      joris     736:        klist = xmalloc((slen[0] + 2) * sizeof(int));
1.1       jfb       737:        clen = 0;
                    738:        clistlen = 100;
1.71      joris     739:        clist = xmalloc(clistlen * sizeof(cand));
1.48      joris     740:
                    741:        if ((i = stone(class, slen[0], member, klist)) < 0)
                    742:                goto closem;
                    743:
1.71      joris     744:        xfree(member);
                    745:        xfree(class);
1.1       jfb       746:
1.71      joris     747:        tmp = xrealloc(J, (diff_len[0] + 2) * sizeof(int));
1.49      niallo    748:        J = (int *)tmp;
1.1       jfb       749:        unravel(klist[i]);
1.71      joris     750:        xfree(clist);
                    751:        xfree(klist);
1.1       jfb       752:
1.71      joris     753:        tmp = xrealloc(ixold, (diff_len[0] + 2) * sizeof(long));
1.49      niallo    754:        ixold = (long *)tmp;
1.71      joris     755:
                    756:        tmp = xrealloc(ixnew, (diff_len[1] + 2) * sizeof(long));
1.49      niallo    757:        ixnew = (long *)tmp;
1.1       jfb       758:        check(f1, f2);
                    759:        output(file1, f1, file2, f2);
                    760:
                    761: closem:
1.65      xsa       762:        if (anychange == 1) {
1.1       jfb       763:                if (rval == D_SAME)
                    764:                        rval = D_DIFFER;
                    765:        }
                    766:        if (f1 != NULL)
                    767:                fclose(f1);
                    768:        if (f2 != NULL)
                    769:                fclose(f2);
1.20      jfb       770:
1.1       jfb       771:        return (rval);
                    772: }
                    773:
                    774: /*
                    775:  * Check to see if the given files differ.
                    776:  * Returns 0 if they are the same, 1 if different, and -1 on error.
                    777:  * XXX - could use code from cmp(1) [faster]
                    778:  */
                    779: static int
                    780: files_differ(FILE *f1, FILE *f2)
                    781: {
                    782:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    783:        size_t i, j;
                    784:
                    785:        if (stb1.st_size != stb2.st_size)
                    786:                return (1);
                    787:        for (;;) {
1.55      xsa       788:                i = fread(buf1, (size_t)1, sizeof(buf1), f1);
                    789:                j = fread(buf2, (size_t)1, sizeof(buf2), f2);
1.1       jfb       790:                if (i != j)
                    791:                        return (1);
1.65      xsa       792:                if ((i == 0) && (j == 0)) {
1.1       jfb       793:                        if (ferror(f1) || ferror(f2))
                    794:                                return (1);
                    795:                        return (0);
                    796:                }
                    797:                if (memcmp(buf1, buf2, i) != 0)
                    798:                        return (1);
                    799:        }
                    800: }
                    801:
1.20      jfb       802: static int
1.1       jfb       803: prepare(int i, FILE *fd, off_t filesize)
                    804: {
1.20      jfb       805:        void *tmp;
1.1       jfb       806:        struct line *p;
                    807:        int j, h;
                    808:        size_t sz;
                    809:
                    810:        rewind(fd);
                    811:
1.16      xsa       812:        sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
1.1       jfb       813:        if (sz < 100)
                    814:                sz = 100;
                    815:
1.71      joris     816:        p = (struct line *)xmalloc((sz + 3) * sizeof(struct line));
1.1       jfb       817:        for (j = 0; (h = readhash(fd));) {
                    818:                if (j == (int)sz) {
                    819:                        sz = sz * 3 / 2;
1.71      joris     820:                        tmp = xrealloc(p, (sz + 3) * sizeof(struct line));
1.20      jfb       821:                        p = (struct line *)tmp;
1.1       jfb       822:                }
                    823:                p[++j].value = h;
                    824:        }
1.36      jfb       825:        diff_len[i] = j;
1.1       jfb       826:        file[i] = p;
1.20      jfb       827:
                    828:        return (0);
1.1       jfb       829: }
                    830:
                    831: static void
                    832: prune(void)
                    833: {
                    834:        int i, j;
                    835:
1.36      jfb       836:        for (pref = 0; pref < diff_len[0] && pref < diff_len[1] &&
1.1       jfb       837:            file[0][pref + 1].value == file[1][pref + 1].value;
                    838:            pref++)
                    839:                ;
1.36      jfb       840:        for (suff = 0;
                    841:            (suff < diff_len[0] - pref) && (suff < diff_len[1] - pref) &&
                    842:            (file[0][diff_len[0] - suff].value ==
                    843:            file[1][diff_len[1] - suff].value);
1.1       jfb       844:            suff++)
                    845:                ;
                    846:        for (j = 0; j < 2; j++) {
                    847:                sfile[j] = file[j] + pref;
1.36      jfb       848:                slen[j] = diff_len[j] - pref - suff;
1.1       jfb       849:                for (i = 0; i <= slen[j]; i++)
                    850:                        sfile[j][i].serial = i;
                    851:        }
                    852: }
                    853:
                    854: static void
                    855: equiv(struct line *a, int n, struct line *b, int m, int *c)
                    856: {
                    857:        int i, j;
                    858:
                    859:        i = j = 1;
                    860:        while (i <= n && j <= m) {
                    861:                if (a[i].value < b[j].value)
                    862:                        a[i++].value = 0;
                    863:                else if (a[i].value == b[j].value)
                    864:                        a[i++].value = j;
                    865:                else
                    866:                        j++;
                    867:        }
                    868:        while (i <= n)
                    869:                a[i++].value = 0;
                    870:        b[m + 1].value = 0;
                    871:        j = 0;
                    872:        while (++j <= m) {
                    873:                c[j] = -b[j].serial;
                    874:                while (b[j + 1].value == b[j].value) {
                    875:                        j++;
                    876:                        c[j] = b[j].serial;
                    877:                }
                    878:        }
                    879:        c[j] = -1;
                    880: }
                    881:
                    882: /* Code taken from ping.c */
                    883: static int
                    884: isqrt(int n)
                    885: {
                    886:        int y, x = 1;
                    887:
                    888:        if (n == 0)
1.18      jfb       889:                return (0);
1.1       jfb       890:
                    891:        do { /* newton was a stinker */
                    892:                y = x;
                    893:                x = n / x;
                    894:                x += y;
                    895:                x /= 2;
                    896:        } while ((x - y) > 1 || (x - y) < -1);
                    897:
                    898:        return (x);
                    899: }
                    900:
                    901: static int
                    902: stone(int *a, int n, int *b, int *c)
                    903: {
1.48      joris     904:        int ret;
1.1       jfb       905:        int i, k, y, j, l;
                    906:        int oldc, tc, oldl;
                    907:        u_int numtries;
                    908:
1.18      jfb       909:        /* XXX move the isqrt() out of the macro to avoid multiple calls */
                    910:        const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
1.1       jfb       911:
                    912:        k = 0;
1.48      joris     913:        if ((ret = newcand(0, 0, 0)) < 0)
                    914:                return (-1);
                    915:        c[0] = ret;
1.1       jfb       916:        for (i = 1; i <= n; i++) {
                    917:                j = a[i];
                    918:                if (j == 0)
                    919:                        continue;
                    920:                y = -b[j];
                    921:                oldl = 0;
                    922:                oldc = c[0];
                    923:                numtries = 0;
                    924:                do {
                    925:                        if (y <= clist[oldc].y)
                    926:                                continue;
                    927:                        l = search(c, k, y);
                    928:                        if (l != oldl + 1)
                    929:                                oldc = c[l - 1];
                    930:                        if (l <= k) {
                    931:                                if (clist[c[l]].y <= y)
                    932:                                        continue;
                    933:                                tc = c[l];
1.48      joris     934:                                if ((ret = newcand(i, y, oldc)) < 0)
                    935:                                        return (-1);
                    936:                                c[l] = ret;
1.1       jfb       937:                                oldc = tc;
                    938:                                oldl = l;
                    939:                                numtries++;
                    940:                        } else {
1.48      joris     941:                                if ((ret = newcand(i, y, oldc)) < 0)
                    942:                                        return (-1);
                    943:                                c[l] = ret;
1.1       jfb       944:                                k++;
                    945:                                break;
                    946:                        }
                    947:                } while ((y = b[++j]) > 0 && numtries < bound);
                    948:        }
                    949:        return (k);
                    950: }
                    951:
                    952: static int
                    953: newcand(int x, int y, int pred)
                    954: {
1.49      niallo    955:        struct cand *q, *tmp;
                    956:        int newclistlen;
1.1       jfb       957:
                    958:        if (clen == clistlen) {
1.49      niallo    959:                newclistlen = clistlen * 11 / 10;
1.71      joris     960:                tmp = xrealloc(clist, newclistlen * sizeof(cand));
1.49      niallo    961:                clist = tmp;
                    962:                clistlen = newclistlen;
1.1       jfb       963:        }
                    964:        q = clist + clen;
                    965:        q->x = x;
                    966:        q->y = y;
                    967:        q->pred = pred;
                    968:        return (clen++);
                    969: }
                    970:
                    971: static int
                    972: search(int *c, int k, int y)
                    973: {
                    974:        int i, j, l, t;
                    975:
                    976:        if (clist[c[k]].y < y)  /* quick look for typical case */
                    977:                return (k + 1);
                    978:        i = 0;
                    979:        j = k + 1;
                    980:        while (1) {
                    981:                l = i + j;
                    982:                if ((l >>= 1) <= i)
                    983:                        break;
                    984:                t = clist[c[l]].y;
                    985:                if (t > y)
                    986:                        j = l;
                    987:                else if (t < y)
                    988:                        i = l;
                    989:                else
                    990:                        return (l);
                    991:        }
                    992:        return (l + 1);
                    993: }
                    994:
                    995: static void
                    996: unravel(int p)
                    997: {
                    998:        struct cand *q;
                    999:        int i;
                   1000:
1.36      jfb      1001:        for (i = 0; i <= diff_len[0]; i++)
1.1       jfb      1002:                J[i] = i <= pref ? i :
1.36      jfb      1003:                    i > diff_len[0] - suff ? i + diff_len[1] - diff_len[0] : 0;
1.1       jfb      1004:        for (q = clist + p; q->y != 0; q = clist + q->pred)
                   1005:                J[q->x + pref] = q->y + pref;
                   1006: }
                   1007:
                   1008: /*
                   1009:  * Check does double duty:
                   1010:  *  1. ferret out any fortuitous correspondences due
                   1011:  *     to confounding by hashing (which result in "jackpot")
                   1012:  *  2.  collect random access indexes to the two files
                   1013:  */
                   1014: static void
                   1015: check(FILE *f1, FILE *f2)
                   1016: {
                   1017:        int i, j, jackpot, c, d;
                   1018:        long ctold, ctnew;
                   1019:
                   1020:        rewind(f1);
                   1021:        rewind(f2);
                   1022:        j = 1;
                   1023:        ixold[0] = ixnew[0] = 0;
                   1024:        jackpot = 0;
                   1025:        ctold = ctnew = 0;
1.36      jfb      1026:        for (i = 1; i <= diff_len[0]; i++) {
1.1       jfb      1027:                if (J[i] == 0) {
                   1028:                        ixold[i] = ctold += skipline(f1);
                   1029:                        continue;
                   1030:                }
                   1031:                while (j < J[i]) {
                   1032:                        ixnew[j] = ctnew += skipline(f2);
                   1033:                        j++;
                   1034:                }
1.64      xsa      1035:                if ((bflag == 1)|| (wflag == 1) || (iflag == 1)) {
1.1       jfb      1036:                        for (;;) {
                   1037:                                c = getc(f1);
                   1038:                                d = getc(f2);
                   1039:                                /*
                   1040:                                 * GNU diff ignores a missing newline
                   1041:                                 * in one file if bflag || wflag.
                   1042:                                 */
1.64      xsa      1043:                                if (((bflag == 1) || (wflag == 1)) &&
1.1       jfb      1044:                                    ((c == EOF && d == '\n') ||
                   1045:                                    (c == '\n' && d == EOF))) {
                   1046:                                        break;
                   1047:                                }
                   1048:                                ctold++;
                   1049:                                ctnew++;
1.64      xsa      1050:                                if ((bflag == 1) && isspace(c) && isspace(d)) {
1.1       jfb      1051:                                        do {
                   1052:                                                if (c == '\n')
                   1053:                                                        break;
                   1054:                                                ctold++;
                   1055:                                        } while (isspace(c = getc(f1)));
                   1056:                                        do {
                   1057:                                                if (d == '\n')
                   1058:                                                        break;
                   1059:                                                ctnew++;
                   1060:                                        } while (isspace(d = getc(f2)));
1.64      xsa      1061:                                } else if (wflag == 1) {
1.1       jfb      1062:                                        while (isspace(c) && c != '\n') {
                   1063:                                                c = getc(f1);
                   1064:                                                ctold++;
                   1065:                                        }
                   1066:                                        while (isspace(d) && d != '\n') {
                   1067:                                                d = getc(f2);
                   1068:                                                ctnew++;
                   1069:                                        }
                   1070:                                }
                   1071:                                if (chrtran[c] != chrtran[d]) {
                   1072:                                        jackpot++;
                   1073:                                        J[i] = 0;
1.65      xsa      1074:                                        if ((c != '\n') && (c != EOF))
1.1       jfb      1075:                                                ctold += skipline(f1);
1.65      xsa      1076:                                        if ((d != '\n') && (c != EOF))
1.1       jfb      1077:                                                ctnew += skipline(f2);
                   1078:                                        break;
                   1079:                                }
1.65      xsa      1080:                                if ((c == '\n') || (c == EOF))
1.1       jfb      1081:                                        break;
                   1082:                        }
                   1083:                } else {
                   1084:                        for (;;) {
                   1085:                                ctold++;
                   1086:                                ctnew++;
                   1087:                                if ((c = getc(f1)) != (d = getc(f2))) {
                   1088:                                        /* jackpot++; */
                   1089:                                        J[i] = 0;
1.65      xsa      1090:                                        if ((c != '\n') && (c != EOF))
1.1       jfb      1091:                                                ctold += skipline(f1);
1.65      xsa      1092:                                        if ((d != '\n') && (c != EOF))
1.1       jfb      1093:                                                ctnew += skipline(f2);
                   1094:                                        break;
                   1095:                                }
1.65      xsa      1096:                                if ((c == '\n') || (c == EOF))
1.1       jfb      1097:                                        break;
                   1098:                        }
                   1099:                }
                   1100:                ixold[i] = ctold;
                   1101:                ixnew[j] = ctnew;
                   1102:                j++;
                   1103:        }
1.36      jfb      1104:        for (; j <= diff_len[1]; j++)
1.1       jfb      1105:                ixnew[j] = ctnew += skipline(f2);
                   1106:        /*
1.65      xsa      1107:         * if (jackpot != 0)
1.42      joris    1108:         *      cvs_printf("jackpot\n");
1.1       jfb      1109:         */
                   1110: }
                   1111:
                   1112: /* shellsort CACM #201 */
                   1113: static void
                   1114: sort(struct line *a, int n)
                   1115: {
                   1116:        struct line *ai, *aim, w;
                   1117:        int j, m = 0, k;
                   1118:
                   1119:        if (n == 0)
                   1120:                return;
                   1121:        for (j = 1; j <= n; j *= 2)
                   1122:                m = 2 * j - 1;
                   1123:        for (m /= 2; m != 0; m /= 2) {
                   1124:                k = n - m;
                   1125:                for (j = 1; j <= k; j++) {
                   1126:                        for (ai = &a[j]; ai > a; ai -= m) {
                   1127:                                aim = &ai[m];
                   1128:                                if (aim < ai)
                   1129:                                        break;  /* wraparound */
                   1130:                                if (aim->value > ai[0].value ||
                   1131:                                    (aim->value == ai[0].value &&
                   1132:                                        aim->serial > ai[0].serial))
                   1133:                                        break;
                   1134:                                w.value = ai[0].value;
                   1135:                                ai[0].value = aim->value;
                   1136:                                aim->value = w.value;
                   1137:                                w.serial = ai[0].serial;
                   1138:                                ai[0].serial = aim->serial;
                   1139:                                aim->serial = w.serial;
                   1140:                        }
                   1141:                }
                   1142:        }
                   1143: }
                   1144:
                   1145: static void
                   1146: unsort(struct line *f, int l, int *b)
                   1147: {
                   1148:        int *a, i;
                   1149:
1.71      joris    1150:        a = (int *)xmalloc((l + 1) * sizeof(int));
1.1       jfb      1151:        for (i = 1; i <= l; i++)
                   1152:                a[f[i].serial] = f[i].value;
                   1153:        for (i = 1; i <= l; i++)
                   1154:                b[i] = a[i];
1.71      joris    1155:        xfree(a);
1.1       jfb      1156: }
                   1157:
                   1158: static int
                   1159: skipline(FILE *f)
                   1160: {
                   1161:        int i, c;
                   1162:
                   1163:        for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
                   1164:                continue;
                   1165:        return (i);
                   1166: }
                   1167:
                   1168: static void
                   1169: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
                   1170: {
                   1171:        int m, i0, i1, j0, j1;
                   1172:
                   1173:        rewind(f1);
                   1174:        rewind(f2);
1.36      jfb      1175:        m = diff_len[0];
1.1       jfb      1176:        J[0] = 0;
1.36      jfb      1177:        J[m + 1] = diff_len[1] + 1;
1.1       jfb      1178:        for (i0 = 1; i0 <= m; i0 = i1 + 1) {
                   1179:                while (i0 <= m && J[i0] == J[i0 - 1] + 1)
                   1180:                        i0++;
                   1181:                j0 = J[i0 - 1] + 1;
                   1182:                i1 = i0 - 1;
                   1183:                while (i1 < m && J[i1 + 1] == 0)
                   1184:                        i1++;
                   1185:                j1 = J[i1 + 1] - 1;
                   1186:                J[i1] = j1;
                   1187:                change(file1, f1, file2, f2, i0, i1, j0, j1);
                   1188:        }
                   1189:        if (m == 0)
1.36      jfb      1190:                change(file1, f1, file2, f2, 1, 0, 1, diff_len[1]);
1.58      niallo   1191:        if (diff_format == D_IFDEF) {
1.1       jfb      1192:                for (;;) {
                   1193: #define        c i0
                   1194:                        if ((c = getc(f1)) == EOF)
                   1195:                                return;
1.58      niallo   1196:                        diff_output("%c", c);
1.1       jfb      1197:                }
                   1198: #undef c
                   1199:        }
                   1200:        if (anychange != 0) {
1.58      niallo   1201:                if (diff_format == D_CONTEXT)
1.1       jfb      1202:                        dump_context_vec(f1, f2);
1.58      niallo   1203:                else if (diff_format == D_UNIFIED)
1.1       jfb      1204:                        dump_unified_vec(f1, f2);
                   1205:        }
                   1206: }
                   1207:
                   1208: static __inline void
                   1209: range(int a, int b, char *separator)
                   1210: {
1.58      niallo   1211:        diff_output("%d", a > b ? b : a);
1.1       jfb      1212:        if (a < b)
1.58      niallo   1213:                diff_output("%s%d", separator, b);
1.1       jfb      1214: }
                   1215:
                   1216: static __inline void
                   1217: uni_range(int a, int b)
                   1218: {
                   1219:        if (a < b)
1.58      niallo   1220:                diff_output("%d,%d", a, b - a + 1);
1.1       jfb      1221:        else if (a == b)
1.58      niallo   1222:                diff_output("%d", b);
1.1       jfb      1223:        else
1.58      niallo   1224:                diff_output("%d,0", b);
1.1       jfb      1225: }
                   1226:
                   1227: static char *
1.15      jfb      1228: preadline(int fd, size_t rlen, off_t off)
1.1       jfb      1229: {
                   1230:        char *line;
                   1231:        ssize_t nr;
                   1232:
1.71      joris    1233:        line = xmalloc(rlen + 1);
1.15      jfb      1234:        if ((nr = pread(fd, line, rlen, off)) < 0) {
1.1       jfb      1235:                cvs_log(LP_ERRNO, "preadline failed");
                   1236:                return (NULL);
                   1237:        }
                   1238:        line[nr] = '\0';
                   1239:        return (line);
                   1240: }
                   1241:
                   1242: static int
                   1243: ignoreline(char *line)
                   1244: {
                   1245:        int ret;
                   1246:
1.55      xsa      1247:        ret = regexec(&ignore_re, line, (size_t)0, NULL, 0);
1.71      joris    1248:        xfree(line);
1.1       jfb      1249:        return (ret == 0);      /* if it matched, it should be ignored. */
                   1250: }
                   1251:
                   1252: /*
                   1253:  * Indicate that there is a difference between lines a and b of the from file
                   1254:  * to get to lines c to d of the to file.  If a is greater then b then there
                   1255:  * are no lines in the from file involved and this means that there were
                   1256:  * lines appended (beginning at b).  If c is greater than d then there are
                   1257:  * lines missing from the to file.
                   1258:  */
                   1259: static void
                   1260: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
                   1261:        int a, int b, int c, int d)
                   1262: {
                   1263:        static size_t max_context = 64;
                   1264:        int i;
                   1265:
1.58      niallo   1266:        if (diff_format != D_IFDEF && a > b && c > d)
1.1       jfb      1267:                return;
                   1268:        if (ignore_pats != NULL) {
                   1269:                char *line;
                   1270:                /*
                   1271:                 * All lines in the change, insert, or delete must
                   1272:                 * match an ignore pattern for the change to be
                   1273:                 * ignored.
                   1274:                 */
                   1275:                if (a <= b) {           /* Changes and deletes. */
                   1276:                        for (i = a; i <= b; i++) {
                   1277:                                line = preadline(fileno(f1),
                   1278:                                    ixold[i] - ixold[i - 1], ixold[i - 1]);
                   1279:                                if (!ignoreline(line))
                   1280:                                        goto proceed;
                   1281:                        }
                   1282:                }
1.65      xsa      1283:                if ((a > b) || (c <= d)) {      /* Changes and inserts. */
1.1       jfb      1284:                        for (i = c; i <= d; i++) {
                   1285:                                line = preadline(fileno(f2),
                   1286:                                    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
                   1287:                                if (!ignoreline(line))
                   1288:                                        goto proceed;
                   1289:                        }
                   1290:                }
                   1291:                return;
                   1292:        }
                   1293: proceed:
1.58      niallo   1294:        if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1.1       jfb      1295:                /*
                   1296:                 * Allocate change records as needed.
                   1297:                 */
                   1298:                if (context_vec_ptr == context_vec_end - 1) {
1.49      niallo   1299:                        struct context_vec *tmp;
1.1       jfb      1300:                        ptrdiff_t offset = context_vec_ptr - context_vec_start;
                   1301:                        max_context <<= 1;
1.71      joris    1302:                        tmp = xrealloc(context_vec_start, max_context *
                   1303:                            sizeof(struct context_vec));
1.49      niallo   1304:                        context_vec_start = tmp;
1.1       jfb      1305:                        context_vec_end = context_vec_start + max_context;
                   1306:                        context_vec_ptr = context_vec_start + offset;
                   1307:                }
                   1308:                if (anychange == 0) {
                   1309:                        /*
                   1310:                         * Print the context/unidiff header first time through.
                   1311:                         */
1.58      niallo   1312:                        diff_output("%s %s      %s",
                   1313:                            diff_format == D_CONTEXT ? "***" : "---", diff_file,
1.1       jfb      1314:                            ctime(&stb1.st_mtime));
1.58      niallo   1315:                        diff_output("%s %s      %s",
                   1316:                            diff_format == D_CONTEXT ? "---" : "+++", diff_file,
1.1       jfb      1317:                            ctime(&stb2.st_mtime));
                   1318:                        anychange = 1;
                   1319:                } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
                   1320:                    c > context_vec_ptr->d + (2 * context) + 1) {
                   1321:                        /*
                   1322:                         * If this change is more than 'context' lines from the
                   1323:                         * previous change, dump the record and reset it.
                   1324:                         */
1.58      niallo   1325:                        if (diff_format == D_CONTEXT)
1.1       jfb      1326:                                dump_context_vec(f1, f2);
                   1327:                        else
                   1328:                                dump_unified_vec(f1, f2);
                   1329:                }
                   1330:                context_vec_ptr++;
                   1331:                context_vec_ptr->a = a;
                   1332:                context_vec_ptr->b = b;
                   1333:                context_vec_ptr->c = c;
                   1334:                context_vec_ptr->d = d;
                   1335:                return;
                   1336:        }
                   1337:        if (anychange == 0)
                   1338:                anychange = 1;
1.58      niallo   1339:        switch (diff_format) {
1.1       jfb      1340:        case D_BRIEF:
                   1341:                return;
                   1342:        case D_NORMAL:
                   1343:                range(a, b, ",");
1.58      niallo   1344:                diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
                   1345:                if (diff_format == D_NORMAL)
1.1       jfb      1346:                        range(c, d, ",");
1.58      niallo   1347:                diff_output("\n");
1.1       jfb      1348:                break;
1.33      jfb      1349:        case D_RCSDIFF:
                   1350:                if (a > b)
1.58      niallo   1351:                        diff_output("a%d %d\n", b, d - c + 1);
1.33      jfb      1352:                else {
1.58      niallo   1353:                        diff_output("d%d %d\n", a, b - a + 1);
1.33      jfb      1354:
                   1355:                        if (!(c > d))   /* add changed lines */
1.58      niallo   1356:                                diff_output("a%d %d\n", b, d - c + 1);
1.33      jfb      1357:                }
                   1358:                break;
1.1       jfb      1359:        }
1.58      niallo   1360:        if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1.1       jfb      1361:                fetch(ixold, a, b, f1, '<', 1);
1.58      niallo   1362:                if (a <= b && c <= d && diff_format == D_NORMAL)
1.60      joris    1363:                        diff_output("---\n");
1.1       jfb      1364:        }
1.58      niallo   1365:        i = fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
1.1       jfb      1366:        if (inifdef) {
1.58      niallo   1367:                diff_output("#endif /* %s */\n", ifdefname);
1.1       jfb      1368:                inifdef = 0;
                   1369:        }
                   1370: }
                   1371:
                   1372: static int
                   1373: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
                   1374: {
                   1375:        int i, j, c, lastc, col, nc;
                   1376:
                   1377:        /*
                   1378:         * When doing #ifdef's, copy down to current line
                   1379:         * if this is the first file, so that stuff makes it to output.
                   1380:         */
1.58      niallo   1381:        if (diff_format == D_IFDEF && oldfile) {
1.1       jfb      1382:                long curpos = ftell(lb);
                   1383:                /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
                   1384:                nc = f[a > b ? b : a - 1] - curpos;
                   1385:                for (i = 0; i < nc; i++)
1.58      niallo   1386:                        diff_output("%c", getc(lb));
1.1       jfb      1387:        }
                   1388:        if (a > b)
                   1389:                return (0);
1.58      niallo   1390:        if (diff_format == D_IFDEF) {
1.1       jfb      1391:                if (inifdef) {
1.58      niallo   1392:                        diff_output("#else /* %s%s */\n",
1.1       jfb      1393:                            oldfile == 1 ? "!" : "", ifdefname);
                   1394:                } else {
                   1395:                        if (oldfile)
1.58      niallo   1396:                                diff_output("#ifndef %s\n", ifdefname);
1.1       jfb      1397:                        else
1.58      niallo   1398:                                diff_output("#ifdef %s\n", ifdefname);
1.1       jfb      1399:                }
                   1400:                inifdef = 1 + oldfile;
                   1401:        }
                   1402:        for (i = a; i <= b; i++) {
                   1403:                fseek(lb, f[i - 1], SEEK_SET);
                   1404:                nc = f[i] - f[i - 1];
1.58      niallo   1405:                if (diff_format != D_IFDEF && ch != '\0') {
                   1406:                        diff_output("%c", ch);
1.64      xsa      1407:                        if ((Tflag == 1 ) && (diff_format == D_NORMAL ||
1.59      joris    1408:                            diff_format == D_CONTEXT ||
                   1409:                            diff_format == D_UNIFIED))
1.58      niallo   1410:                                diff_output("\t");
                   1411:                        else if (diff_format != D_UNIFIED)
                   1412:                                diff_output(" ");
1.1       jfb      1413:                }
                   1414:                col = 0;
                   1415:                for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
                   1416:                        if ((c = getc(lb)) == EOF) {
1.58      niallo   1417:                                if (diff_format == D_RCSDIFF)
1.76      xsa      1418:                                        cvs_log(LP_WARN,
                   1419:                                            "No newline at end of file");
1.33      jfb      1420:                                else
1.59      joris    1421:                                        diff_output("\n\\ No newline at end of "
                   1422:                                            "file");
1.1       jfb      1423:                                return (0);
                   1424:                        }
1.64      xsa      1425:                        if ((c == '\t') && (tflag == 1)) {
1.1       jfb      1426:                                do {
1.58      niallo   1427:                                        diff_output(" ");
1.1       jfb      1428:                                } while (++col & 7);
                   1429:                        } else {
1.58      niallo   1430:                                diff_output("%c", c);
1.1       jfb      1431:                                col++;
                   1432:                        }
                   1433:                }
                   1434:        }
                   1435:        return (0);
                   1436: }
                   1437:
                   1438: /*
                   1439:  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
                   1440:  */
                   1441: static int
                   1442: readhash(FILE *f)
                   1443: {
                   1444:        int i, t, space;
                   1445:        int sum;
                   1446:
                   1447:        sum = 1;
                   1448:        space = 0;
1.64      xsa      1449:        if ((bflag != 1) && (wflag != 1)) {
                   1450:                if (iflag == 1)
1.1       jfb      1451:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1452:                                if (t == EOF) {
                   1453:                                        if (i == 0)
                   1454:                                                return (0);
                   1455:                                        break;
                   1456:                                }
                   1457:                                sum = sum * 127 + chrtran[t];
                   1458:                        }
                   1459:                else
                   1460:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1461:                                if (t == EOF) {
                   1462:                                        if (i == 0)
                   1463:                                                return (0);
                   1464:                                        break;
                   1465:                                }
                   1466:                                sum = sum * 127 + t;
                   1467:                        }
                   1468:        } else {
                   1469:                for (i = 0;;) {
                   1470:                        switch (t = getc(f)) {
                   1471:                        case '\t':
                   1472:                        case ' ':
                   1473:                                space++;
                   1474:                                continue;
                   1475:                        default:
1.64      xsa      1476:                                if ((space != 0) && (wflag != 1)) {
1.1       jfb      1477:                                        i++;
                   1478:                                        space = 0;
                   1479:                                }
                   1480:                                sum = sum * 127 + chrtran[t];
                   1481:                                i++;
                   1482:                                continue;
                   1483:                        case EOF:
                   1484:                                if (i == 0)
                   1485:                                        return (0);
                   1486:                                /* FALLTHROUGH */
                   1487:                        case '\n':
                   1488:                                break;
                   1489:                        }
                   1490:                        break;
                   1491:                }
                   1492:        }
                   1493:        /*
                   1494:         * There is a remote possibility that we end up with a zero sum.
                   1495:         * Zero is used as an EOF marker, so return 1 instead.
                   1496:         */
                   1497:        return (sum == 0 ? 1 : sum);
                   1498: }
                   1499:
                   1500: static int
                   1501: asciifile(FILE *f)
                   1502: {
                   1503:        char buf[BUFSIZ];
                   1504:        int i, cnt;
                   1505:
1.64      xsa      1506:        if ((aflag == 1) || (f == NULL))
1.1       jfb      1507:                return (1);
                   1508:
                   1509:        rewind(f);
1.55      xsa      1510:        cnt = fread(buf, (size_t)1, sizeof(buf), f);
1.1       jfb      1511:        for (i = 0; i < cnt; i++)
                   1512:                if (!isprint(buf[i]) && !isspace(buf[i]))
                   1513:                        return (0);
                   1514:        return (1);
                   1515: }
                   1516:
1.17      jfb      1517: static char*
                   1518: match_function(const long *f, int pos, FILE *fp)
                   1519: {
                   1520:        unsigned char buf[FUNCTION_CONTEXT_SIZE];
                   1521:        size_t nc;
                   1522:        int last = lastline;
                   1523:        char *p;
                   1524:
                   1525:        lastline = pos;
                   1526:        while (pos > last) {
                   1527:                fseek(fp, f[pos - 1], SEEK_SET);
                   1528:                nc = f[pos] - f[pos - 1];
                   1529:                if (nc >= sizeof(buf))
                   1530:                        nc = sizeof(buf) - 1;
1.55      xsa      1531:                nc = fread(buf, (size_t)1, nc, fp);
1.17      jfb      1532:                if (nc > 0) {
                   1533:                        buf[nc] = '\0';
1.44      niallo   1534:                        p = strchr((const char *)buf, '\n');
1.17      jfb      1535:                        if (p != NULL)
                   1536:                                *p = '\0';
                   1537:                        if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.57      reyk     1538:                                strlcpy(lastbuf, (const char *)buf,
                   1539:                                    sizeof lastbuf);
1.17      jfb      1540:                                lastmatchline = pos;
                   1541:                                return lastbuf;
                   1542:                        }
                   1543:                }
                   1544:                pos--;
                   1545:        }
                   1546:        return (lastmatchline > 0) ? lastbuf : NULL;
                   1547: }
                   1548:
1.1       jfb      1549:
                   1550: /* dump accumulated "context" diff changes */
                   1551: static void
                   1552: dump_context_vec(FILE *f1, FILE *f2)
                   1553: {
                   1554:        struct context_vec *cvp = context_vec_start;
                   1555:        int lowa, upb, lowc, upd, do_output;
                   1556:        int a, b, c, d;
1.17      jfb      1557:        char ch, *f;
1.1       jfb      1558:
                   1559:        if (context_vec_start > context_vec_ptr)
                   1560:                return;
                   1561:
                   1562:        b = d = 0;              /* gcc */
1.4       jfb      1563:        lowa = MAX(1, cvp->a - context);
1.36      jfb      1564:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4       jfb      1565:        lowc = MAX(1, cvp->c - context);
1.36      jfb      1566:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1       jfb      1567:
1.58      niallo   1568:        diff_output("***************");
1.64      xsa      1569:        if (pflag == 1) {
1.17      jfb      1570:                f = match_function(ixold, lowa - 1, f1);
                   1571:                if (f != NULL) {
1.58      niallo   1572:                        diff_output(" ");
                   1573:                        diff_output("%s", f);
1.17      jfb      1574:                }
                   1575:        }
1.58      niallo   1576:        diff_output("\n*** ");
1.1       jfb      1577:        range(lowa, upb, ",");
1.58      niallo   1578:        diff_output(" ****\n");
1.1       jfb      1579:
                   1580:        /*
                   1581:         * Output changes to the "old" file.  The first loop suppresses
                   1582:         * output if there were no changes to the "old" file (we'll see
                   1583:         * the "old" lines as context in the "new" list).
                   1584:         */
                   1585:        do_output = 0;
                   1586:        for (; cvp <= context_vec_ptr; cvp++)
                   1587:                if (cvp->a <= cvp->b) {
                   1588:                        cvp = context_vec_start;
                   1589:                        do_output++;
                   1590:                        break;
                   1591:                }
1.65      xsa      1592:        if (do_output != 0) {
1.1       jfb      1593:                while (cvp <= context_vec_ptr) {
                   1594:                        a = cvp->a;
                   1595:                        b = cvp->b;
                   1596:                        c = cvp->c;
                   1597:                        d = cvp->d;
                   1598:
                   1599:                        if (a <= b && c <= d)
                   1600:                                ch = 'c';
                   1601:                        else
                   1602:                                ch = (a <= b) ? 'd' : 'a';
                   1603:
                   1604:                        if (ch == 'a')
                   1605:                                fetch(ixold, lowa, b, f1, ' ', 0);
                   1606:                        else {
                   1607:                                fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1608:                                fetch(ixold, a, b, f1,
                   1609:                                    ch == 'c' ? '!' : '-', 0);
                   1610:                        }
                   1611:                        lowa = b + 1;
                   1612:                        cvp++;
                   1613:                }
                   1614:                fetch(ixold, b + 1, upb, f1, ' ', 0);
                   1615:        }
                   1616:        /* output changes to the "new" file */
1.58      niallo   1617:        diff_output("--- ");
1.1       jfb      1618:        range(lowc, upd, ",");
1.58      niallo   1619:        diff_output(" ----\n");
1.1       jfb      1620:
                   1621:        do_output = 0;
                   1622:        for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
                   1623:                if (cvp->c <= cvp->d) {
                   1624:                        cvp = context_vec_start;
                   1625:                        do_output++;
                   1626:                        break;
                   1627:                }
1.65      xsa      1628:        if (do_output != 0) {
1.1       jfb      1629:                while (cvp <= context_vec_ptr) {
                   1630:                        a = cvp->a;
                   1631:                        b = cvp->b;
                   1632:                        c = cvp->c;
                   1633:                        d = cvp->d;
                   1634:
                   1635:                        if (a <= b && c <= d)
                   1636:                                ch = 'c';
                   1637:                        else
                   1638:                                ch = (a <= b) ? 'd' : 'a';
                   1639:
                   1640:                        if (ch == 'd')
                   1641:                                fetch(ixnew, lowc, d, f2, ' ', 0);
                   1642:                        else {
                   1643:                                fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1644:                                fetch(ixnew, c, d, f2,
                   1645:                                    ch == 'c' ? '!' : '+', 0);
                   1646:                        }
                   1647:                        lowc = d + 1;
                   1648:                        cvp++;
                   1649:                }
                   1650:                fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1651:        }
                   1652:        context_vec_ptr = context_vec_start - 1;
                   1653: }
                   1654:
                   1655: /* dump accumulated "unified" diff changes */
                   1656: static void
                   1657: dump_unified_vec(FILE *f1, FILE *f2)
                   1658: {
                   1659:        struct context_vec *cvp = context_vec_start;
                   1660:        int lowa, upb, lowc, upd;
                   1661:        int a, b, c, d;
1.17      jfb      1662:        char ch, *f;
1.1       jfb      1663:
                   1664:        if (context_vec_start > context_vec_ptr)
                   1665:                return;
                   1666:
                   1667:        b = d = 0;              /* gcc */
1.4       jfb      1668:        lowa = MAX(1, cvp->a - context);
1.36      jfb      1669:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4       jfb      1670:        lowc = MAX(1, cvp->c - context);
1.36      jfb      1671:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1       jfb      1672:
1.58      niallo   1673:        diff_output("@@ -");
1.1       jfb      1674:        uni_range(lowa, upb);
1.58      niallo   1675:        diff_output(" +");
1.1       jfb      1676:        uni_range(lowc, upd);
1.58      niallo   1677:        diff_output(" @@");
1.64      xsa      1678:        if (pflag == 1) {
1.17      jfb      1679:                f = match_function(ixold, lowa - 1, f1);
                   1680:                if (f != NULL) {
1.58      niallo   1681:                        diff_output(" ");
                   1682:                        diff_output("%s", f);
1.17      jfb      1683:                }
                   1684:        }
1.58      niallo   1685:        diff_output("\n");
1.1       jfb      1686:
                   1687:        /*
                   1688:         * Output changes in "unified" diff format--the old and new lines
                   1689:         * are printed together.
                   1690:         */
                   1691:        for (; cvp <= context_vec_ptr; cvp++) {
                   1692:                a = cvp->a;
                   1693:                b = cvp->b;
                   1694:                c = cvp->c;
                   1695:                d = cvp->d;
                   1696:
                   1697:                /*
                   1698:                 * c: both new and old changes
                   1699:                 * d: only changes in the old file
                   1700:                 * a: only changes in the new file
                   1701:                 */
                   1702:                if (a <= b && c <= d)
                   1703:                        ch = 'c';
                   1704:                else
                   1705:                        ch = (a <= b) ? 'd' : 'a';
                   1706:
                   1707:                switch (ch) {
                   1708:                case 'c':
                   1709:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1710:                        fetch(ixold, a, b, f1, '-', 0);
                   1711:                        fetch(ixnew, c, d, f2, '+', 0);
                   1712:                        break;
                   1713:                case 'd':
                   1714:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1715:                        fetch(ixold, a, b, f1, '-', 0);
                   1716:                        break;
                   1717:                case 'a':
                   1718:                        fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1719:                        fetch(ixnew, c, d, f2, '+', 0);
                   1720:                        break;
                   1721:                }
                   1722:                lowa = b + 1;
                   1723:                lowc = d + 1;
                   1724:        }
                   1725:        fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1726:
                   1727:        context_vec_ptr = context_vec_start - 1;
1.58      niallo   1728: }
                   1729:
1.63      joris    1730: void
1.58      niallo   1731: diff_output(const char *fmt, ...)
                   1732: {
                   1733:        va_list vap;
                   1734:        char *str;
                   1735:
                   1736:        va_start(vap, fmt);
                   1737:        vasprintf(&str, fmt, vap);
                   1738:        if (diffbuf != NULL)
                   1739:                cvs_buf_append(diffbuf, str, strlen(str));
                   1740:        else
                   1741:                cvs_printf("%s", str);
1.71      joris    1742:        xfree(str);
1.58      niallo   1743:        va_end(vap);
1.1       jfb      1744: }