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