1 /* $OpenBSD: diffreg.c,v 1.93 2019/06/28 13:35:00 deraadt Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (C) Caldera International Inc. 2001-2002.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code and documentation must retain the above
13 * copyright notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed or owned by Caldera
21 * 4. Neither the name of Caldera International, Inc. nor the names of other
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
26 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
30 * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)diffreg.c 8.1 (Berkeley) 6/6/93
69 #include <sys/cdefs.h>
70 #include <sys/capsicum.h>
73 #include <capsicum_helpers.h>
93 * diff - compare two files.
97 * Uses an algorithm due to Harold Stone, which finds a pair of longest
98 * identical subsequences in the two files.
100 * The major goal is to generate the match vector J. J[i] is the index of
101 * the line in file1 corresponding to line i file0. J[i] = 0 if there is no
102 * such line in file1.
104 * Lines are hashed so as to work in core. All potential matches are
105 * located by sorting the lines of each file on the hash (called
106 * ``value''). In particular, this collects the equivalence classes in
107 * file1 together. Subroutine equiv replaces the value of each line in
108 * file0 by the index of the first element of its matching equivalence in
109 * (the reordered) file1. To save space equiv squeezes file1 into a single
110 * array member in which the equivalence classes are simply concatenated,
111 * except that their first members are flagged by changing sign.
113 * Next the indices that point into member are unsorted into array class
114 * according to the original order of file0.
116 * The cleverness lies in routine stone. This marches through the lines of
117 * file0, developing a vector klist of "k-candidates". At step i
118 * a k-candidate is a matched pair of lines x,y (x in file0 y in file1)
119 * such that there is a common subsequence of length k between the first
120 * i lines of file0 and the first y lines of file1, but there is no such
121 * subsequence for any smaller y. x is the earliest possible mate to y that
122 * occurs in such a subsequence.
124 * Whenever any of the members of the equivalence class of lines in file1
125 * matable to a line in file0 has serial number less than the y of some
126 * k-candidate, that k-candidate with the smallest such y is replaced. The
127 * new k-candidate is chained (via pred) to the current k-1 candidate so
128 * that the actual subsequence can be recovered. When a member has serial
129 * number greater that the y of all k-candidates, the klist is extended. At
130 * the end, the longest subsequence is pulled out and placed in the array J
133 * With J in hand, the matches there recorded are check'ed against reality
134 * to assure that no spurious matches have crept in due to hashing. If they
135 * have, they are broken, and "jackpot" is recorded -- a harmless matter
136 * except that a true match for a spuriously mated line may now be
137 * unnecessarily reported as a change.
139 * Much of the complexity of the program comes simply from trying to
140 * minimize core utilization and maximize the range of doable problems by
141 * dynamically allocating what is needed and reusing what is not. The core
142 * requirements for problems larger than somewhat are (in words)
143 * 2*length(file0) + length(file1) + 3*(number of k-candidates installed),
144 * typically about 6n words for files of length n.
159 * The following struct is used to record change information when
160 * doing a "context" or "unified" diff. (see routine "change" to
161 * understand the highly mnemonic field names)
164 int a; /* start line in old file */
165 int b; /* end line in old file */
166 int c; /* start line in new file */
167 int d; /* end line in new file */
170 enum readhash { RH_BINARY, RH_OK, RH_EOF };
173 static FILE *opentemp(const char *);
174 static void output(char *, FILE *, char *, FILE *, int);
175 static void check(FILE *, FILE *, int);
176 static void range(int, int, const char *);
177 static void uni_range(int, int);
178 static void dump_context_vec(FILE *, FILE *, int);
179 static void dump_unified_vec(FILE *, FILE *, int);
180 static bool prepare(int, FILE *, size_t, int);
181 static void prune(void);
182 static void equiv(struct line *, int, struct line *, int, int *);
183 static void unravel(int);
184 static void unsort(struct line *, int, int *);
185 static void change(char *, FILE *, char *, FILE *, int, int, int, int, int *);
186 static void sort(struct line *, int);
187 static void print_header(const char *, const char *);
188 static void print_space(int, int, int);
189 static bool ignoreline_pattern(char *);
190 static bool ignoreline(char *, bool);
191 static int asciifile(FILE *);
192 static int fetch(long *, int, int, FILE *, int, int, int);
193 static int newcand(int, int, int);
194 static int search(int *, int, int);
195 static int skipline(FILE *);
196 static int stone(int *, int, int *, int *, int);
197 static enum readhash readhash(FILE *, int, unsigned *);
198 static int files_differ(FILE *, FILE *, int);
199 static char *match_function(const long *, int, FILE *);
200 static char *preadline(int, size_t, off_t);
202 static int *J; /* will be overlaid on class */
203 static int *class; /* will be overlaid on file[0] */
204 static int *klist; /* will be overlaid on file[0] after class */
205 static int *member; /* will be overlaid on file[1] */
207 static int inifdef; /* whether or not we are in a #ifdef block */
209 static int pref, suff; /* length of prefix and suffix */
211 static int anychange;
212 static int hw, padding; /* half width and padding */
214 static long *ixnew; /* will be overlaid on file[1] */
215 static long *ixold; /* will be overlaid on klist */
216 static struct cand *clist; /* merely a free storage pot for candidates */
217 static int clistlen; /* the length of clist */
218 static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
219 static int (*chrtran)(int); /* translation table for case-folding */
220 static struct context_vec *context_vec_start;
221 static struct context_vec *context_vec_end;
222 static struct context_vec *context_vec_ptr;
224 #define FUNCTION_CONTEXT_SIZE 55
225 static char lastbuf[FUNCTION_CONTEXT_SIZE];
227 static int lastmatchline;
244 diffreg(char *file1, char *file2, int flags, int capsicum)
248 struct pr *pr = NULL;
249 cap_rights_t rights_ro;
258 * hw excludes padding and make sure when -t is not used,
259 * the second column always starts from the closest tab stop
261 if (diff_format == D_SIDEBYSIDE) {
263 padding = tabsize - (hw % tabsize);
264 if ((flags & D_EXPANDTABS) != 0 || (padding % tabsize == 0))
268 ((padding == MIN_PAD) ? (padding << 1) : padding) - 1;
272 if (flags & D_IGNORECASE)
276 if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
277 return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2);
278 if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0)
281 if (flags & D_EMPTY1)
282 f1 = fopen(_PATH_DEVNULL, "r");
284 if (!S_ISREG(stb1.st_mode)) {
285 if ((f1 = opentemp(file1)) == NULL ||
286 fstat(fileno(f1), &stb1) == -1) {
292 } else if (strcmp(file1, "-") == 0)
295 f1 = fopen(file1, "r");
304 if (flags & D_EMPTY2)
305 f2 = fopen(_PATH_DEVNULL, "r");
307 if (!S_ISREG(stb2.st_mode)) {
308 if ((f2 = opentemp(file2)) == NULL ||
309 fstat(fileno(f2), &stb2) == -1) {
315 } else if (strcmp(file2, "-") == 0)
318 f2 = fopen(file2, "r");
328 pr = start_pr(file1, file2);
331 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
332 if (caph_rights_limit(fileno(f1), &rights_ro) < 0)
333 err(2, "unable to limit rights on: %s", file1);
334 if (caph_rights_limit(fileno(f2), &rights_ro) < 0)
335 err(2, "unable to limit rights on: %s", file2);
336 if (fileno(f1) == STDIN_FILENO || fileno(f2) == STDIN_FILENO) {
337 /* stdin has already been limited */
338 if (caph_limit_stderr() == -1)
339 err(2, "unable to limit stderr");
340 if (caph_limit_stdout() == -1)
341 err(2, "unable to limit stdout");
342 } else if (caph_limit_stdio() == -1)
343 err(2, "unable to limit stdio");
345 caph_cache_catpages();
347 if (caph_enter() < 0)
348 err(2, "unable to enter capability mode");
351 switch (files_differ(f1, f2, flags)) {
363 if (diff_format == D_BRIEF && ignore_pats == NULL &&
364 (flags & (D_FOLDBLANKS|D_IGNOREBLANKS|D_IGNORECASE|D_STRIPCR)) == 0)
370 if ((flags & D_FORCEASCII) != 0) {
371 (void)prepare(0, f1, stb1.st_size, flags);
372 (void)prepare(1, f2, stb2.st_size, flags);
373 } else if (!asciifile(f1) || !asciifile(f2) ||
374 !prepare(0, f1, stb1.st_size, flags) ||
375 !prepare(1, f2, stb2.st_size, flags)) {
382 sort(sfile[0], slen[0]);
383 sort(sfile[1], slen[1]);
385 member = (int *)file[1];
386 equiv(sfile[0], slen[0], sfile[1], slen[1], member);
387 member = xreallocarray(member, slen[1] + 2, sizeof(*member));
389 class = (int *)file[0];
390 unsort(sfile[0], slen[0], class);
391 class = xreallocarray(class, slen[0] + 2, sizeof(*class));
393 klist = xcalloc(slen[0] + 2, sizeof(*klist));
396 clist = xcalloc(clistlen, sizeof(*clist));
397 i = stone(class, slen[0], member, klist, flags);
401 J = xreallocarray(J, len[0] + 2, sizeof(*J));
406 ixold = xreallocarray(ixold, len[0] + 2, sizeof(*ixold));
407 ixnew = xreallocarray(ixnew, len[1] + 2, sizeof(*ixnew));
408 check(f1, f2, flags);
409 output(file1, f1, file2, f2, flags);
428 * Check to see if the given files differ.
429 * Returns 0 if they are the same, 1 if different, and -1 on error.
430 * XXX - could use code from cmp(1) [faster]
433 files_differ(FILE *f1, FILE *f2, int flags)
435 char buf1[BUFSIZ], buf2[BUFSIZ];
438 if ((flags & (D_EMPTY1|D_EMPTY2)) || stb1.st_size != stb2.st_size ||
439 (stb1.st_mode & S_IFMT) != (stb2.st_mode & S_IFMT))
442 if (stb1.st_dev == stb2.st_dev && stb1.st_ino == stb2.st_ino)
446 i = fread(buf1, 1, sizeof(buf1), f1);
447 j = fread(buf2, 1, sizeof(buf2), f2);
448 if ((!i && ferror(f1)) || (!j && ferror(f2)))
454 if (memcmp(buf1, buf2, i) != 0)
460 opentemp(const char *f)
462 char buf[BUFSIZ], tempfile[PATH_MAX];
466 if (strcmp(f, "-") == 0)
468 else if ((ifd = open(f, O_RDONLY, 0644)) == -1)
471 (void)strlcpy(tempfile, _PATH_TMP "/diff.XXXXXXXX", sizeof(tempfile));
473 if ((ofd = mkstemp(tempfile)) == -1) {
478 while ((nread = read(ifd, buf, BUFSIZ)) > 0) {
479 if (write(ofd, buf, nread) != nread) {
486 lseek(ofd, (off_t)0, SEEK_SET);
487 return (fdopen(ofd, "r"));
491 prepare(int i, FILE *fd, size_t filesize, int flags)
500 sz = MIN(filesize, SIZE_MAX) / 25;
504 p = xcalloc(sz + 3, sizeof(*p));
505 while ((r = readhash(fd, flags, &h)) != RH_EOF)
507 case RH_EOF: /* otherwise clang complains */
513 p = xreallocarray(p, sz + 3, sizeof(*p));
529 for (pref = 0; pref < len[0] && pref < len[1] &&
530 file[0][pref + 1].value == file[1][pref + 1].value;
533 for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
534 file[0][len[0] - suff].value == file[1][len[1] - suff].value;
537 for (j = 0; j < 2; j++) {
538 sfile[j] = file[j] + pref;
539 slen[j] = len[j] - pref - suff;
540 for (i = 0; i <= slen[j]; i++)
541 sfile[j][i].serial = i;
546 equiv(struct line *a, int n, struct line *b, int m, int *c)
551 while (i <= n && j <= m) {
552 if (a[i].value < b[j].value)
554 else if (a[i].value == b[j].value)
565 while (b[j + 1].value == b[j].value) {
574 stone(int *a, int n, int *b, int *c, int flags)
577 int oldc, tc, oldl, sq;
578 unsigned numtries, bound;
580 if (flags & D_MINIMAL)
584 bound = MAX(256, sq);
588 c[0] = newcand(0, 0, 0);
589 for (i = 1; i <= n; i++) {
598 if (y <= clist[oldc].y)
604 if (clist[c[l]].y <= y)
607 c[l] = newcand(i, y, oldc);
612 c[l] = newcand(i, y, oldc);
616 } while ((y = b[++j]) > 0 && numtries < bound);
622 newcand(int x, int y, int pred)
626 if (clen == clistlen) {
627 clistlen = clistlen * 11 / 10;
628 clist = xreallocarray(clist, clistlen, sizeof(*clist));
638 search(int *c, int k, int y)
642 if (clist[c[k]].y < y) /* quick look for typical case */
667 for (i = 0; i <= len[0]; i++)
668 J[i] = i <= pref ? i :
669 i > len[0] - suff ? i + len[1] - len[0] : 0;
670 for (q = clist + p; q->y != 0; q = clist + q->pred)
671 J[q->x + pref] = q->y + pref;
675 * Check does double duty:
676 * 1. ferret out any fortuitous correspondences due to confounding by
677 * hashing (which result in "jackpot")
678 * 2. collect random access indexes to the two files
681 check(FILE *f1, FILE *f2, int flags)
683 int i, j, /* jackpot, */ c, d;
689 ixold[0] = ixnew[0] = 0;
692 for (i = 1; i <= len[0]; i++) {
694 ixold[i] = ctold += skipline(f1);
698 ixnew[j] = ctnew += skipline(f2);
701 if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS | D_IGNORECASE | D_STRIPCR)) {
706 * GNU diff ignores a missing newline
707 * in one file for -b or -w.
709 if (flags & (D_FOLDBLANKS | D_IGNOREBLANKS)) {
710 if (c == EOF && d == '\n') {
713 } else if (c == '\n' && d == EOF) {
720 if (flags & D_STRIPCR && (c == '\r' || d == '\r')) {
722 if ((c = getc(f1)) == '\n') {
729 if ((d = getc(f2)) == '\n') {
737 if ((flags & D_FOLDBLANKS) && isspace(c) &&
743 } while (isspace(c = getc(f1)));
748 } while (isspace(d = getc(f2)));
749 } else if (flags & D_IGNOREBLANKS) {
750 while (isspace(c) && c != '\n') {
754 while (isspace(d) && d != '\n') {
759 if (chrtran(c) != chrtran(d)) {
762 if (c != '\n' && c != EOF)
763 ctold += skipline(f1);
764 if (d != '\n' && c != EOF)
765 ctnew += skipline(f2);
768 if (c == '\n' || c == EOF)
775 if ((c = getc(f1)) != (d = getc(f2))) {
778 if (c != '\n' && c != EOF)
779 ctold += skipline(f1);
780 if (d != '\n' && c != EOF)
781 ctnew += skipline(f2);
784 if (c == '\n' || c == EOF)
792 for (; j <= len[1]; j++) {
793 ixnew[j] = ctnew += skipline(f2);
797 * fprintf(stderr, "jackpot\n");
801 /* shellsort CACM #201 */
803 sort(struct line *a, int n)
805 struct line *ai, *aim, w;
810 for (j = 1; j <= n; j *= 2)
812 for (m /= 2; m != 0; m /= 2) {
814 for (j = 1; j <= k; j++) {
815 for (ai = &a[j]; ai > a; ai -= m) {
818 break; /* wraparound */
819 if (aim->value > ai[0].value ||
820 (aim->value == ai[0].value &&
821 aim->serial > ai[0].serial))
823 w.value = ai[0].value;
824 ai[0].value = aim->value;
825 aim->value = w.value;
826 w.serial = ai[0].serial;
827 ai[0].serial = aim->serial;
828 aim->serial = w.serial;
835 unsort(struct line *f, int l, int *b)
839 a = xcalloc(l + 1, sizeof(*a));
840 for (i = 1; i <= l; i++)
841 a[f[i].serial] = f[i].value;
842 for (i = 1; i <= l; i++)
852 for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
858 output(char *file1, FILE *f1, char *file2, FILE *f2, int flags)
860 int i, j, m, i0, i1, j0, j1, nc;
866 J[m + 1] = len[1] + 1;
867 if (diff_format != D_EDIT) {
868 for (i0 = 1; i0 <= m; i0 = i1 + 1) {
869 while (i0 <= m && J[i0] == J[i0 - 1] + 1) {
870 if (diff_format == D_SIDEBYSIDE && suppress_common != 1) {
871 nc = fetch(ixold, i0, i0, f1, '\0', 1, flags);
872 print_space(nc, (hw - nc) + (padding << 1) + 1, flags);
873 fetch(ixnew, J[i0], J[i0], f2, '\0', 0, flags);
880 while (i1 < m && J[i1 + 1] == 0)
886 * When using side-by-side, lines from both of the files are
887 * printed. The algorithm used by diff(1) identifies the ranges
888 * in which two files differ.
889 * See the change() function below.
890 * The for loop below consumes the shorter range, whereas one of
891 * the while loops deals with the longer one.
893 if (diff_format == D_SIDEBYSIDE) {
894 for (i = i0, j = j0; i <= i1 && j <= j1; i++, j++)
895 change(file1, f1, file2, f2, i, i, j, j, &flags);
898 change(file1, f1, file2, f2, i, i, j + 1, j, &flags);
903 change(file1, f1, file2, f2, i + 1, i, j, j, &flags);
907 change(file1, f1, file2, f2, i0, i1, j0, j1, &flags);
910 for (i0 = m; i0 >= 1; i0 = i1 - 1) {
911 while (i0 >= 1 && J[i0] == J[i0 + 1] - 1 && J[i0] != 0)
915 while (i1 > 1 && J[i1 - 1] == 0)
919 change(file1, f1, file2, f2, i1, i0, j1, j0, &flags);
923 change(file1, f1, file2, f2, 1, 0, 1, len[1], &flags);
924 if (diff_format == D_IFDEF || diff_format == D_GFORMAT) {
927 if ((c = getc(f1)) == EOF)
933 if (anychange != 0) {
934 if (diff_format == D_CONTEXT)
935 dump_context_vec(f1, f2, flags);
936 else if (diff_format == D_UNIFIED)
937 dump_unified_vec(f1, f2, flags);
942 range(int a, int b, const char *separator)
944 printf("%d", a > b ? b : a);
946 printf("%s%d", separator, b);
950 uni_range(int a, int b)
953 printf("%d,%d", a, b - a + 1);
961 preadline(int fd, size_t rlen, off_t off)
966 line = xmalloc(rlen + 1);
967 if ((nr = pread(fd, line, rlen, off)) == -1)
969 if (nr > 0 && line[nr-1] == '\n')
976 ignoreline_pattern(char *line)
980 ret = regexec(&ignore_re, line, 0, NULL, 0);
981 return (ret == 0); /* if it matched, it should be ignored. */
985 ignoreline(char *line, bool skip_blanks)
988 if (skip_blanks && *line == '\0')
990 if (ignore_pats != NULL && ignoreline_pattern(line))
996 * Indicate that there is a difference between lines a and b of the from file
997 * to get to lines c to d of the to file. If a is greater then b then there
998 * are no lines in the from file involved and this means that there were
999 * lines appended (beginning at b). If c is greater than d then there are
1000 * lines missing from the to file.
1003 change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d,
1006 static size_t max_context = 64;
1010 bool skip_blanks, ignore;
1012 skip_blanks = (*pflags & D_SKIPBLANKLINES);
1014 if ((diff_format != D_IFDEF || diff_format == D_GFORMAT) &&
1017 if (ignore_pats != NULL || skip_blanks) {
1020 * All lines in the change, insert, or delete must match an ignore
1021 * pattern for the change to be ignored.
1023 if (a <= b) { /* Changes and deletes. */
1024 for (i = a; i <= b; i++) {
1025 line = preadline(fileno(f1),
1026 ixold[i] - ixold[i - 1], ixold[i - 1]);
1027 ignore = ignoreline(line, skip_blanks);
1033 if (a > b || c <= d) { /* Changes and inserts. */
1034 for (i = c; i <= d; i++) {
1035 line = preadline(fileno(f2),
1036 ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1037 ignore = ignoreline(line, skip_blanks);
1046 if (*pflags & D_HEADER && diff_format != D_BRIEF) {
1047 printf("%s %s %s\n", diffargs, file1, file2);
1048 *pflags &= ~D_HEADER;
1050 if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1052 * Allocate change records as needed.
1054 if (context_vec_start == NULL ||
1055 context_vec_ptr == context_vec_end - 1) {
1056 ptrdiff_t offset = -1;
1058 if (context_vec_start != NULL)
1059 offset = context_vec_ptr - context_vec_start;
1061 context_vec_start = xreallocarray(context_vec_start,
1062 max_context, sizeof(*context_vec_start));
1063 context_vec_end = context_vec_start + max_context;
1064 context_vec_ptr = context_vec_start + offset;
1066 if (anychange == 0) {
1068 * Print the context/unidiff header first time through.
1070 print_header(file1, file2);
1072 } else if (a > context_vec_ptr->b + (2 * diff_context) + 1 &&
1073 c > context_vec_ptr->d + (2 * diff_context) + 1) {
1075 * If this change is more than 'diff_context' lines from the
1076 * previous change, dump the record and reset it.
1078 if (diff_format == D_CONTEXT)
1079 dump_context_vec(f1, f2, *pflags);
1081 dump_unified_vec(f1, f2, *pflags);
1084 context_vec_ptr->a = a;
1085 context_vec_ptr->b = b;
1086 context_vec_ptr->c = c;
1087 context_vec_ptr->d = d;
1092 switch (diff_format) {
1098 printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1099 if (diff_format == D_NORMAL)
1104 printf("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1110 printf("a%d %d\n", b, d - c + 1);
1112 printf("d%d %d\n", a, b - a + 1);
1114 /* add changed lines */
1115 printf("a%d %d\n", b, d - c + 1);
1119 if (diff_format == D_GFORMAT) {
1121 /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1122 nc = ixold[a > b ? b : a - 1] - curpos;
1123 for (i = 0; i < nc; i++)
1124 printf("%c", getc(f1));
1125 for (walk = group_format; *walk != '\0'; walk++) {
1130 fetch(ixold, a, b, f1, '<', 1, *pflags);
1133 fetch(ixnew, c, d, f2, '>', 0, *pflags);
1136 printf("%%%c", *walk);
1141 printf("%c", *walk);
1144 if (diff_format == D_SIDEBYSIDE) {
1146 printf("\033[%sm", add_code);
1147 else if (color && c > d)
1148 printf("\033[%sm", del_code);
1150 print_space(0, hw + padding , *pflags);
1152 nc = fetch(ixold, a, b, f1, '\0', 1, *pflags);
1153 print_space(nc, hw - nc + padding, *pflags);
1156 printf("\033[%sm", add_code);
1157 else if (color && c > d)
1158 printf("\033[%sm", del_code);
1159 printf("%c", (a > b) ? '>' : ((c > d) ? '<' : '|'));
1162 print_space(hw + padding + 1 , padding, *pflags);
1163 fetch(ixnew, c, d, f2, '\0', 0, *pflags);
1166 if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1167 fetch(ixold, a, b, f1, '<', 1, *pflags);
1168 if (a <= b && c <= d && diff_format == D_NORMAL)
1171 if (diff_format != D_GFORMAT && diff_format != D_SIDEBYSIDE)
1172 fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0, *pflags);
1173 if (edoffset != 0 && diff_format == D_EDIT) {
1175 * A non-zero edoffset value for D_EDIT indicates that the last line
1176 * printed was a bare dot (".") that has been escaped as ".." to
1177 * prevent ed(1) from misinterpreting it. We have to add a
1178 * substitute command to change this back and restart where we left
1182 printf("%ds/.//\n", a + edoffset - 1);
1183 b = a + edoffset - 1;
1188 if ((diff_format == D_EDIT || diff_format == D_REVERSE) && c <= d)
1191 printf("#endif /* %s */\n", ifdefname);
1197 fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile, int flags)
1199 int i, j, c, lastc, col, nc, newcol;
1204 * When doing #ifdef's, copy down to current line
1205 * if this is the first file, so that stuff makes it to output.
1207 if ((diff_format == D_IFDEF) && oldfile) {
1208 long curpos = ftell(lb);
1209 /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1210 nc = f[a > b ? b : a - 1] - curpos;
1211 for (i = 0; i < nc; i++)
1212 printf("%c", getc(lb));
1216 if (diff_format == D_IFDEF) {
1218 printf("#else /* %s%s */\n",
1219 oldfile == 1 ? "!" : "", ifdefname);
1222 printf("#ifndef %s\n", ifdefname);
1224 printf("#ifdef %s\n", ifdefname);
1226 inifdef = 1 + oldfile;
1228 for (i = a; i <= b; i++) {
1229 fseek(lb, f[i - 1], SEEK_SET);
1230 nc = f[i] - f[i - 1];
1231 if (diff_format == D_SIDEBYSIDE && hw < nc)
1233 if (diff_format != D_IFDEF && diff_format != D_GFORMAT &&
1235 if (color && (ch == '>' || ch == '+'))
1236 printf("\033[%sm", add_code);
1237 else if (color && (ch == '<' || ch == '-'))
1238 printf("\033[%sm", del_code);
1240 if (Tflag && (diff_format == D_NORMAL ||
1241 diff_format == D_CONTEXT ||
1242 diff_format == D_UNIFIED))
1244 else if (diff_format != D_UNIFIED)
1249 while (j < nc && (hw == 0 || col < hw)) {
1251 if (flags & D_STRIPCR && c == '\r') {
1252 if ((c = getc(lb)) == '\n')
1260 if (diff_format == D_EDIT ||
1261 diff_format == D_REVERSE ||
1262 diff_format == D_NREVERSE)
1263 warnx("No newline at end of file");
1265 printf("\n\\ No newline at end of file\n");
1269 * when using --side-by-side, col needs to be increased
1270 * in any case to keep the columns aligned
1273 if (flags & D_EXPANDTABS) {
1274 newcol = ((col / tabsize) + 1) * tabsize;
1277 } while (++col < newcol && col < hw);
1279 if (diff_format == D_SIDEBYSIDE) {
1280 if ((col + tabsize) > hw) {
1281 printf("%*s", hw - col, "");
1293 if (diff_format == D_EDIT && j == 1 && c == '\n' &&
1296 * Don't print a bare "." line since that will confuse
1297 * ed(1). Print ".." instead and set the, global variable
1298 * edoffset to an offset from which to restart. The
1299 * caller must check the value of edoffset
1302 edoffset = i - a + 1;
1305 /* when side-by-side, do not print a newline */
1306 if (diff_format != D_SIDEBYSIDE || c != '\n') {
1307 if (color && c == '\n')
1308 printf("\033[m%c", c);
1319 if (color && diff_format == D_SIDEBYSIDE)
1325 * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1327 static enum readhash
1328 readhash(FILE *f, int flags, unsigned *hash)
1336 switch (t = getc(f)) {
1338 if ((flags & D_FORCEASCII) == 0)
1342 if (flags & D_STRIPCR) {
1353 if ((flags & (D_FOLDBLANKS|D_IGNOREBLANKS)) != 0) {
1360 if (space && (flags & D_IGNOREBLANKS) == 0) {
1364 sum = sum * 127 + chrtran(t);
1383 unsigned char buf[BUFSIZ];
1390 cnt = fread(buf, 1, sizeof(buf), f);
1391 return (memchr(buf, '\0', cnt) == NULL);
1394 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre) - 1) == 0)
1397 match_function(const long *f, int pos, FILE *fp)
1399 unsigned char buf[FUNCTION_CONTEXT_SIZE];
1401 int last = lastline;
1402 const char *state = NULL;
1405 for (; pos > last; pos--) {
1406 fseek(fp, f[pos - 1], SEEK_SET);
1407 nc = f[pos] - f[pos - 1];
1408 if (nc >= sizeof(buf))
1409 nc = sizeof(buf) - 1;
1410 nc = fread(buf, 1, nc, fp);
1414 buf[strcspn(buf, "\n")] = '\0';
1415 if (most_recent_pat != NULL) {
1416 int ret = regexec(&most_recent_re, buf, 0, NULL, 0);
1420 strlcpy(lastbuf, buf, sizeof(lastbuf));
1421 lastmatchline = pos;
1423 } else if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$'
1424 || buf[0] == '-' || buf[0] == '+') {
1425 if (begins_with(buf, "private:")) {
1427 state = " (private)";
1428 } else if (begins_with(buf, "protected:")) {
1430 state = " (protected)";
1431 } else if (begins_with(buf, "public:")) {
1433 state = " (public)";
1435 strlcpy(lastbuf, buf, sizeof(lastbuf));
1437 strlcat(lastbuf, state, sizeof(lastbuf));
1438 lastmatchline = pos;
1443 return (lastmatchline > 0 ? lastbuf : NULL);
1446 /* dump accumulated "context" diff changes */
1448 dump_context_vec(FILE *f1, FILE *f2, int flags)
1450 struct context_vec *cvp = context_vec_start;
1451 int lowa, upb, lowc, upd, do_output;
1455 if (context_vec_start > context_vec_ptr)
1458 b = d = 0; /* gcc */
1459 lowa = MAX(1, cvp->a - diff_context);
1460 upb = MIN(len[0], context_vec_ptr->b + diff_context);
1461 lowc = MAX(1, cvp->c - diff_context);
1462 upd = MIN(len[1], context_vec_ptr->d + diff_context);
1464 printf("***************");
1465 if (flags & (D_PROTOTYPE | D_MATCHLAST)) {
1466 f = match_function(ixold, cvp->a - 1, f1);
1471 range(lowa, upb, ",");
1475 * Output changes to the "old" file. The first loop suppresses
1476 * output if there were no changes to the "old" file (we'll see
1477 * the "old" lines as context in the "new" list).
1480 for (; cvp <= context_vec_ptr; cvp++)
1481 if (cvp->a <= cvp->b) {
1482 cvp = context_vec_start;
1487 while (cvp <= context_vec_ptr) {
1493 if (a <= b && c <= d)
1496 ch = (a <= b) ? 'd' : 'a';
1499 fetch(ixold, lowa, b, f1, ' ', 0, flags);
1501 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1502 fetch(ixold, a, b, f1,
1503 ch == 'c' ? '!' : '-', 0, flags);
1508 fetch(ixold, b + 1, upb, f1, ' ', 0, flags);
1510 /* output changes to the "new" file */
1512 range(lowc, upd, ",");
1516 for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1517 if (cvp->c <= cvp->d) {
1518 cvp = context_vec_start;
1523 while (cvp <= context_vec_ptr) {
1529 if (a <= b && c <= d)
1532 ch = (a <= b) ? 'd' : 'a';
1535 fetch(ixnew, lowc, d, f2, ' ', 0, flags);
1537 fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1538 fetch(ixnew, c, d, f2,
1539 ch == 'c' ? '!' : '+', 0, flags);
1544 fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1546 context_vec_ptr = context_vec_start - 1;
1549 /* dump accumulated "unified" diff changes */
1551 dump_unified_vec(FILE *f1, FILE *f2, int flags)
1553 struct context_vec *cvp = context_vec_start;
1554 int lowa, upb, lowc, upd;
1558 if (context_vec_start > context_vec_ptr)
1561 b = d = 0; /* gcc */
1562 lowa = MAX(1, cvp->a - diff_context);
1563 upb = MIN(len[0], context_vec_ptr->b + diff_context);
1564 lowc = MAX(1, cvp->c - diff_context);
1565 upd = MIN(len[1], context_vec_ptr->d + diff_context);
1568 uni_range(lowa, upb);
1570 uni_range(lowc, upd);
1572 if (flags & (D_PROTOTYPE | D_MATCHLAST)) {
1573 f = match_function(ixold, cvp->a - 1, f1);
1580 * Output changes in "unified" diff format--the old and new lines
1581 * are printed together.
1583 for (; cvp <= context_vec_ptr; cvp++) {
1590 * c: both new and old changes
1591 * d: only changes in the old file
1592 * a: only changes in the new file
1594 if (a <= b && c <= d)
1597 ch = (a <= b) ? 'd' : 'a';
1601 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1602 fetch(ixold, a, b, f1, '-', 0, flags);
1603 fetch(ixnew, c, d, f2, '+', 0, flags);
1606 fetch(ixold, lowa, a - 1, f1, ' ', 0, flags);
1607 fetch(ixold, a, b, f1, '-', 0, flags);
1610 fetch(ixnew, lowc, c - 1, f2, ' ', 0, flags);
1611 fetch(ixnew, c, d, f2, '+', 0, flags);
1617 fetch(ixnew, d + 1, upd, f2, ' ', 0, flags);
1619 context_vec_ptr = context_vec_start - 1;
1623 print_header(const char *file1, const char *file2)
1625 const char *time_format;
1627 struct tm tm1, tm2, *tm_ptr1, *tm_ptr2;
1628 int nsec1 = stb1.st_mtim.tv_nsec;
1629 int nsec2 = stb2.st_mtim.tv_nsec;
1631 time_format = "%Y-%m-%d %H:%M:%S";
1635 tm_ptr1 = localtime_r(&stb1.st_mtime, &tm1);
1636 tm_ptr2 = localtime_r(&stb2.st_mtime, &tm2);
1637 if (label[0] != NULL)
1638 printf("%s %s\n", diff_format == D_CONTEXT ? "***" : "---",
1641 strftime(buf, sizeof(buf), time_format, tm_ptr1);
1642 printf("%s %s\t%s", diff_format == D_CONTEXT ? "***" : "---",
1645 strftime(buf, sizeof(buf), "%z", tm_ptr1);
1646 printf(".%.9d %s", nsec1, buf);
1650 if (label[1] != NULL)
1651 printf("%s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
1654 strftime(buf, sizeof(buf), time_format, tm_ptr2);
1655 printf("%s %s\t%s", diff_format == D_CONTEXT ? "---" : "+++",
1658 strftime(buf, sizeof(buf), "%z", tm_ptr2);
1659 printf(".%.9d %s", nsec2, buf);
1666 * Prints n number of space characters either by using tab
1667 * or single space characters.
1668 * nc is the preceding number of characters
1671 print_space(int nc, int n, int flags) {
1675 if ((flags & D_EXPANDTABS) == 0) {
1676 /* first tabstop may be closer than tabsize */
1677 i = tabsize - (nc % tabsize);
1678 while (col >= tabsize) {
1684 printf("%*s", col, "");