1 74b37681 2019-02-07 stsp /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $ */
4 74b37681 2019-02-07 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 74b37681 2019-02-07 stsp * All rights reserved.
7 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
8 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code and documentation must retain the above
11 74b37681 2019-02-07 stsp * copyright notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
13 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
14 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
15 74b37681 2019-02-07 stsp * 3. All advertising materials mentioning features or use of this software
16 74b37681 2019-02-07 stsp * must display the following acknowledgement:
17 74b37681 2019-02-07 stsp * This product includes software developed or owned by Caldera
18 74b37681 2019-02-07 stsp * International, Inc.
19 74b37681 2019-02-07 stsp * 4. Neither the name of Caldera International, Inc. nor the names of other
20 74b37681 2019-02-07 stsp * contributors may be used to endorse or promote products derived from
21 74b37681 2019-02-07 stsp * this software without specific prior written permission.
23 74b37681 2019-02-07 stsp * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 74b37681 2019-02-07 stsp * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 74b37681 2019-02-07 stsp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 74b37681 2019-02-07 stsp * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 74b37681 2019-02-07 stsp * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 74b37681 2019-02-07 stsp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 74b37681 2019-02-07 stsp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 74b37681 2019-02-07 stsp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 74b37681 2019-02-07 stsp * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 74b37681 2019-02-07 stsp * POSSIBILITY OF SUCH DAMAGE.
37 74b37681 2019-02-07 stsp * Copyright (c) 1991, 1993
38 74b37681 2019-02-07 stsp * The Regents of the University of California. All rights reserved.
40 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
41 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
43 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
44 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
45 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
46 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
47 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
48 74b37681 2019-02-07 stsp * 3. Neither the name of the University nor the names of its contributors
49 74b37681 2019-02-07 stsp * may be used to endorse or promote products derived from this software
50 74b37681 2019-02-07 stsp * without specific prior written permission.
52 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 74b37681 2019-02-07 stsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 74b37681 2019-02-07 stsp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 74b37681 2019-02-07 stsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 74b37681 2019-02-07 stsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 74b37681 2019-02-07 stsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 74b37681 2019-02-07 stsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 74b37681 2019-02-07 stsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 74b37681 2019-02-07 stsp * SUCH DAMAGE.
64 74b37681 2019-02-07 stsp * @(#)diff3.c 8.1 (Berkeley) 6/6/93
67 c03c6cb3 2019-02-08 stsp #include <sys/stat.h>
68 c03c6cb3 2019-02-08 stsp #include <sys/queue.h>
70 74b37681 2019-02-07 stsp #include <ctype.h>
71 c03c6cb3 2019-02-08 stsp #include <limits.h>
72 74b37681 2019-02-07 stsp #include <stdio.h>
73 c03c6cb3 2019-02-08 stsp #include <stdarg.h>
74 74b37681 2019-02-07 stsp #include <stdlib.h>
75 74b37681 2019-02-07 stsp #include <string.h>
76 74b37681 2019-02-07 stsp #include <time.h>
77 74b37681 2019-02-07 stsp #include <unistd.h>
79 c03c6cb3 2019-02-08 stsp #include "got_error.h"
80 c03c6cb3 2019-02-08 stsp #include "got_opentemp.h"
81 c03c6cb3 2019-02-08 stsp #include "got_object.h"
83 c03c6cb3 2019-02-08 stsp #include "buf.h"
84 c03c6cb3 2019-02-08 stsp #include "rcsutil.h"
85 c03c6cb3 2019-02-08 stsp #include "got_lib_diff.h"
86 c03c6cb3 2019-02-08 stsp #include "worklist.h"
88 0d26ce36 2019-02-08 stsp #ifndef nitems
89 0d26ce36 2019-02-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
92 74b37681 2019-02-07 stsp /* diff3 - 3-way differential file comparison */
94 74b37681 2019-02-07 stsp /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
96 74b37681 2019-02-07 stsp * d13 = diff report on f1 vs f3
97 74b37681 2019-02-07 stsp * d23 = diff report on f2 vs f3
98 74b37681 2019-02-07 stsp * f1, f2, f3 the 3 files
99 74b37681 2019-02-07 stsp * if changes in f1 overlap with changes in f3, m1 and m3 are used
100 74b37681 2019-02-07 stsp * to mark the overlaps; otherwise, the file names f1 and f3 are used
101 74b37681 2019-02-07 stsp * (only for options E and X).
105 74b37681 2019-02-07 stsp * "from" is first in range of changed lines; "to" is last+1
106 74b37681 2019-02-07 stsp * from=to=line after point of insertion for added lines.
108 74b37681 2019-02-07 stsp struct range {
113 74b37681 2019-02-07 stsp struct diff {
114 74b37681 2019-02-07 stsp struct range old;
115 74b37681 2019-02-07 stsp struct range new;
118 078e971d 2019-02-08 stsp struct diff3_state {
119 078e971d 2019-02-08 stsp size_t szchanges;
121 078e971d 2019-02-08 stsp struct diff *d13;
122 078e971d 2019-02-08 stsp struct diff *d23;
125 078e971d 2019-02-08 stsp * "de" is used to gather editing scripts. These are later spewed out
126 078e971d 2019-02-08 stsp * in reverse order. Its first element must be all zero, the "new"
127 078e971d 2019-02-08 stsp * component of "de" contains line positions or byte positions
128 078e971d 2019-02-08 stsp * depending on when you look (!?). Array overlap indicates which
129 078e971d 2019-02-08 stsp * sections in "de" correspond to lines that are different in all
130 078e971d 2019-02-08 stsp * three files.
132 078e971d 2019-02-08 stsp struct diff *de;
133 078e971d 2019-02-08 stsp char *overlap;
134 078e971d 2019-02-08 stsp int overlapcnt;
135 078e971d 2019-02-08 stsp FILE *fp[3];
136 078e971d 2019-02-08 stsp int cline[3]; /* # of the last-read line in each file (0-2) */
139 078e971d 2019-02-08 stsp * the latest known correspondence between line numbers of the 3 files
140 078e971d 2019-02-08 stsp * is stored in last[1-3];
142 078e971d 2019-02-08 stsp int last[4];
146 078e971d 2019-02-08 stsp char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
149 dfc4f8fe 2019-02-08 stsp size_t bufsize;
151 5176f942 2019-02-08 stsp BUF *diffbuf;
155 078e971d 2019-02-08 stsp static int duplicate(struct range *, struct range *, struct diff3_state *);
156 078e971d 2019-02-08 stsp static int edit(struct diff *, int, int, struct diff3_state *);
157 dfc4f8fe 2019-02-08 stsp static char *getchange(FILE *, struct diff3_state *);
158 dfc4f8fe 2019-02-08 stsp static char *get_line(FILE *, size_t *, struct diff3_state *);
159 74b37681 2019-02-07 stsp static int number(char **);
160 b90524b1 2019-02-08 stsp static const struct got_error *readin(size_t *, char *, struct diff **,
161 b90524b1 2019-02-08 stsp struct diff3_state *);
162 c03c6cb3 2019-02-08 stsp static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
163 078e971d 2019-02-08 stsp static int skip(int, int, char *, struct diff3_state *);
164 078e971d 2019-02-08 stsp static int edscript(int, struct diff3_state *);
165 078e971d 2019-02-08 stsp static int merge(size_t, size_t, struct diff3_state *);
166 078e971d 2019-02-08 stsp static void change(int, struct range *, int, struct diff3_state *);
167 078e971d 2019-02-08 stsp static void keep(int, struct range *, struct diff3_state *);
168 5176f942 2019-02-08 stsp static void prange(struct range *, struct diff3_state *);
169 078e971d 2019-02-08 stsp static void repos(int, struct diff3_state *);
170 5176f942 2019-02-08 stsp static void separate(const char *, struct diff3_state *);
171 078e971d 2019-02-08 stsp static const struct got_error *increase(struct diff3_state *);
172 c03c6cb3 2019-02-08 stsp static const struct got_error *diff3_internal(char *, char *, char *,
173 078e971d 2019-02-08 stsp char *, char *, const char *, const char *, struct diff3_state *);
175 c03c6cb3 2019-02-08 stsp static const struct got_error *
176 5176f942 2019-02-08 stsp diff_output(BUF *diffbuf, const char *fmt, ...)
178 c03c6cb3 2019-02-08 stsp va_list vap;
181 c03c6cb3 2019-02-08 stsp size_t newsize;
183 c03c6cb3 2019-02-08 stsp va_start(vap, fmt);
184 c03c6cb3 2019-02-08 stsp i = vasprintf(&str, fmt, vap);
185 c03c6cb3 2019-02-08 stsp va_end(vap);
186 c03c6cb3 2019-02-08 stsp if (i == -1)
187 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
188 5176f942 2019-02-08 stsp buf_append(&newsize, diffbuf, str, strlen(str));
190 c03c6cb3 2019-02-08 stsp return NULL;
193 c03c6cb3 2019-02-08 stsp static const struct got_error*
194 c03c6cb3 2019-02-08 stsp diffreg(BUF **d, const char *path1, const char *path2)
196 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
197 c03c6cb3 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
198 c03c6cb3 2019-02-08 stsp char *outpath = NULL;
199 c03c6cb3 2019-02-08 stsp struct got_diff_state ds;
200 c03c6cb3 2019-02-08 stsp struct got_diff_args args;
205 c03c6cb3 2019-02-08 stsp f1 = fopen(path1, "r");
206 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
207 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
210 c03c6cb3 2019-02-08 stsp f2 = fopen(path2, "r");
211 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
212 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
216 c03c6cb3 2019-02-08 stsp err = got_opentemp_named(&outpath, &outfile, "/tmp/got-diff");
220 c03c6cb3 2019-02-08 stsp memset(&ds, 0, sizeof(ds));
221 c03c6cb3 2019-02-08 stsp /* XXX should stat buffers be passed in args instead of ds? */
222 c03c6cb3 2019-02-08 stsp if (stat(path1, &ds.stb1) == -1) {
223 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
226 c03c6cb3 2019-02-08 stsp if (stat(path2, &ds.stb2) == -1) {
227 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
231 c03c6cb3 2019-02-08 stsp memset(&args, 0, sizeof(args));
232 c03c6cb3 2019-02-08 stsp args.diff_format = D_NORMAL;
233 c03c6cb3 2019-02-08 stsp args.label[0] = "";
234 c03c6cb3 2019-02-08 stsp args.label[1] = "";
235 c03c6cb3 2019-02-08 stsp args.diff_context = 0;
237 c03c6cb3 2019-02-08 stsp err = got_diffreg(&res, f1, f2, D_FORCEASCII, &args, &ds,
238 c03c6cb3 2019-02-08 stsp outfile, NULL);
242 c03c6cb3 2019-02-08 stsp *d = buf_load(outpath);
243 c03c6cb3 2019-02-08 stsp if (*d == NULL)
244 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
246 c03c6cb3 2019-02-08 stsp free(outpath);
247 c03c6cb3 2019-02-08 stsp if (outfile)
248 c03c6cb3 2019-02-08 stsp fclose(outfile);
250 c03c6cb3 2019-02-08 stsp fclose(f1);
252 c03c6cb3 2019-02-08 stsp fclose(f2);
253 c03c6cb3 2019-02-08 stsp return err;
257 74b37681 2019-02-07 stsp * For merge(1).
259 c03c6cb3 2019-02-08 stsp const struct got_error *
260 9f98de9c 2019-02-08 stsp got_merge_diff3(int outfd, const char *p1, const char *p2, const char *p3)
262 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
263 c03c6cb3 2019-02-08 stsp char *dp13, *dp23, *path1, *path2, *path3;
264 74b37681 2019-02-07 stsp BUF *b1, *b2, *b3, *d1, *d2, *diffb;
265 74b37681 2019-02-07 stsp u_char *data, *patch;
266 74b37681 2019-02-07 stsp size_t dlen, plen;
267 c03c6cb3 2019-02-08 stsp struct wklhead temp_files;
268 078e971d 2019-02-08 stsp struct diff3_state *d3s;
271 078e971d 2019-02-08 stsp d3s = calloc(1, sizeof(*d3s));
272 078e971d 2019-02-08 stsp if (d3s == NULL)
273 078e971d 2019-02-08 stsp return got_error_from_errno();
274 078e971d 2019-02-08 stsp d3s->eflag = 3; /* default -E for compatibility with former RCS */
275 078e971d 2019-02-08 stsp d3s->oflag = 1; /* default -E for compatibility with former RCS */
277 74b37681 2019-02-07 stsp b1 = b2 = b3 = d1 = d2 = diffb = NULL;
278 74b37681 2019-02-07 stsp dp13 = dp23 = path1 = path2 = path3 = NULL;
279 74b37681 2019-02-07 stsp data = patch = NULL;
281 0ec6f014 2019-02-08 stsp if ((b1 = buf_load(p1)) == NULL)
283 0ec6f014 2019-02-08 stsp if ((b2 = buf_load(p2)) == NULL)
285 0ec6f014 2019-02-08 stsp if ((b3 = buf_load(p3)) == NULL)
288 74b37681 2019-02-07 stsp diffb = buf_alloc(128);
290 c03c6cb3 2019-02-08 stsp if (asprintf(&path1, "/tmp/got-diff1.XXXXXXXX") == -1) {
291 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
294 c03c6cb3 2019-02-08 stsp if (asprintf(&path2, "/tmp/got-diff2.XXXXXXXX") == -1) {
295 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
298 c03c6cb3 2019-02-08 stsp if (asprintf(&path3, "/tmp/got-diff3.XXXXXXXX") == -1) {
299 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
303 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b1, path1, &temp_files);
306 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b2, path2, &temp_files);
309 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b3, path3, &temp_files);
313 74b37681 2019-02-07 stsp buf_free(b2);
316 c03c6cb3 2019-02-08 stsp err = diffreg(&d1, path1, path3);
318 74b37681 2019-02-07 stsp buf_free(diffb);
319 74b37681 2019-02-07 stsp diffb = NULL;
323 c03c6cb3 2019-02-08 stsp err = diffreg(&d2, path2, path3);
325 c03c6cb3 2019-02-08 stsp buf_free(diffb);
326 c03c6cb3 2019-02-08 stsp diffb = NULL;
330 c03c6cb3 2019-02-08 stsp if (asprintf(&dp13, "/tmp/got-d13.XXXXXXXXXX") == -1) {
331 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
334 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d1, dp13, &temp_files);
338 74b37681 2019-02-07 stsp buf_free(d1);
341 c03c6cb3 2019-02-08 stsp if (asprintf(&dp23, "/tmp/got-d23.XXXXXXXXXX") == -1) {
342 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
345 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d2, dp23, &temp_files);
349 74b37681 2019-02-07 stsp buf_free(d2);
352 5176f942 2019-02-08 stsp d3s->diffbuf = diffb;
353 0ec6f014 2019-02-08 stsp err = diff3_internal(dp13, dp23, path1, path2, path3, p1, p3,
356 74b37681 2019-02-07 stsp buf_free(diffb);
357 74b37681 2019-02-07 stsp diffb = NULL;
361 74b37681 2019-02-07 stsp plen = buf_len(diffb);
362 74b37681 2019-02-07 stsp patch = buf_release(diffb);
363 74b37681 2019-02-07 stsp dlen = buf_len(b1);
364 74b37681 2019-02-07 stsp data = buf_release(b1);
366 5a3ad9e0 2019-02-08 stsp diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines);
368 74b37681 2019-02-07 stsp buf_free(b2);
369 74b37681 2019-02-07 stsp buf_free(b3);
370 74b37681 2019-02-07 stsp buf_free(d1);
371 74b37681 2019-02-07 stsp buf_free(d2);
373 74b37681 2019-02-07 stsp (void)unlink(path1);
374 74b37681 2019-02-07 stsp (void)unlink(path2);
375 74b37681 2019-02-07 stsp (void)unlink(path3);
376 74b37681 2019-02-07 stsp (void)unlink(dp13);
377 74b37681 2019-02-07 stsp (void)unlink(dp23);
379 74b37681 2019-02-07 stsp free(path1);
380 74b37681 2019-02-07 stsp free(path2);
381 74b37681 2019-02-07 stsp free(path3);
382 74b37681 2019-02-07 stsp free(dp13);
383 74b37681 2019-02-07 stsp free(dp23);
384 74b37681 2019-02-07 stsp free(data);
385 74b37681 2019-02-07 stsp free(patch);
387 c03c6cb3 2019-02-08 stsp worklist_clean(&temp_files, worklist_unlink);
389 0d26ce36 2019-02-08 stsp for (i = 0; i < nitems(d3s->fp); i++) {
390 0d26ce36 2019-02-08 stsp if (d3s->fp[i])
391 0d26ce36 2019-02-08 stsp fclose(d3s->fp[i]);
394 5a3ad9e0 2019-02-08 stsp if (err == NULL && diffb) {
395 62f59172 2019-02-08 stsp if (buf_write_fd(diffb, outfd) < 0)
396 62f59172 2019-02-08 stsp err = got_error_from_errno();
398 62f59172 2019-02-08 stsp buf_free(diffb);
399 c03c6cb3 2019-02-08 stsp return err;
402 c03c6cb3 2019-02-08 stsp static const struct got_error *
403 c03c6cb3 2019-02-08 stsp diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3,
404 078e971d 2019-02-08 stsp const char *fmark, const char *rmark, struct diff3_state *d3s)
406 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
407 74b37681 2019-02-07 stsp ssize_t m, n;
410 078e971d 2019-02-08 stsp i = snprintf(d3s->f1mark, sizeof(d3s->f1mark), "<<<<<<< %s", fmark);
411 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f1mark))
412 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
414 078e971d 2019-02-08 stsp i = snprintf(d3s->f3mark, sizeof(d3s->f3mark), ">>>>>>> %s", rmark);
415 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f3mark))
416 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
418 078e971d 2019-02-08 stsp err = increase(d3s);
420 c03c6cb3 2019-02-08 stsp return err;
422 b90524b1 2019-02-08 stsp err = readin(&m, dp13, &d3s->d13, d3s);
424 b90524b1 2019-02-08 stsp return err;
425 b90524b1 2019-02-08 stsp err = readin(&n, dp23, &d3s->d23, d3s);
427 b90524b1 2019-02-08 stsp return err;
429 078e971d 2019-02-08 stsp if ((d3s->fp[0] = fopen(path1, "r")) == NULL)
430 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
431 078e971d 2019-02-08 stsp if ((d3s->fp[1] = fopen(path2, "r")) == NULL)
432 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
433 078e971d 2019-02-08 stsp if ((d3s->fp[2] = fopen(path3, "r")) == NULL)
434 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
436 078e971d 2019-02-08 stsp if (merge(m, n, d3s) < 0)
437 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
438 c03c6cb3 2019-02-08 stsp return NULL;
442 74b37681 2019-02-07 stsp ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
444 74b37681 2019-02-07 stsp char op, *ep;
445 74b37681 2019-02-07 stsp struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
446 74b37681 2019-02-07 stsp int start, end, i, lineno;
447 74b37681 2019-02-07 stsp u_char tmp;
449 74b37681 2019-02-07 stsp dlp = TAILQ_FIRST(&(dlines->l_lines));
450 74b37681 2019-02-07 stsp lp = TAILQ_FIRST(&(plines->l_lines));
453 74b37681 2019-02-07 stsp for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
454 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list)) {
455 74b37681 2019-02-07 stsp /* Skip blank lines */
456 74b37681 2019-02-07 stsp if (lp->l_len < 2)
459 74b37681 2019-02-07 stsp /* NUL-terminate line buffer for strtol() safety. */
460 74b37681 2019-02-07 stsp tmp = lp->l_line[lp->l_len - 1];
461 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = '\0';
463 74b37681 2019-02-07 stsp /* len - 1 is NUL terminator so we use len - 2 for 'op' */
464 74b37681 2019-02-07 stsp op = lp->l_line[lp->l_len - 2];
465 74b37681 2019-02-07 stsp start = (int)strtol(lp->l_line, &ep, 10);
467 74b37681 2019-02-07 stsp /* Restore the last byte of the buffer */
468 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = tmp;
470 74b37681 2019-02-07 stsp if (op == 'a') {
471 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
472 74b37681 2019-02-07 stsp start < 0 || *ep != 'a')
474 74b37681 2019-02-07 stsp } else if (op == 'c') {
475 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
476 74b37681 2019-02-07 stsp start < 0 || (*ep != ',' && *ep != 'c'))
479 74b37681 2019-02-07 stsp if (*ep == ',') {
481 74b37681 2019-02-07 stsp end = (int)strtol(ep, &ep, 10);
482 74b37681 2019-02-07 stsp if (end < 0 || *ep != 'c')
485 74b37681 2019-02-07 stsp end = start;
491 74b37681 2019-02-07 stsp if (dlp == NULL)
493 74b37681 2019-02-07 stsp if (dlp->l_lineno == start)
495 74b37681 2019-02-07 stsp if (dlp->l_lineno > start) {
496 74b37681 2019-02-07 stsp dlp = TAILQ_PREV(dlp, tqh, l_list);
497 74b37681 2019-02-07 stsp } else if (dlp->l_lineno < start) {
498 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
499 74b37681 2019-02-07 stsp if (ndlp->l_lineno > start)
501 74b37681 2019-02-07 stsp dlp = ndlp;
505 74b37681 2019-02-07 stsp if (dlp == NULL)
509 74b37681 2019-02-07 stsp if (op == 'c') {
510 74b37681 2019-02-07 stsp insert_after = TAILQ_PREV(dlp, tqh, l_list);
511 74b37681 2019-02-07 stsp for (i = 0; i <= (end - start); i++) {
512 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
513 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
514 74b37681 2019-02-07 stsp dlp = ndlp;
516 74b37681 2019-02-07 stsp dlp = insert_after;
519 74b37681 2019-02-07 stsp if (op == 'a' || op == 'c') {
522 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list);
523 74b37681 2019-02-07 stsp if (lp == NULL)
526 74b37681 2019-02-07 stsp if (!memcmp(lp->l_line, ".", 1))
529 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
530 74b37681 2019-02-07 stsp TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
531 74b37681 2019-02-07 stsp lp, l_list);
534 74b37681 2019-02-07 stsp lp->l_lineno = start;
540 74b37681 2019-02-07 stsp * always resort lines as the markers might be put at the
541 74b37681 2019-02-07 stsp * same line as we first started editing.
543 74b37681 2019-02-07 stsp lineno = 0;
544 74b37681 2019-02-07 stsp TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
545 74b37681 2019-02-07 stsp sort->l_lineno = lineno++;
546 74b37681 2019-02-07 stsp dlines->l_nblines = lineno - 1;
549 74b37681 2019-02-07 stsp return (0);
553 74b37681 2019-02-07 stsp * Pick up the line numbers of all changes from one change file.
554 74b37681 2019-02-07 stsp * (This puts the numbers in a vector, which is not strictly necessary,
555 74b37681 2019-02-07 stsp * since the vector is processed in one sequential pass.
556 74b37681 2019-02-07 stsp * The vector could be optimized out of existence)
558 b90524b1 2019-02-08 stsp static const struct got_error *
559 b90524b1 2019-02-08 stsp readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
561 b90524b1 2019-02-08 stsp const struct got_error *err = NULL;
562 74b37681 2019-02-07 stsp int a, b, c, d;
563 74b37681 2019-02-07 stsp char kind, *p;
566 078e971d 2019-02-08 stsp d3s->fp[0] = fopen(name, "r");
567 078e971d 2019-02-08 stsp if (d3s->fp[0] == NULL)
568 b90524b1 2019-02-08 stsp return got_error_from_errno();
569 dfc4f8fe 2019-02-08 stsp for (i = 0; (p = getchange(d3s->fp[0], d3s)); i++) {
570 b90524b1 2019-02-08 stsp if (i >= d3s->szchanges - 1) {
571 b90524b1 2019-02-08 stsp err = increase(d3s);
573 b90524b1 2019-02-08 stsp return err;
575 74b37681 2019-02-07 stsp a = b = number(&p);
576 74b37681 2019-02-07 stsp if (*p == ',') {
578 74b37681 2019-02-07 stsp b = number(&p);
580 74b37681 2019-02-07 stsp kind = *p++;
581 74b37681 2019-02-07 stsp c = d = number(&p);
582 74b37681 2019-02-07 stsp if (*p==',') {
584 74b37681 2019-02-07 stsp d = number(&p);
586 74b37681 2019-02-07 stsp if (kind == 'a')
588 74b37681 2019-02-07 stsp if (kind == 'd')
592 74b37681 2019-02-07 stsp (*dd)[i].old.from = a;
593 74b37681 2019-02-07 stsp (*dd)[i].old.to = b;
594 74b37681 2019-02-07 stsp (*dd)[i].new.from = c;
595 74b37681 2019-02-07 stsp (*dd)[i].new.to = d;
599 74b37681 2019-02-07 stsp (*dd)[i].old.from = (*dd)[i-1].old.to;
600 74b37681 2019-02-07 stsp (*dd)[i].new.from = (*dd)[i-1].new.to;
603 078e971d 2019-02-08 stsp (void)fclose(d3s->fp[0]);
605 b90524b1 2019-02-08 stsp return NULL;
609 74b37681 2019-02-07 stsp number(char **lc)
614 74b37681 2019-02-07 stsp while (isdigit((unsigned char)(**lc)))
615 74b37681 2019-02-07 stsp nn = nn*10 + *(*lc)++ - '0';
617 74b37681 2019-02-07 stsp return (nn);
620 74b37681 2019-02-07 stsp static char *
621 dfc4f8fe 2019-02-08 stsp getchange(FILE *b, struct diff3_state *d3s)
623 74b37681 2019-02-07 stsp char *line;
625 dfc4f8fe 2019-02-08 stsp while ((line = get_line(b, NULL, d3s))) {
626 74b37681 2019-02-07 stsp if (isdigit((unsigned char)line[0]))
627 74b37681 2019-02-07 stsp return (line);
630 74b37681 2019-02-07 stsp return (NULL);
633 74b37681 2019-02-07 stsp static char *
634 dfc4f8fe 2019-02-08 stsp get_line(FILE *b, size_t *n, struct diff3_state *d3s)
637 74b37681 2019-02-07 stsp size_t len;
640 74b37681 2019-02-07 stsp if ((cp = fgetln(b, &len)) == NULL)
641 74b37681 2019-02-07 stsp return (NULL);
643 74b37681 2019-02-07 stsp if (cp[len - 1] != '\n')
645 dfc4f8fe 2019-02-08 stsp if (len + 1 > d3s->bufsize) {
647 dfc4f8fe 2019-02-08 stsp d3s->bufsize += 1024;
648 dfc4f8fe 2019-02-08 stsp } while (len + 1 > d3s->bufsize);
649 dfc4f8fe 2019-02-08 stsp new = reallocarray(d3s->buf, 1, d3s->bufsize);
650 c03c6cb3 2019-02-08 stsp if (new == NULL)
651 c03c6cb3 2019-02-08 stsp return NULL;
652 dfc4f8fe 2019-02-08 stsp d3s->buf = new;
654 dfc4f8fe 2019-02-08 stsp memcpy(d3s->buf, cp, len - 1);
655 dfc4f8fe 2019-02-08 stsp d3s->buf[len - 1] = '\n';
656 dfc4f8fe 2019-02-08 stsp d3s->buf[len] = '\0';
657 74b37681 2019-02-07 stsp if (n != NULL)
660 dfc4f8fe 2019-02-08 stsp return (d3s->buf);
664 078e971d 2019-02-08 stsp merge(size_t m1, size_t m2, struct diff3_state *d3s)
666 74b37681 2019-02-07 stsp struct diff *d1, *d2, *d3;
667 74b37681 2019-02-07 stsp int dpl, j, t1, t2;
669 078e971d 2019-02-08 stsp d1 = d3s->d13;
670 078e971d 2019-02-08 stsp d2 = d3s->d23;
673 078e971d 2019-02-08 stsp t1 = (d1 < d3s->d13 + m1);
674 078e971d 2019-02-08 stsp t2 = (d2 < d3s->d23 + m2);
675 74b37681 2019-02-07 stsp if (!t1 && !t2)
678 078e971d 2019-02-08 stsp if (d3s->debug) {
679 74b37681 2019-02-07 stsp printf("%d,%d=%d,%d %d,%d=%d,%d\n",
680 74b37681 2019-02-07 stsp d1->old.from, d1->old.to,
681 74b37681 2019-02-07 stsp d1->new.from, d1->new.to,
682 74b37681 2019-02-07 stsp d2->old.from, d2->old.to,
683 74b37681 2019-02-07 stsp d2->new.from, d2->new.to);
686 74b37681 2019-02-07 stsp /* first file is different from others */
687 74b37681 2019-02-07 stsp if (!t2 || (t1 && d1->new.to < d2->new.from)) {
688 74b37681 2019-02-07 stsp /* stuff peculiar to 1st file */
689 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
690 5176f942 2019-02-08 stsp separate("1", d3s);
691 078e971d 2019-02-08 stsp change(1, &d1->old, 0, d3s);
692 078e971d 2019-02-08 stsp keep(2, &d1->new, d3s);
693 078e971d 2019-02-08 stsp change(3, &d1->new, 0, d3s);
699 74b37681 2019-02-07 stsp /* second file is different from others */
700 74b37681 2019-02-07 stsp if (!t1 || (t2 && d2->new.to < d1->new.from)) {
701 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
702 5176f942 2019-02-08 stsp separate("2", d3s);
703 078e971d 2019-02-08 stsp keep(1, &d2->new, d3s);
704 078e971d 2019-02-08 stsp change(2, &d2->old, 0, d3s);
705 078e971d 2019-02-08 stsp change(3, &d2->new, 0, d3s);
712 74b37681 2019-02-07 stsp * Merge overlapping changes in first file
713 74b37681 2019-02-07 stsp * this happens after extension (see below).
715 078e971d 2019-02-08 stsp if (d1 + 1 < d3s->d13 + m1 && d1->new.to >= d1[1].new.from) {
716 74b37681 2019-02-07 stsp d1[1].old.from = d1->old.from;
717 74b37681 2019-02-07 stsp d1[1].new.from = d1->new.from;
722 74b37681 2019-02-07 stsp /* merge overlapping changes in second */
723 078e971d 2019-02-08 stsp if (d2 + 1 < d3s->d23 + m2 && d2->new.to >= d2[1].new.from) {
724 74b37681 2019-02-07 stsp d2[1].old.from = d2->old.from;
725 74b37681 2019-02-07 stsp d2[1].new.from = d2->new.from;
729 74b37681 2019-02-07 stsp /* stuff peculiar to third file or different in all */
730 74b37681 2019-02-07 stsp if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
731 078e971d 2019-02-08 stsp dpl = duplicate(&d1->old, &d2->old, d3s);
732 74b37681 2019-02-07 stsp if (dpl == -1)
733 74b37681 2019-02-07 stsp return (-1);
736 74b37681 2019-02-07 stsp * dpl = 0 means all files differ
737 74b37681 2019-02-07 stsp * dpl = 1 means files 1 and 2 identical
739 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
740 5176f942 2019-02-08 stsp separate(dpl ? "3" : "", d3s);
741 078e971d 2019-02-08 stsp change(1, &d1->old, dpl, d3s);
742 078e971d 2019-02-08 stsp change(2, &d2->old, 0, d3s);
743 74b37681 2019-02-07 stsp d3 = d1->old.to > d1->old.from ? d1 : d2;
744 078e971d 2019-02-08 stsp change(3, &d3->new, 0, d3s);
746 078e971d 2019-02-08 stsp j = edit(d1, dpl, j, d3s);
753 74b37681 2019-02-07 stsp * Overlapping changes from file 1 and 2; extend changes
754 74b37681 2019-02-07 stsp * appropriately to make them coincide.
756 74b37681 2019-02-07 stsp if (d1->new.from < d2->new.from) {
757 74b37681 2019-02-07 stsp d2->old.from -= d2->new.from-d1->new.from;
758 74b37681 2019-02-07 stsp d2->new.from = d1->new.from;
759 74b37681 2019-02-07 stsp } else if (d2->new.from < d1->new.from) {
760 74b37681 2019-02-07 stsp d1->old.from -= d1->new.from-d2->new.from;
761 74b37681 2019-02-07 stsp d1->new.from = d2->new.from;
763 74b37681 2019-02-07 stsp if (d1->new.to > d2->new.to) {
764 74b37681 2019-02-07 stsp d2->old.to += d1->new.to - d2->new.to;
765 74b37681 2019-02-07 stsp d2->new.to = d1->new.to;
766 74b37681 2019-02-07 stsp } else if (d2->new.to > d1->new.to) {
767 74b37681 2019-02-07 stsp d1->old.to += d2->new.to - d1->new.to;
768 74b37681 2019-02-07 stsp d1->new.to = d2->new.to;
772 078e971d 2019-02-08 stsp return (edscript(j, d3s));
775 74b37681 2019-02-07 stsp static void
776 5176f942 2019-02-08 stsp separate(const char *s, struct diff3_state *d3s)
778 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "====%s\n", s);
782 74b37681 2019-02-07 stsp * The range of lines rold.from thru rold.to in file i is to be changed.
783 74b37681 2019-02-07 stsp * It is to be printed only if it does not duplicate something to be
784 74b37681 2019-02-07 stsp * printed later.
786 74b37681 2019-02-07 stsp static void
787 078e971d 2019-02-08 stsp change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
789 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%d:", i);
790 078e971d 2019-02-08 stsp d3s->last[i] = rold->to;
791 5176f942 2019-02-08 stsp prange(rold, d3s);
792 078e971d 2019-02-08 stsp if (fdup || d3s->debug)
795 078e971d 2019-02-08 stsp (void)skip(i, rold->from, NULL, d3s);
796 078e971d 2019-02-08 stsp (void)skip(i, rold->to, " ", d3s);
800 74b37681 2019-02-07 stsp * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
802 74b37681 2019-02-07 stsp static void
803 5176f942 2019-02-08 stsp prange(struct range *rold, struct diff3_state *d3s)
805 74b37681 2019-02-07 stsp if (rold->to <= rold->from)
806 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
808 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%d", rold->from);
809 74b37681 2019-02-07 stsp if (rold->to > rold->from+1)
810 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, ",%d", rold->to - 1);
811 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "c\n");
816 74b37681 2019-02-07 stsp * No difference was reported by diff between file 1 (or 2) and file 3,
817 74b37681 2019-02-07 stsp * and an artificial dummy difference (trange) must be ginned up to
818 74b37681 2019-02-07 stsp * correspond to the change reported in the other file.
820 74b37681 2019-02-07 stsp static void
821 078e971d 2019-02-08 stsp keep(int i, struct range *rnew, struct diff3_state *d3s)
824 74b37681 2019-02-07 stsp struct range trange;
826 078e971d 2019-02-08 stsp delta = d3s->last[3] - d3s->last[i];
827 74b37681 2019-02-07 stsp trange.from = rnew->from - delta;
828 74b37681 2019-02-07 stsp trange.to = rnew->to - delta;
829 078e971d 2019-02-08 stsp change(i, &trange, 1, d3s);
833 74b37681 2019-02-07 stsp * skip to just before line number from in file "i". If "pr" is non-NULL,
834 74b37681 2019-02-07 stsp * print all skipped stuff with string pr as a prefix.
837 078e971d 2019-02-08 stsp skip(int i, int from, char *pr, struct diff3_state *d3s)
839 74b37681 2019-02-07 stsp size_t j, n;
840 74b37681 2019-02-07 stsp char *line;
842 078e971d 2019-02-08 stsp for (n = 0; d3s->cline[i] < from - 1; n += j) {
843 dfc4f8fe 2019-02-08 stsp if ((line = get_line(d3s->fp[i], &j, d3s)) == NULL)
844 74b37681 2019-02-07 stsp return (-1);
845 74b37681 2019-02-07 stsp if (pr != NULL)
846 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%s%s", pr, line);
847 078e971d 2019-02-08 stsp d3s->cline[i]++;
849 74b37681 2019-02-07 stsp return ((int) n);
853 74b37681 2019-02-07 stsp * Return 1 or 0 according as the old range (in file 1) contains exactly
854 74b37681 2019-02-07 stsp * the same data as the new range (in file 2).
857 078e971d 2019-02-08 stsp duplicate(struct range *r1, struct range *r2, struct diff3_state *d3s)
863 74b37681 2019-02-07 stsp if (r1->to-r1->from != r2->to-r2->from)
864 74b37681 2019-02-07 stsp return (0);
865 078e971d 2019-02-08 stsp (void)skip(0, r1->from, NULL, d3s);
866 078e971d 2019-02-08 stsp (void)skip(1, r2->from, NULL, d3s);
868 74b37681 2019-02-07 stsp for (nline=0; nline < r1->to - r1->from; nline++) {
870 078e971d 2019-02-08 stsp c = getc(d3s->fp[0]);
871 078e971d 2019-02-08 stsp d = getc(d3s->fp[1]);
872 74b37681 2019-02-07 stsp if (c == -1 || d== -1)
873 74b37681 2019-02-07 stsp return (-1);
875 74b37681 2019-02-07 stsp if (c != d) {
876 078e971d 2019-02-08 stsp repos(nchar, d3s);
877 74b37681 2019-02-07 stsp return (0);
879 74b37681 2019-02-07 stsp } while (c != '\n');
881 078e971d 2019-02-08 stsp repos(nchar, d3s);
882 74b37681 2019-02-07 stsp return (1);
885 74b37681 2019-02-07 stsp static void
886 078e971d 2019-02-08 stsp repos(int nchar, struct diff3_state *d3s)
890 74b37681 2019-02-07 stsp for (i = 0; i < 2; i++)
891 078e971d 2019-02-08 stsp (void)fseek(d3s->fp[i], (long)-nchar, SEEK_CUR);
895 74b37681 2019-02-07 stsp * collect an editing script for later regurgitation
898 078e971d 2019-02-08 stsp edit(struct diff *diff, int fdup, int j, struct diff3_state *d3s)
900 078e971d 2019-02-08 stsp if (((fdup + 1) & d3s->eflag) == 0)
901 74b37681 2019-02-07 stsp return (j);
903 078e971d 2019-02-08 stsp d3s->overlap[j] = !fdup;
905 078e971d 2019-02-08 stsp d3s->overlapcnt++;
906 078e971d 2019-02-08 stsp d3s->de[j].old.from = diff->old.from;
907 078e971d 2019-02-08 stsp d3s->de[j].old.to = diff->old.to;
908 078e971d 2019-02-08 stsp d3s->de[j].new.from =
909 078e971d 2019-02-08 stsp d3s->de[j-1].new.to + skip(2, diff->new.from, NULL, d3s);
910 078e971d 2019-02-08 stsp d3s->de[j].new.to =
911 078e971d 2019-02-08 stsp d3s->de[j].new.from + skip(2, diff->new.to, NULL, d3s);
912 74b37681 2019-02-07 stsp return (j);
915 74b37681 2019-02-07 stsp /* regurgitate */
917 078e971d 2019-02-08 stsp edscript(int n, struct diff3_state *d3s)
920 74b37681 2019-02-07 stsp char block[BUFSIZ+1];
922 74b37681 2019-02-07 stsp for (; n > 0; n--) {
923 078e971d 2019-02-08 stsp if (!d3s->oflag || !d3s->overlap[n])
924 5176f942 2019-02-08 stsp prange(&d3s->de[n].old, d3s);
926 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%da\n=======\n",
927 5176f942 2019-02-08 stsp d3s->de[n].old.to -1);
928 078e971d 2019-02-08 stsp (void)fseek(d3s->fp[2], (long)d3s->de[n].new.from, SEEK_SET);
929 078e971d 2019-02-08 stsp k = d3s->de[n].new.to - d3s->de[n].new.from;
930 078e971d 2019-02-08 stsp for (; k > 0; k-= j) {
931 74b37681 2019-02-07 stsp j = k > BUFSIZ ? BUFSIZ : k;
932 078e971d 2019-02-08 stsp if (fread(block, 1, j, d3s->fp[2]) != (size_t)j)
933 74b37681 2019-02-07 stsp return (-1);
934 74b37681 2019-02-07 stsp block[j] = '\0';
935 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%s", block);
938 078e971d 2019-02-08 stsp if (!d3s->oflag || !d3s->overlap[n])
939 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, ".\n");
941 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%s\n.\n", d3s->f3mark);
942 5176f942 2019-02-08 stsp diff_output(d3s->diffbuf, "%da\n%s\n.\n",
943 5176f942 2019-02-08 stsp d3s->de[n].old.from - 1, d3s->f1mark);
947 078e971d 2019-02-08 stsp return (d3s->overlapcnt);
950 c03c6cb3 2019-02-08 stsp static const struct got_error *
951 078e971d 2019-02-08 stsp increase(struct diff3_state *d3s)
953 74b37681 2019-02-07 stsp size_t newsz, incr;
954 c03c6cb3 2019-02-08 stsp struct diff *d;
957 74b37681 2019-02-07 stsp /* are the memset(3) calls needed? */
958 078e971d 2019-02-08 stsp newsz = d3s->szchanges == 0 ? 64 : 2 * d3s->szchanges;
959 078e971d 2019-02-08 stsp incr = newsz - d3s->szchanges;
961 078e971d 2019-02-08 stsp d = reallocarray(d3s->d13, newsz, sizeof(*d3s->d13));
962 c03c6cb3 2019-02-08 stsp if (d == NULL)
963 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
964 078e971d 2019-02-08 stsp d3s->d13 = d;
965 078e971d 2019-02-08 stsp memset(d3s->d13 + d3s->szchanges, 0, incr * sizeof(*d3s->d13));
967 078e971d 2019-02-08 stsp d = reallocarray(d3s->d23, newsz, sizeof(*d3s->d23));
968 c03c6cb3 2019-02-08 stsp if (d == NULL)
969 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
970 078e971d 2019-02-08 stsp d3s->d23 = d;
971 078e971d 2019-02-08 stsp memset(d3s->d23 + d3s->szchanges, 0, incr * sizeof(*d3s->d23));
973 078e971d 2019-02-08 stsp d = reallocarray(d3s->de, newsz, sizeof(*d3s->de));
974 c03c6cb3 2019-02-08 stsp if (d == NULL)
975 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
976 078e971d 2019-02-08 stsp d3s->de = d;
977 078e971d 2019-02-08 stsp memset(d3s->de + d3s->szchanges, 0, incr * sizeof(*d3s->de));
979 078e971d 2019-02-08 stsp s = reallocarray(d3s->overlap, newsz, sizeof(*d3s->overlap));
980 c03c6cb3 2019-02-08 stsp if (s == NULL)
981 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
982 078e971d 2019-02-08 stsp d3s->overlap = s;
983 078e971d 2019-02-08 stsp memset(d3s->overlap + d3s->szchanges, 0, incr * sizeof(*d3s->overlap));
984 078e971d 2019-02-08 stsp d3s->szchanges = newsz;
986 c03c6cb3 2019-02-08 stsp return NULL;