Annotation of src/usr.bin/cvs/diff3.c, Revision 1.9
1.9 ! xsa 1: /* $OpenBSD: diff3.c,v 1.8 2005/12/10 20:27:45 joris 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.9 ! xsa 74: static const char rcsid[] = "$OpenBSD: diff3.c,v 1.8 2005/12/10 20:27:45 joris 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));
1.9 ! xsa 200: cvs_buf_write_stmp(b1, path1, 0600);
1.1 joris 201:
202: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
1.9 ! xsa 203: cvs_buf_write_stmp(b2, path2, 0600);
1.1 joris 204:
205: strlcpy(path3, "/tmp/diff3.XXXXXXXXXX", sizeof(path3));
1.9 ! xsa 206: cvs_buf_write_stmp(b3, path3, 0600);
1.1 joris 207:
208: cvs_buf_free(b2);
209: b2 = NULL;
210:
211: cvs_diffreg(path1, path3, d1);
212: cvs_diffreg(path2, path3, d2);
213:
214: strlcpy(dp13, "/tmp/d13.XXXXXXXXXX", sizeof(dp13));
1.9 ! xsa 215: cvs_buf_write_stmp(d1, dp13, 0600);
1.1 joris 216:
217: cvs_buf_free(d1);
218: d1 = NULL;
219:
220: strlcpy(dp23, "/tmp/d23.XXXXXXXXXX", sizeof(dp23));
1.9 ! xsa 221: cvs_buf_write_stmp(d2, dp23, 0600);
1.1 joris 222:
223: cvs_buf_free(d2);
224: d2 = NULL;
225:
226: argc = 0;
227: diffbuf = diffb;
228: argv[argc++] = dp13;
229: argv[argc++] = dp23;
230: argv[argc++] = path1;
231: argv[argc++] = path2;
232: argv[argc++] = path3;
1.7 joris 233:
234: diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
235: if (diff3_conflicts < 0) {
236: cvs_buf_free(diffb);
237: diffb = NULL;
1.1 joris 238: goto out;
1.7 joris 239: }
1.1 joris 240:
1.9 ! xsa 241: cvs_buf_putc(diffb, '\0');
! 242: cvs_buf_putc(b1, '\0');
1.1 joris 243:
244: patch = cvs_buf_release(diffb);
245: data = cvs_buf_release(b1);
246: diffb = b1 = NULL;
1.4 joris 247:
248: cvs_printf("Merging changes between '%s' and '%s' ", r1, r2);
249: cvs_printf("into '%s'\n", workfile);
1.1 joris 250:
251: if ((diffb = cvs_patchfile(data, patch, ed_patch_lines)) == NULL)
252: goto out;
1.7 joris 253:
254: if (diff3_conflicts != 0) {
255: cvs_printf("%d conflict%s found during merge, please correct.\n",
256: diff3_conflicts, (diff3_conflicts > 1) ? "s" : "");
257: }
1.1 joris 258:
1.8 joris 259: xfree(data);
260: xfree(patch);
1.1 joris 261:
262: out:
263: if (b1 != NULL)
264: cvs_buf_free(b1);
265: if (b2 != NULL)
266: cvs_buf_free(b2);
267: if (b3 != NULL)
268: cvs_buf_free(b3);
269: if (d1 != NULL)
270: cvs_buf_free(d1);
271: if (d2 != NULL)
272: cvs_buf_free(d2);
273:
274: (void)unlink(path1);
275: (void)unlink(path2);
276: (void)unlink(path3);
277: (void)unlink(dp13);
278: (void)unlink(dp23);
279:
280: return (diffb);
281: }
282:
283: static int
284: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
285: {
286: int i, m, n;
287:
288: /* XXX */
289: eflag = 3;
290: oflag = 1;
291:
292: if (argc < 5)
293: return (-1);
294:
295: snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
296: snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
297:
298: increase();
299: m = readin(argv[0], &d13);
300: n = readin(argv[1], &d23);
301:
302: for (i = 0; i <= 2; i++) {
303: if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
304: cvs_log(LP_ERRNO, "%s", argv[i + 2]);
305: return (-1);
306: }
307: }
308:
309: return (merge(m, n));
310: }
311:
312: int
313: ed_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
314: {
315: char op, *ep;
316: struct cvs_line *sort, *lp, *dlp, *ndlp;
317: int start, end, busy, i, lineno;
318:
319: dlp = TAILQ_FIRST(&(dlines->l_lines));
320: lp = TAILQ_FIRST(&(plines->l_lines));
321:
1.3 joris 322: busy = end = 0;
1.1 joris 323: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
324: lp = TAILQ_NEXT(lp, l_list)) {
325: op = lp->l_line[strlen(lp->l_line) - 1];
326: start = (int)strtol(lp->l_line, &ep, 10);
327: if (op == 'a') {
328: if ((start > dlines->l_nblines) ||
329: (start < 0) || (*ep != 'a'))
330: return (-1);
331: } else if (op == 'c') {
332: if ((start > dlines->l_nblines) ||
1.2 joris 333: (start < 0) || ((*ep != ',') && (*ep != 'c')))
1.1 joris 334: return (-1);
335:
1.2 joris 336: if (*ep == ',') {
337: ep++;
338: end = (int)strtol(ep, &ep, 10);
339: if ((end < 0) || (*ep != 'c'))
340: return (-1);
341: } else {
342: end = start;
343: }
1.1 joris 344: }
345:
1.2 joris 346:
1.1 joris 347: for (;;) {
348: if (dlp == NULL)
349: break;
350: if (dlp->l_lineno == start)
351: break;
352: if (dlp->l_lineno > start) {
353: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
354: } else if (dlp->l_lineno < start) {
355: ndlp = TAILQ_NEXT(dlp, l_list);
356: if (ndlp->l_lineno > start)
357: break;
358: dlp = ndlp;
359: }
360: }
361:
362: if (dlp == NULL)
363: return (-1);
364:
1.2 joris 365:
1.1 joris 366: if (op == 'c') {
1.2 joris 367: for (i = 0; i <= (end - start); i++) {
1.1 joris 368: ndlp = TAILQ_NEXT(dlp, l_list);
369: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
370: dlp = ndlp;
371: }
1.2 joris 372: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
1.1 joris 373: }
374:
375: if (op == 'a' || op == 'c') {
376: for (;;) {
377: ndlp = lp;
378: lp = TAILQ_NEXT(lp, l_list);
379: if (lp == NULL)
380: return (-1);
381:
382: if (!strcmp(lp->l_line, "."))
383: break;
384:
385: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
386: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
387: lp, l_list);
388: dlp = lp;
389:
390: lp->l_lineno = start;
391: lp = ndlp;
392: }
393: }
394:
395: /*
396: * always resort lines as the markers might be put at the
397: * same line as we first started editing.
398: */
399: lineno = 0;
400: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
401: sort->l_lineno = lineno++;
402: dlines->l_nblines = lineno - 1;
403: }
404:
405: return (0);
406: }
407:
408: /*
409: * Pick up the line numbers of all changes from one change file.
410: * (This puts the numbers in a vector, which is not strictly necessary,
411: * since the vector is processed in one sequential pass.
412: * The vector could be optimized out of existence)
413: */
414: static int
415: readin(char *name, struct diff **dd)
416: {
417: int a, b, c, d;
418: char kind, *p;
419: size_t i;
420:
421: fp[0] = fopen(name, "r");
422: for (i = 0; (p = getchange(fp[0])); i++) {
423: if (i >= szchanges - 1)
424: increase();
425: a = b = number(&p);
426: if (*p == ',') {
427: p++;
428: b = number(&p);
429: }
430: kind = *p++;
431: c = d = number(&p);
432: if (*p==',') {
433: p++;
434: d = number(&p);
435: }
436: if (kind == 'a')
437: a++;
438: if (kind == 'd')
439: c++;
440: b++;
441: d++;
442: (*dd)[i].old.from = a;
443: (*dd)[i].old.to = b;
444: (*dd)[i].new.from = c;
445: (*dd)[i].new.to = d;
446: }
447:
448: (*dd)[i].old.from = (*dd)[i-1].old.to;
449: (*dd)[i].new.from = (*dd)[i-1].new.to;
450: (void)fclose(fp[0]);
451:
452: return (i);
453: }
454:
455: static int
456: number(char **lc)
457: {
458: int nn;
459:
460: nn = 0;
461: while (isdigit((unsigned char)(**lc)))
462: nn = nn*10 + *(*lc)++ - '0';
463:
464: return (nn);
465: }
466:
467: static char *
468: getchange(FILE *b)
469: {
470: char *line;
471:
472: while ((line = getline(b, NULL))) {
473: if (isdigit((unsigned char)line[0]))
474: return (line);
475: }
476:
477: return (NULL);
478: }
479:
480: static char *
481: getline(FILE *b, size_t *n)
482: {
483: char *cp;
484: size_t len;
485: static char *buf;
486: static size_t bufsize;
487:
488: if ((cp = fgetln(b, &len)) == NULL)
489: return (NULL);
490:
491: if (cp[len - 1] != '\n')
492: len++;
493: if (len + 1 > bufsize) {
494: do {
495: bufsize += 1024;
496: } while (len + 1 > bufsize);
1.8 joris 497: buf = xrealloc(buf, bufsize);
1.1 joris 498: }
499: memcpy(buf, cp, len - 1);
500: buf[len - 1] = '\n';
501: buf[len] = '\0';
502: if (n != NULL)
503: *n = len;
504:
505: return (buf);
506: }
507:
508: static int
509: merge(int m1, int m2)
510: {
511: struct diff *d1, *d2, *d3;
512: int dpl, j, t1, t2;
513:
514: d1 = d13;
515: d2 = d23;
516: j = 0;
517: while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
518: if (debug) {
519: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
520: d1->old.from,d1->old.to,
521: d1->new.from,d1->new.to,
522: d2->old.from,d2->old.to,
523: d2->new.from,d2->new.to);
524: }
525:
526: /* first file is different from others */
527: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
528: /* stuff peculiar to 1st file */
529: if (eflag==0) {
530: separate("1");
531: change(1, &d1->old, 0);
532: keep(2, &d1->new);
533: change(3, &d1->new, 0);
534: }
535: d1++;
536: continue;
537: }
538:
539: /* second file is different from others */
540: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
541: if (eflag==0) {
542: separate("2");
543: keep(1, &d2->new);
544: change(2, &d2->old, 0);
545: change(3, &d2->new, 0);
546: }
547: d2++;
548: continue;
549: }
550:
551: /*
552: * Merge overlapping changes in first file
553: * this happens after extension (see below).
554: */
555: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
556: d1[1].old.from = d1->old.from;
557: d1[1].new.from = d1->new.from;
558: d1++;
559: continue;
560: }
561:
562: /* merge overlapping changes in second */
563: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
564: d2[1].old.from = d2->old.from;
565: d2[1].new.from = d2->new.from;
566: d2++;
567: continue;
568: }
569: /* stuff peculiar to third file or different in all */
570: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
571: dpl = duplicate(&d1->old,&d2->old);
572: if (dpl == -1)
573: return (-1);
574:
575: /*
576: * dpl = 0 means all files differ
577: * dpl = 1 means files 1 and 2 identical
578: */
579: if (eflag==0) {
580: separate(dpl ? "3" : "");
581: change(1, &d1->old, dpl);
582: change(2, &d2->old, 0);
583: d3 = d1->old.to > d1->old.from ? d1 : d2;
584: change(3, &d3->new, 0);
585: } else
586: j = edit(d1, dpl, j);
587: d1++;
588: d2++;
589: continue;
590: }
591:
592: /*
593: * Overlapping changes from file 1 and 2; extend changes
594: * appropriately to make them coincide.
595: */
596: if (d1->new.from < d2->new.from) {
597: d2->old.from -= d2->new.from-d1->new.from;
598: d2->new.from = d1->new.from;
599: } else if (d2->new.from < d1->new.from) {
600: d1->old.from -= d1->new.from-d2->new.from;
601: d1->new.from = d2->new.from;
602: }
603: if (d1->new.to > d2->new.to) {
604: d2->old.to += d1->new.to - d2->new.to;
605: d2->new.to = d1->new.to;
606: } else if (d2->new.to > d1->new.to) {
607: d1->old.to += d2->new.to - d1->new.to;
608: d1->new.to = d2->new.to;
609: }
610: }
611:
612: return (edscript(j));
613: }
614:
615: static void
616: separate(const char *s)
617: {
618: diff_output("====%s\n", s);
619: }
620:
621: /*
622: * The range of lines rold.from thru rold.to in file i is to be changed.
623: * It is to be printed only if it does not duplicate something to be
624: * printed later.
625: */
626: static void
627: change(int i, struct range *rold, int fdup)
628: {
629: diff_output("%d:", i);
630: last[i] = rold->to;
631: prange(rold);
632: if (fdup || debug)
633: return;
634: i--;
635: (void)skip(i, rold->from, NULL);
636: (void)skip(i, rold->to, " ");
637: }
638:
639: /*
640: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
641: */
642: static void
643: prange(struct range *rold)
644: {
645: if (rold->to <= rold->from)
646: diff_output("%da\n", rold->from - 1);
647: else {
648: diff_output("%d", rold->from);
649: if (rold->to > rold->from+1)
650: diff_output(",%d", rold->to - 1);
651: diff_output("c\n");
652: }
653: }
654:
655: /*
656: * No difference was reported by diff between file 1 (or 2) and file 3,
657: * and an artificial dummy difference (trange) must be ginned up to
658: * correspond to the change reported in the other file.
659: */
660: static void
661: keep(int i, struct range *rnew)
662: {
663: int delta;
664: struct range trange;
665:
666: delta = last[3] - last[i];
667: trange.from = rnew->from - delta;
668: trange.to = rnew->to - delta;
669: change(i, &trange, 1);
670: }
671:
672: /*
673: * skip to just before line number from in file "i". If "pr" is non-NULL,
674: * print all skipped stuff with string pr as a prefix.
675: */
676: static int
677: skip(int i, int from, char *pr)
678: {
679: size_t j, n;
680: char *line;
681:
682: for (n = 0; cline[i] < from - 1; n += j) {
683: if ((line = getline(fp[i], &j)) == NULL)
684: return (-1);
685: if (pr != NULL)
686: diff_output("%s%s", pr, line);
687: cline[i]++;
688: }
689: return ((int) n);
690: }
691:
692: /*
693: * Return 1 or 0 according as the old range (in file 1) contains exactly
694: * the same data as the new range (in file 2).
695: */
696: static int
697: duplicate(struct range *r1, struct range *r2)
698: {
699: int c,d;
700: int nchar;
701: int nline;
702:
703: if (r1->to-r1->from != r2->to-r2->from)
704: return (0);
705: (void)skip(0, r1->from, NULL);
706: (void)skip(1, r2->from, NULL);
707: nchar = 0;
708: for (nline=0; nline < r1->to - r1->from; nline++) {
709: do {
710: c = getc(fp[0]);
711: d = getc(fp[1]);
712: if (c == -1 || d== -1)
713: return (-1);
714: nchar++;
715: if (c != d) {
716: repos(nchar);
717: return (0);
718: }
719: } while (c != '\n');
720: }
721: repos(nchar);
722: return (1);
723: }
724:
725: static void
726: repos(int nchar)
727: {
728: int i;
729:
730: for (i = 0; i < 2; i++)
731: (void)fseek(fp[i], (long)-nchar, 1);
732: }
733:
734: /*
735: * collect an editing script for later regurgitation
736: */
737: static int
738: edit(struct diff *diff, int fdup, int j)
739: {
740: if (((fdup + 1) & eflag) == 0)
741: return (j);
742: j++;
743: overlap[j] = !fdup;
744: if (!fdup)
745: overlapcnt++;
746: de[j].old.from = diff->old.from;
747: de[j].old.to = diff->old.to;
748: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
749: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
750: return (j);
751: }
752:
753: /* regurgitate */
754: static int
755: edscript(int n)
756: {
757: int j, k;
1.2 joris 758: char block[BUFSIZ+1];
1.1 joris 759:
760: for (n = n; n > 0; n--) {
761: if (!oflag || !overlap[n])
762: prange(&de[n].old);
763: else
764: diff_output("%da\n=======\n", de[n].old.to -1);
765: (void)fseek(fp[2], (long)de[n].new.from, 0);
766: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
767: j = k > BUFSIZ ? BUFSIZ : k;
1.6 xsa 768: if (fread(block, (size_t)1, (size_t)j, fp[2]) != (size_t)j)
1.1 joris 769: return (-1);
770: block[j] = '\0';
771: diff_output("%s", block);
772: }
1.2 joris 773:
1.1 joris 774: if (!oflag || !overlap[n])
775: diff_output(".\n");
776: else {
777: diff_output("%s\n.\n", f3mark);
778: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
779: }
780: }
781:
782: return (overlapcnt);
783: }
784:
785: static void
786: increase(void)
787: {
788: struct diff *p;
789: char *q;
790: size_t newsz, incr;
791:
792: /* are the memset(3) calls needed? */
793: newsz = szchanges == 0 ? 64 : 2 * szchanges;
794: incr = newsz - szchanges;
795:
1.8 joris 796: p = xrealloc(d13, newsz * sizeof(struct diff));
1.1 joris 797: memset(p + szchanges, 0, incr * sizeof(struct diff));
798: d13 = p;
1.8 joris 799: p = xrealloc(d23, newsz * sizeof(struct diff));
1.1 joris 800: memset(p + szchanges, 0, incr * sizeof(struct diff));
801: d23 = p;
1.8 joris 802: p = xrealloc(de, newsz * sizeof(struct diff));
1.1 joris 803: memset(p + szchanges, 0, incr * sizeof(struct diff));
804: de = p;
1.8 joris 805: q = xrealloc(overlap, newsz * sizeof(char));
1.1 joris 806: memset(q + szchanges, 0, incr * sizeof(char));
807: overlap = q;
808: szchanges = newsz;
809: }