Annotation of src/usr.bin/cvs/diff3.c, Revision 1.6
1.6 ! xsa 1: /* $OpenBSD: diff3.c,v 1.5 2005/10/26 18:13:58 xsa Exp $ */
1.1 joris 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36: /*-
37: * Copyright (c) 1991, 1993
38: * The Regents of the University of California. 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: * @(#)diff3.c 8.1 (Berkeley) 6/6/93
65: */
66:
67: #ifndef lint
68: static const char copyright[] =
69: "@(#) Copyright (c) 1991, 1993\n\
70: The Regents of the University of California. All rights reserved.\n";
71: #endif /* not lint */
72:
73: #ifndef lint
1.6 ! xsa 74: static const char rcsid[] = "$OpenBSD: diff3.c,v 1.5 2005/10/26 18:13:58 xsa Exp $";
1.1 joris 75: #endif /* not lint */
76:
77: #include <sys/queue.h>
78:
79: #include <err.h>
80: #include <ctype.h>
81: #include <stdio.h>
82: #include <stdlib.h>
83: #include <string.h>
84: #include <unistd.h>
85:
86: #include "cvs.h"
87: #include "log.h"
88: #include "diff.h"
89:
90: /* diff3 - 3-way differential file comparison */
91:
92: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
93: *
94: * d13 = diff report on f1 vs f3
95: * d23 = diff report on f2 vs f3
96: * f1, f2, f3 the 3 files
97: * if changes in f1 overlap with changes in f3, m1 and m3 are used
98: * to mark the overlaps; otherwise, the file names f1 and f3 are used
99: * (only for options E and X).
100: */
101:
102: /*
103: * "from" is first in range of changed lines; "to" is last+1
104: * from=to=line after point of insertion for added lines.
105: */
106: struct range {
107: int from;
108: int to;
109: };
110:
111: struct diff {
112: struct range old;
113: struct range new;
114: };
115:
116: static size_t szchanges;
117:
118: static struct diff *d13;
119: static struct diff *d23;
120:
121: /*
122: * "de" is used to gather editing scripts. These are later spewed out in
123: * reverse order. Its first element must be all zero, the "new" component
124: * of "de" contains line positions or byte positions depending on when you
125: * look (!?). Array overlap indicates which sections in "de" correspond to
126: * lines that are different in all three files.
127: */
128: static struct diff *de;
129: static char *overlap;
1.2 joris 130: static int overlapcnt = 0;
1.1 joris 131: static FILE *fp[3];
132: static int cline[3]; /* # of the last-read line in each file (0-2) */
133:
134: /*
135: * the latest known correspondence between line numbers of the 3 files
136: * is stored in last[1-3];
137: */
138: static int last[4];
139: static int eflag;
140: static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
141: static int debug = 0;
142: static char f1mark[40], f3mark[40]; /* markers for -E and -X */
143:
144: static int duplicate(struct range *, struct range *);
145: static int edit(struct diff *, int, int);
146: static char *getchange(FILE *);
147: static char *getline(FILE *, size_t *);
148: static int number(char **);
149: static int readin(char *, struct diff **);
150: static int skip(int, int, char *);
151: static int edscript(int);
152: static int merge(int, int);
153: static void change(int, struct range *, int);
154: static void keep(int, struct range *);
155: static void prange(struct range *);
156: static void repos(int);
157: static void separate(const char *);
158: static void increase(void);
159: static int diff3_internal(int, char **, const char *, const char *);
160:
1.5 xsa 161: int diff3_conflicts = 0;
1.4 joris 162:
1.1 joris 163: BUF *
164: cvs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2)
165: {
166: int ret, argc;
167: char *data, *patch;
168: char *argv[5], r1[16], r2[16];
169: char path1[MAXPATHLEN], path2[MAXPATHLEN], path3[MAXPATHLEN];
170: char dp13[MAXPATHLEN], dp23[MAXPATHLEN];
171: BUF *b1, *b2, *b3, *d1, *d2, *diffb;
172:
173: ret = -1;
174: b1 = b2 = b3 = d1 = d2 = diffb = NULL;
175:
176: rcsnum_tostr(rev1, r1, sizeof(r1));
177: rcsnum_tostr(rev2, r2, sizeof(r2));
178:
179: if ((b1 = cvs_buf_load(workfile, BUF_AUTOEXT)) == NULL)
180: goto out;
181:
1.4 joris 182: cvs_printf("Retrieving revision %s\n", r1);
1.1 joris 183: if ((b2 = rcs_getrev(rf, rev1)) == NULL)
184: goto out;
185:
1.4 joris 186: cvs_printf("Retrieving revision %s\n", r2);
1.1 joris 187: if ((b3 = rcs_getrev(rf, rev2)) == NULL)
188: goto out;
189:
1.6 ! xsa 190: if ((d1 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL)
1.1 joris 191: goto out;
192:
1.6 ! xsa 193: if ((d2 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL)
1.1 joris 194: goto out;
195:
1.6 ! xsa 196: if ((diffb = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL)
1.1 joris 197: goto out;
198:
199: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
200: if (cvs_buf_write_stmp(b1, path1, 0600) == -1)
201: goto out;
202:
203: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
204: if (cvs_buf_write_stmp(b2, path2, 0600) == -1)
205: goto out;
206:
207: strlcpy(path3, "/tmp/diff3.XXXXXXXXXX", sizeof(path3));
208: if (cvs_buf_write_stmp(b3, path3, 0600) == -1)
209: goto out;
210:
211: cvs_buf_free(b2);
212: b2 = NULL;
213:
214: cvs_diffreg(path1, path3, d1);
215: cvs_diffreg(path2, path3, d2);
216:
217: strlcpy(dp13, "/tmp/d13.XXXXXXXXXX", sizeof(dp13));
218: if (cvs_buf_write_stmp(d1, dp13, 0600) < 0)
219: goto out;
220:
221: cvs_buf_free(d1);
222: d1 = NULL;
223:
224: strlcpy(dp23, "/tmp/d23.XXXXXXXXXX", sizeof(dp23));
225: if (cvs_buf_write_stmp(d2, dp23, 0600) < 0)
226: goto out;
227:
228: cvs_buf_free(d2);
229: d2 = NULL;
230:
231: argc = 0;
232: diffbuf = diffb;
233: argv[argc++] = dp13;
234: argv[argc++] = dp23;
235: argv[argc++] = path1;
236: argv[argc++] = path2;
237: argv[argc++] = path3;
1.4 joris 238: if ((diff3_conflicts = diff3_internal(argc, argv, workfile, r2)) < 0)
1.1 joris 239: goto out;
240:
241: if (cvs_buf_putc(diffb, '\0') < 0) {
242: cvs_buf_free(diffb);
243: goto out;
244: }
245:
246: if (cvs_buf_putc(b1, '\0') < 0) {
247: cvs_buf_free(diffb);
248: goto out;
249: }
250:
251: patch = cvs_buf_release(diffb);
252: data = cvs_buf_release(b1);
253: diffb = b1 = NULL;
1.4 joris 254:
255: cvs_printf("Merging changes between '%s' and '%s' ", r1, r2);
256: cvs_printf("into '%s'\n", workfile);
1.1 joris 257:
258: if ((diffb = cvs_patchfile(data, patch, ed_patch_lines)) == NULL)
259: goto out;
260:
261: free(data);
262: free(patch);
263:
264: out:
265: if (b1 != NULL)
266: cvs_buf_free(b1);
267: if (b2 != NULL)
268: cvs_buf_free(b2);
269: if (b3 != NULL)
270: cvs_buf_free(b3);
271: if (d1 != NULL)
272: cvs_buf_free(d1);
273: if (d2 != NULL)
274: cvs_buf_free(d2);
275:
276: (void)unlink(path1);
277: (void)unlink(path2);
278: (void)unlink(path3);
279: (void)unlink(dp13);
280: (void)unlink(dp23);
281:
282: return (diffb);
283: }
284:
285: static int
286: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
287: {
288: int i, m, n;
289:
290: /* XXX */
291: eflag = 3;
292: oflag = 1;
293:
294: if (argc < 5)
295: return (-1);
296:
297: snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
298: snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
299:
300: increase();
301: m = readin(argv[0], &d13);
302: n = readin(argv[1], &d23);
303:
304: for (i = 0; i <= 2; i++) {
305: if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
306: cvs_log(LP_ERRNO, "%s", argv[i + 2]);
307: return (-1);
308: }
309: }
310:
311: return (merge(m, n));
312: }
313:
314: int
315: ed_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
316: {
317: char op, *ep;
318: struct cvs_line *sort, *lp, *dlp, *ndlp;
319: int start, end, busy, i, lineno;
320:
321: dlp = TAILQ_FIRST(&(dlines->l_lines));
322: lp = TAILQ_FIRST(&(plines->l_lines));
323:
1.3 joris 324: busy = end = 0;
1.1 joris 325: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
326: lp = TAILQ_NEXT(lp, l_list)) {
327: op = lp->l_line[strlen(lp->l_line) - 1];
328: start = (int)strtol(lp->l_line, &ep, 10);
329: if (op == 'a') {
330: if ((start > dlines->l_nblines) ||
331: (start < 0) || (*ep != 'a'))
332: return (-1);
333: } else if (op == 'c') {
334: if ((start > dlines->l_nblines) ||
1.2 joris 335: (start < 0) || ((*ep != ',') && (*ep != 'c')))
1.1 joris 336: return (-1);
337:
1.2 joris 338: if (*ep == ',') {
339: ep++;
340: end = (int)strtol(ep, &ep, 10);
341: if ((end < 0) || (*ep != 'c'))
342: return (-1);
343: } else {
344: end = start;
345: }
1.1 joris 346: }
347:
1.2 joris 348:
1.1 joris 349: for (;;) {
350: if (dlp == NULL)
351: break;
352: if (dlp->l_lineno == start)
353: break;
354: if (dlp->l_lineno > start) {
355: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
356: } else if (dlp->l_lineno < start) {
357: ndlp = TAILQ_NEXT(dlp, l_list);
358: if (ndlp->l_lineno > start)
359: break;
360: dlp = ndlp;
361: }
362: }
363:
364: if (dlp == NULL)
365: return (-1);
366:
1.2 joris 367:
1.1 joris 368: if (op == 'c') {
1.2 joris 369: for (i = 0; i <= (end - start); i++) {
1.1 joris 370: ndlp = TAILQ_NEXT(dlp, l_list);
371: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
372: dlp = ndlp;
373: }
1.2 joris 374: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
1.1 joris 375: }
376:
377: if (op == 'a' || op == 'c') {
378: for (;;) {
379: ndlp = lp;
380: lp = TAILQ_NEXT(lp, l_list);
381: if (lp == NULL)
382: return (-1);
383:
384: if (!strcmp(lp->l_line, "."))
385: break;
386:
387: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
388: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
389: lp, l_list);
390: dlp = lp;
391:
392: lp->l_lineno = start;
393: lp = ndlp;
394: }
395: }
396:
397: /*
398: * always resort lines as the markers might be put at the
399: * same line as we first started editing.
400: */
401: lineno = 0;
402: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
403: sort->l_lineno = lineno++;
404: dlines->l_nblines = lineno - 1;
405: }
406:
407: return (0);
408: }
409:
410: /*
411: * Pick up the line numbers of all changes from one change file.
412: * (This puts the numbers in a vector, which is not strictly necessary,
413: * since the vector is processed in one sequential pass.
414: * The vector could be optimized out of existence)
415: */
416: static int
417: readin(char *name, struct diff **dd)
418: {
419: int a, b, c, d;
420: char kind, *p;
421: size_t i;
422:
423: fp[0] = fopen(name, "r");
424: for (i = 0; (p = getchange(fp[0])); i++) {
425: if (i >= szchanges - 1)
426: increase();
427: a = b = number(&p);
428: if (*p == ',') {
429: p++;
430: b = number(&p);
431: }
432: kind = *p++;
433: c = d = number(&p);
434: if (*p==',') {
435: p++;
436: d = number(&p);
437: }
438: if (kind == 'a')
439: a++;
440: if (kind == 'd')
441: c++;
442: b++;
443: d++;
444: (*dd)[i].old.from = a;
445: (*dd)[i].old.to = b;
446: (*dd)[i].new.from = c;
447: (*dd)[i].new.to = d;
448: }
449:
450: (*dd)[i].old.from = (*dd)[i-1].old.to;
451: (*dd)[i].new.from = (*dd)[i-1].new.to;
452: (void)fclose(fp[0]);
453:
454: return (i);
455: }
456:
457: static int
458: number(char **lc)
459: {
460: int nn;
461:
462: nn = 0;
463: while (isdigit((unsigned char)(**lc)))
464: nn = nn*10 + *(*lc)++ - '0';
465:
466: return (nn);
467: }
468:
469: static char *
470: getchange(FILE *b)
471: {
472: char *line;
473:
474: while ((line = getline(b, NULL))) {
475: if (isdigit((unsigned char)line[0]))
476: return (line);
477: }
478:
479: return (NULL);
480: }
481:
482: static char *
483: getline(FILE *b, size_t *n)
484: {
485: char *cp;
486: size_t len;
487: static char *buf;
488: static size_t bufsize;
489:
490: if ((cp = fgetln(b, &len)) == NULL)
491: return (NULL);
492:
493: if (cp[len - 1] != '\n')
494: len++;
495: if (len + 1 > bufsize) {
496: do {
497: bufsize += 1024;
498: } while (len + 1 > bufsize);
499: if ((buf = realloc(buf, bufsize)) == NULL)
500: err(EXIT_FAILURE, NULL);
501: }
502: memcpy(buf, cp, len - 1);
503: buf[len - 1] = '\n';
504: buf[len] = '\0';
505: if (n != NULL)
506: *n = len;
507:
508: return (buf);
509: }
510:
511: static int
512: merge(int m1, int m2)
513: {
514: struct diff *d1, *d2, *d3;
515: int dpl, j, t1, t2;
516:
517: d1 = d13;
518: d2 = d23;
519: j = 0;
520: while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
521: if (debug) {
522: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
523: d1->old.from,d1->old.to,
524: d1->new.from,d1->new.to,
525: d2->old.from,d2->old.to,
526: d2->new.from,d2->new.to);
527: }
528:
529: /* first file is different from others */
530: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
531: /* stuff peculiar to 1st file */
532: if (eflag==0) {
533: separate("1");
534: change(1, &d1->old, 0);
535: keep(2, &d1->new);
536: change(3, &d1->new, 0);
537: }
538: d1++;
539: continue;
540: }
541:
542: /* second file is different from others */
543: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
544: if (eflag==0) {
545: separate("2");
546: keep(1, &d2->new);
547: change(2, &d2->old, 0);
548: change(3, &d2->new, 0);
549: }
550: d2++;
551: continue;
552: }
553:
554: /*
555: * Merge overlapping changes in first file
556: * this happens after extension (see below).
557: */
558: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
559: d1[1].old.from = d1->old.from;
560: d1[1].new.from = d1->new.from;
561: d1++;
562: continue;
563: }
564:
565: /* merge overlapping changes in second */
566: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
567: d2[1].old.from = d2->old.from;
568: d2[1].new.from = d2->new.from;
569: d2++;
570: continue;
571: }
572: /* stuff peculiar to third file or different in all */
573: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
574: dpl = duplicate(&d1->old,&d2->old);
575: if (dpl == -1)
576: return (-1);
577:
578: /*
579: * dpl = 0 means all files differ
580: * dpl = 1 means files 1 and 2 identical
581: */
582: if (eflag==0) {
583: separate(dpl ? "3" : "");
584: change(1, &d1->old, dpl);
585: change(2, &d2->old, 0);
586: d3 = d1->old.to > d1->old.from ? d1 : d2;
587: change(3, &d3->new, 0);
588: } else
589: j = edit(d1, dpl, j);
590: d1++;
591: d2++;
592: continue;
593: }
594:
595: /*
596: * Overlapping changes from file 1 and 2; extend changes
597: * appropriately to make them coincide.
598: */
599: if (d1->new.from < d2->new.from) {
600: d2->old.from -= d2->new.from-d1->new.from;
601: d2->new.from = d1->new.from;
602: } else if (d2->new.from < d1->new.from) {
603: d1->old.from -= d1->new.from-d2->new.from;
604: d1->new.from = d2->new.from;
605: }
606: if (d1->new.to > d2->new.to) {
607: d2->old.to += d1->new.to - d2->new.to;
608: d2->new.to = d1->new.to;
609: } else if (d2->new.to > d1->new.to) {
610: d1->old.to += d2->new.to - d1->new.to;
611: d1->new.to = d2->new.to;
612: }
613: }
614:
615: return (edscript(j));
616: }
617:
618: static void
619: separate(const char *s)
620: {
621: diff_output("====%s\n", s);
622: }
623:
624: /*
625: * The range of lines rold.from thru rold.to in file i is to be changed.
626: * It is to be printed only if it does not duplicate something to be
627: * printed later.
628: */
629: static void
630: change(int i, struct range *rold, int fdup)
631: {
632: diff_output("%d:", i);
633: last[i] = rold->to;
634: prange(rold);
635: if (fdup || debug)
636: return;
637: i--;
638: (void)skip(i, rold->from, NULL);
639: (void)skip(i, rold->to, " ");
640: }
641:
642: /*
643: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
644: */
645: static void
646: prange(struct range *rold)
647: {
648: if (rold->to <= rold->from)
649: diff_output("%da\n", rold->from - 1);
650: else {
651: diff_output("%d", rold->from);
652: if (rold->to > rold->from+1)
653: diff_output(",%d", rold->to - 1);
654: diff_output("c\n");
655: }
656: }
657:
658: /*
659: * No difference was reported by diff between file 1 (or 2) and file 3,
660: * and an artificial dummy difference (trange) must be ginned up to
661: * correspond to the change reported in the other file.
662: */
663: static void
664: keep(int i, struct range *rnew)
665: {
666: int delta;
667: struct range trange;
668:
669: delta = last[3] - last[i];
670: trange.from = rnew->from - delta;
671: trange.to = rnew->to - delta;
672: change(i, &trange, 1);
673: }
674:
675: /*
676: * skip to just before line number from in file "i". If "pr" is non-NULL,
677: * print all skipped stuff with string pr as a prefix.
678: */
679: static int
680: skip(int i, int from, char *pr)
681: {
682: size_t j, n;
683: char *line;
684:
685: for (n = 0; cline[i] < from - 1; n += j) {
686: if ((line = getline(fp[i], &j)) == NULL)
687: return (-1);
688: if (pr != NULL)
689: diff_output("%s%s", pr, line);
690: cline[i]++;
691: }
692: return ((int) n);
693: }
694:
695: /*
696: * Return 1 or 0 according as the old range (in file 1) contains exactly
697: * the same data as the new range (in file 2).
698: */
699: static int
700: duplicate(struct range *r1, struct range *r2)
701: {
702: int c,d;
703: int nchar;
704: int nline;
705:
706: if (r1->to-r1->from != r2->to-r2->from)
707: return (0);
708: (void)skip(0, r1->from, NULL);
709: (void)skip(1, r2->from, NULL);
710: nchar = 0;
711: for (nline=0; nline < r1->to - r1->from; nline++) {
712: do {
713: c = getc(fp[0]);
714: d = getc(fp[1]);
715: if (c == -1 || d== -1)
716: return (-1);
717: nchar++;
718: if (c != d) {
719: repos(nchar);
720: return (0);
721: }
722: } while (c != '\n');
723: }
724: repos(nchar);
725: return (1);
726: }
727:
728: static void
729: repos(int nchar)
730: {
731: int i;
732:
733: for (i = 0; i < 2; i++)
734: (void)fseek(fp[i], (long)-nchar, 1);
735: }
736:
737: /*
738: * collect an editing script for later regurgitation
739: */
740: static int
741: edit(struct diff *diff, int fdup, int j)
742: {
743: if (((fdup + 1) & eflag) == 0)
744: return (j);
745: j++;
746: overlap[j] = !fdup;
747: if (!fdup)
748: overlapcnt++;
749: de[j].old.from = diff->old.from;
750: de[j].old.to = diff->old.to;
751: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
752: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
753: return (j);
754: }
755:
756: /* regurgitate */
757: static int
758: edscript(int n)
759: {
760: int j, k;
1.2 joris 761: char block[BUFSIZ+1];
1.1 joris 762:
763: for (n = n; n > 0; n--) {
764: if (!oflag || !overlap[n])
765: prange(&de[n].old);
766: else
767: diff_output("%da\n=======\n", de[n].old.to -1);
768: (void)fseek(fp[2], (long)de[n].new.from, 0);
769: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
770: j = k > BUFSIZ ? BUFSIZ : k;
1.6 ! xsa 771: if (fread(block, (size_t)1, (size_t)j, fp[2]) != (size_t)j)
1.1 joris 772: return (-1);
773: block[j] = '\0';
774: diff_output("%s", block);
775: }
1.2 joris 776:
1.1 joris 777: if (!oflag || !overlap[n])
778: diff_output(".\n");
779: else {
780: diff_output("%s\n.\n", f3mark);
781: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
782: }
783: }
784:
785: return (overlapcnt);
786: }
787:
788: static void
789: increase(void)
790: {
791: struct diff *p;
792: char *q;
793: size_t newsz, incr;
794:
795: /* are the memset(3) calls needed? */
796: newsz = szchanges == 0 ? 64 : 2 * szchanges;
797: incr = newsz - szchanges;
798:
799: p = realloc(d13, newsz * sizeof(struct diff));
800: if (p == NULL)
801: err(1, NULL);
802: memset(p + szchanges, 0, incr * sizeof(struct diff));
803: d13 = p;
804: p = realloc(d23, newsz * sizeof(struct diff));
805: if (p == NULL)
806: err(1, NULL);
807: memset(p + szchanges, 0, incr * sizeof(struct diff));
808: d23 = p;
809: p = realloc(de, newsz * sizeof(struct diff));
810: if (p == NULL)
811: err(1, NULL);
812: memset(p + szchanges, 0, incr * sizeof(struct diff));
813: de = p;
814: q = realloc(overlap, newsz * sizeof(char));
815: if (q == NULL)
816: err(1, NULL);
817: memset(q + szchanges, 0, incr * sizeof(char));
818: overlap = q;
819: szchanges = newsz;
820: }