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