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