Commit 74b37681cba183e4655a6b08701e64aeac7c9a05

Stefan Sperling 2019-02-07T11:30:27

add unmodified buf.c, buf.h, and diff3.c from OpenRCS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
diff --git a/lib/buf.c b/lib/buf.c
new file mode 100644
index 0000000..1a9450d
--- /dev/null
+++ b/lib/buf.c
@@ -0,0 +1,333 @@
+/*	$OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $	*/
+/*
+ * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/queue.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "buf.h"
+#include "xmalloc.h"
+#include "worklist.h"
+
+#define BUF_INCR	128
+
+struct buf {
+	/* buffer handle, buffer size, and data length */
+	u_char	*cb_buf;
+	size_t	 cb_size;
+	size_t	 cb_len;
+};
+
+#define SIZE_LEFT(b)	(b->cb_size - b->cb_len)
+
+static void	buf_grow(BUF *, size_t);
+
+/*
+ * Create a new buffer structure and return a pointer to it.  This structure
+ * uses dynamically-allocated memory and must be freed with buf_free(), once
+ * the buffer is no longer needed.
+ */
+BUF *
+buf_alloc(size_t len)
+{
+	BUF *b;
+
+	b = xmalloc(sizeof(*b));
+	/* Postpone creation of zero-sized buffers */
+	if (len > 0)
+		b->cb_buf = xcalloc(1, len);
+	else
+		b->cb_buf = NULL;
+
+	b->cb_size = len;
+	b->cb_len = 0;
+
+	return (b);
+}
+
+/*
+ * Open the file specified by <path> and load all of its contents into a
+ * buffer.
+ * Returns the loaded buffer on success or NULL on failure.
+ * Sets errno on error.
+ */
+BUF *
+buf_load(const char *path)
+{
+	int fd;
+	ssize_t ret;
+	size_t len;
+	u_char *bp;
+	struct stat st;
+	BUF *buf;
+
+	buf = NULL;
+
+	if ((fd = open(path, O_RDONLY, 0600)) == -1)
+		goto out;
+
+	if (fstat(fd, &st) == -1)
+		goto out;
+
+	if ((uintmax_t)st.st_size > SIZE_MAX) {
+		errno = EFBIG;
+		goto out;
+	}
+	buf = buf_alloc(st.st_size);
+	for (bp = buf->cb_buf; ; bp += (size_t)ret) {
+		len = SIZE_LEFT(buf);
+		ret = read(fd, bp, len);
+		if (ret == -1) {
+			int saved_errno;
+
+			saved_errno = errno;
+			buf_free(buf);
+			buf = NULL;
+			errno = saved_errno;
+			goto out;
+		} else if (ret == 0)
+			break;
+
+		buf->cb_len += (size_t)ret;
+	}
+
+out:
+	if (fd != -1) {
+		int saved_errno;
+
+		/* We may want to preserve errno here. */
+		saved_errno = errno;
+		(void)close(fd);
+		errno = saved_errno;
+	}
+
+	return (buf);
+}
+
+void
+buf_free(BUF *b)
+{
+	if (b == NULL)
+		return;
+	free(b->cb_buf);
+	free(b);
+}
+
+/*
+ * Free the buffer <b>'s structural information but do not free the contents
+ * of the buffer.  Instead, they are returned and should be freed later using
+ * free().
+ */
+void *
+buf_release(BUF *b)
+{
+	void *tmp;
+
+	tmp = b->cb_buf;
+	free(b);
+	return (tmp);
+}
+
+u_char *
+buf_get(BUF *b)
+{
+	return (b->cb_buf);
+}
+
+/*
+ * Empty the contents of the buffer <b> and reset pointers.
+ */
+void
+buf_empty(BUF *b)
+{
+	memset(b->cb_buf, 0, b->cb_size);
+	b->cb_len = 0;
+}
+
+/*
+ * Append a single character <c> to the end of the buffer <b>.
+ */
+void
+buf_putc(BUF *b, int c)
+{
+	u_char *bp;
+
+	if (SIZE_LEFT(b) == 0)
+		buf_grow(b, BUF_INCR);
+	bp = b->cb_buf + b->cb_len;
+	*bp = (u_char)c;
+	b->cb_len++;
+}
+
+/*
+ * Append a string <s> to the end of buffer <b>.
+ */
+void
+buf_puts(BUF *b, const char *str)
+{
+	buf_append(b, str, strlen(str));
+}
+
+/*
+ * Return u_char at buffer position <pos>.
+ */
+u_char
+buf_getc(BUF *b, size_t pos)
+{
+	return (b->cb_buf[pos]);
+}
+
+/*
+ * Append <len> bytes of data pointed to by <data> to the buffer <b>.  If the
+ * buffer is too small to accept all data, it will get resized to an
+ * appropriate size to accept all data.
+ * Returns the number of bytes successfully appended to the buffer.
+ */
+size_t
+buf_append(BUF *b, const void *data, size_t len)
+{
+	size_t left, rlen;
+	u_char *bp;
+
+	left = SIZE_LEFT(b);
+	rlen = len;
+
+	if (left < len)
+		buf_grow(b, len - left);
+	bp = b->cb_buf + b->cb_len;
+	memcpy(bp, data, rlen);
+	b->cb_len += rlen;
+
+	return (rlen);
+}
+
+/*
+ * Returns the size of the buffer that is being used.
+ */
+size_t
+buf_len(BUF *b)
+{
+	return (b->cb_len);
+}
+
+/*
+ * Write the contents of the buffer <b> to the specified <fd>
+ */
+int
+buf_write_fd(BUF *b, int fd)
+{
+	u_char *bp;
+	size_t len;
+	ssize_t ret;
+
+	len = b->cb_len;
+	bp = b->cb_buf;
+
+	do {
+		ret = write(fd, bp, len);
+		if (ret == -1) {
+			if (errno == EINTR || errno == EAGAIN)
+				continue;
+			return (-1);
+		}
+
+		len -= (size_t)ret;
+		bp += (size_t)ret;
+	} while (len > 0);
+
+	return (0);
+}
+
+/*
+ * Write the contents of the buffer <b> to the file whose path is given in
+ * <path>.  If the file does not exist, it is created with mode <mode>.
+ */
+int
+buf_write(BUF *b, const char *path, mode_t mode)
+{
+	int fd;
+ open:
+	if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
+		if (errno == EACCES && unlink(path) != -1)
+			goto open;
+		else
+			err(1, "%s", path);
+	}
+
+	if (buf_write_fd(b, fd) == -1) {
+		(void)unlink(path);
+		errx(1, "buf_write: buf_write_fd: `%s'", path);
+	}
+
+	if (fchmod(fd, mode) < 0)
+		warn("permissions not set on file %s", path);
+
+	(void)close(fd);
+
+	return (0);
+}
+
+/*
+ * Write the contents of the buffer <b> to a temporary file whose path is
+ * specified using <template> (see mkstemp.3).
+ * NB. This function will modify <template>, as per mkstemp
+ */
+void
+buf_write_stmp(BUF *b, char *template)
+{
+	int fd;
+
+	if ((fd = mkstemp(template)) == -1)
+		err(1, "%s", template);
+
+	worklist_add(template, &temp_files);
+
+	if (buf_write_fd(b, fd) == -1) {
+		(void)unlink(template);
+		errx(1, "buf_write_stmp: buf_write_fd: `%s'", template);
+	}
+
+	(void)close(fd);
+}
+
+/*
+ * Grow the buffer <b> by <len> bytes.  The contents are unchanged by this
+ * operation regardless of the result.
+ */
+static void
+buf_grow(BUF *b, size_t len)
+{
+	b->cb_buf = xreallocarray(b->cb_buf, 1, b->cb_size + len);
+	b->cb_size += len;
+}
diff --git a/lib/buf.h b/lib/buf.h
new file mode 100644
index 0000000..f05e7e8
--- /dev/null
+++ b/lib/buf.h
@@ -0,0 +1,60 @@
+/*	$OpenBSD: buf.h,v 1.13 2011/07/06 15:36:52 nicm Exp $	*/
+/*
+ * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Buffer management
+ * -----------------
+ *
+ * This code provides an API to generic memory buffer management.  All
+ * operations are performed on a buf structure, which is kept opaque to the
+ * API user in order to avoid corruption of the fields and make sure that only
+ * the internals can modify the fields.
+ *
+ * The first step is to allocate a new buffer using the buf_alloc()
+ * function, which returns a pointer to a new buffer.
+ */
+
+#ifndef BUF_H
+#define BUF_H
+
+#include <sys/types.h>
+
+typedef struct buf BUF;
+
+BUF		*buf_alloc(size_t);
+BUF		*buf_load(const char *);
+void		 buf_free(BUF *);
+void		*buf_release(BUF *);
+u_char		 buf_getc(BUF *, size_t);
+void		 buf_empty(BUF *);
+size_t		 buf_append(BUF *, const void *, size_t);
+void		 buf_putc(BUF *, int);
+void		 buf_puts(BUF *b, const char *str);
+size_t		 buf_len(BUF *);
+int		 buf_write_fd(BUF *, int);
+int		 buf_write(BUF *, const char *, mode_t);
+void		 buf_write_stmp(BUF *, char *);
+u_char		*buf_get(BUF *b);
+
+#endif	/* BUF_H */
diff --git a/lib/diff3.c b/lib/diff3.c
new file mode 100644
index 0000000..16cc1b2
--- /dev/null
+++ b/lib/diff3.c
@@ -0,0 +1,930 @@
+/*	$OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $	*/
+
+/*
+ * Copyright (C) Caldera International Inc.  2001-2002.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code and documentation must retain the above
+ *    copyright notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed or owned by Caldera
+ *	International, Inc.
+ * 4. Neither the name of Caldera International, Inc. nor the names of other
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
+ * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
+ * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+/*-
+ * Copyright (c) 1991, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)diff3.c	8.1 (Berkeley) 6/6/93
+ */
+
+#include <ctype.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "diff.h"
+#include "rcsprog.h"
+
+/* diff3 - 3-way differential file comparison */
+
+/* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
+ *
+ * d13 = diff report on f1 vs f3
+ * d23 = diff report on f2 vs f3
+ * f1, f2, f3 the 3 files
+ * if changes in f1 overlap with changes in f3, m1 and m3 are used
+ * to mark the overlaps; otherwise, the file names f1 and f3 are used
+ * (only for options E and X).
+ */
+
+/*
+ * "from" is first in range of changed lines; "to" is last+1
+ * from=to=line after point of insertion for added lines.
+ */
+struct range {
+	int from;
+	int to;
+};
+
+struct diff {
+	struct range old;
+	struct range new;
+};
+
+static size_t szchanges;
+
+static struct diff *d13;
+static struct diff *d23;
+
+/*
+ * "de" is used to gather editing scripts.  These are later spewed out in
+ * reverse order.  Its first element must be all zero, the "new" component
+ * of "de" contains line positions or byte positions depending on when you
+ * look (!?).  Array overlap indicates which sections in "de" correspond to
+ * lines that are different in all three files.
+ */
+static struct diff *de;
+static char *overlap;
+static int overlapcnt = 0;
+static FILE *fp[3];
+static int cline[3];		/* # of the last-read line in each file (0-2) */
+
+/*
+ * the latest known correspondence between line numbers of the 3 files
+ * is stored in last[1-3];
+ */
+static int last[4];
+static int eflag = 3;	/* default -E for compatibility with former RCS */
+static int oflag = 1;	/* default -E for compatibility with former RCS */
+static int debug  = 0;
+static char f1mark[PATH_MAX], f3mark[PATH_MAX];	/* markers for -E and -X */
+
+static int duplicate(struct range *, struct range *);
+static int edit(struct diff *, int, int);
+static char *getchange(FILE *);
+static char *get_line(FILE *, size_t *);
+static int number(char **);
+static ssize_t readin(char *, struct diff **);
+static int skip(int, int, char *);
+static int edscript(int);
+static int merge(size_t, size_t);
+static void change(int, struct range *, int);
+static void keep(int, struct range *);
+static void prange(struct range *);
+static void repos(int);
+static void separate(const char *);
+static void increase(void);
+static int diff3_internal(int, char **, const char *, const char *);
+
+int diff3_conflicts = 0;
+
+/*
+ * For merge(1).
+ */
+BUF *
+merge_diff3(char **av, int flags)
+{
+	int argc;
+	char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
+	BUF *b1, *b2, *b3, *d1, *d2, *diffb;
+	u_char *data, *patch;
+	size_t dlen, plen;
+
+	b1 = b2 = b3 = d1 = d2 = diffb = NULL;
+	dp13 = dp23 = path1 = path2 = path3 = NULL;
+	data = patch = NULL;
+
+	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
+		oflag = 0;
+
+	if ((b1 = buf_load(av[0])) == NULL)
+		goto out;
+	if ((b2 = buf_load(av[1])) == NULL)
+		goto out;
+	if ((b3 = buf_load(av[2])) == NULL)
+		goto out;
+
+	d1 = buf_alloc(128);
+	d2 = buf_alloc(128);
+	diffb = buf_alloc(128);
+
+	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
+	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
+	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
+
+	buf_write_stmp(b1, path1);
+	buf_write_stmp(b2, path2);
+	buf_write_stmp(b3, path3);
+
+	buf_free(b2);
+	b2 = NULL;
+
+	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
+	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
+		buf_free(diffb);
+		diffb = NULL;
+		goto out;
+	}
+
+	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
+	buf_write_stmp(d1, dp13);
+
+	buf_free(d1);
+	d1 = NULL;
+
+	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
+	buf_write_stmp(d2, dp23);
+
+	buf_free(d2);
+	d2 = NULL;
+
+	argc = 0;
+	diffbuf = diffb;
+	argv[argc++] = dp13;
+	argv[argc++] = dp23;
+	argv[argc++] = path1;
+	argv[argc++] = path2;
+	argv[argc++] = path3;
+
+	diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
+	if (diff3_conflicts < 0) {
+		buf_free(diffb);
+		diffb = NULL;
+		goto out;
+	}
+
+	plen = buf_len(diffb);
+	patch = buf_release(diffb);
+	dlen = buf_len(b1);
+	data = buf_release(b1);
+
+	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
+		goto out;
+
+	if (!(flags & QUIET) && diff3_conflicts != 0)
+		warnx("warning: overlaps or other problems during merge");
+
+out:
+	buf_free(b2);
+	buf_free(b3);
+	buf_free(d1);
+	buf_free(d2);
+
+	(void)unlink(path1);
+	(void)unlink(path2);
+	(void)unlink(path3);
+	(void)unlink(dp13);
+	(void)unlink(dp23);
+
+	free(path1);
+	free(path2);
+	free(path3);
+	free(dp13);
+	free(dp23);
+	free(data);
+	free(patch);
+
+	return (diffb);
+}
+
+BUF *
+rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags)
+{
+	int argc;
+	char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
+	char *dp13, *dp23, *path1, *path2, *path3;
+	BUF *b1, *b2, *b3, *d1, *d2, *diffb;
+	size_t dlen, plen;
+	u_char *data, *patch;
+
+	b1 = b2 = b3 = d1 = d2 = diffb = NULL;
+	dp13 = dp23 = path1 = path2 = path3 = NULL;
+	data = patch = NULL;
+
+	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
+		oflag = 0;
+
+	rcsnum_tostr(rev1, r1, sizeof(r1));
+	rcsnum_tostr(rev2, r2, sizeof(r2));
+
+	if ((b1 = buf_load(workfile)) == NULL)
+		goto out;
+
+	if (!(flags & QUIET))
+		(void)fprintf(stderr, "retrieving revision %s\n", r1);
+	if ((b2 = rcs_getrev(rf, rev1)) == NULL)
+		goto out;
+
+	if (!(flags & QUIET))
+		(void)fprintf(stderr, "retrieving revision %s\n", r2);
+	if ((b3 = rcs_getrev(rf, rev2)) == NULL)
+		goto out;
+
+	d1 = buf_alloc(128);
+	d2 = buf_alloc(128);
+	diffb = buf_alloc(128);
+
+	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
+	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
+	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
+
+	buf_write_stmp(b1, path1);
+	buf_write_stmp(b2, path2);
+	buf_write_stmp(b3, path3);
+
+	buf_free(b2);
+	b2 = NULL;
+
+	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
+	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
+		buf_free(diffb);
+		diffb = NULL;
+		goto out;
+	}
+
+	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
+	buf_write_stmp(d1, dp13);
+
+	buf_free(d1);
+	d1 = NULL;
+
+	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
+	buf_write_stmp(d2, dp23);
+
+	buf_free(d2);
+	d2 = NULL;
+
+	argc = 0;
+	diffbuf = diffb;
+	argv[argc++] = dp13;
+	argv[argc++] = dp23;
+	argv[argc++] = path1;
+	argv[argc++] = path2;
+	argv[argc++] = path3;
+
+	diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
+	if (diff3_conflicts < 0) {
+		buf_free(diffb);
+		diffb = NULL;
+		goto out;
+	}
+
+	plen = buf_len(diffb);
+	patch = buf_release(diffb);
+	dlen = buf_len(b1);
+	data = buf_release(b1);
+
+	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
+		goto out;
+
+	if (!(flags & QUIET) && diff3_conflicts != 0)
+		warnx("warning: overlaps or other problems during merge");
+
+out:
+	buf_free(b2);
+	buf_free(b3);
+	buf_free(d1);
+	buf_free(d2);
+
+	(void)unlink(path1);
+	(void)unlink(path2);
+	(void)unlink(path3);
+	(void)unlink(dp13);
+	(void)unlink(dp23);
+
+	free(path1);
+	free(path2);
+	free(path3);
+	free(dp13);
+	free(dp23);
+	free(data);
+	free(patch);
+
+	return (diffb);
+}
+
+static int
+diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
+{
+	ssize_t m, n;
+	int i;
+
+	if (argc < 5)
+		return (-1);
+
+	if (oflag) {
+		i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
+		if (i < 0 || i >= (int)sizeof(f1mark))
+			errx(1, "diff3_internal: string truncated");
+
+		i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
+		if (i < 0 || i >= (int)sizeof(f3mark))
+			errx(1, "diff3_internal: string truncated");
+	}
+
+	increase();
+	if ((m = readin(argv[0], &d13)) < 0) {
+		warn("%s", argv[0]);
+		return (-1);
+	}
+	if ((n = readin(argv[1], &d23)) < 0) {
+		warn("%s", argv[1]);
+		return (-1);
+	}
+
+	for (i = 0; i <= 2; i++)
+		if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
+			warn("%s", argv[i + 2]);
+			return (-1);
+		}
+
+	return (merge(m, n));
+}
+
+int
+ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
+{
+	char op, *ep;
+	struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
+	int start, end, i, lineno;
+	u_char tmp;
+
+	dlp = TAILQ_FIRST(&(dlines->l_lines));
+	lp = TAILQ_FIRST(&(plines->l_lines));
+
+	end = 0;
+	for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
+	    lp = TAILQ_NEXT(lp, l_list)) {
+		/* Skip blank lines */
+		if (lp->l_len < 2)
+			continue;
+
+		/* NUL-terminate line buffer for strtol() safety. */
+		tmp = lp->l_line[lp->l_len - 1];
+		lp->l_line[lp->l_len - 1] = '\0';
+
+		/* len - 1 is NUL terminator so we use len - 2 for 'op' */
+		op = lp->l_line[lp->l_len - 2];
+		start = (int)strtol(lp->l_line, &ep, 10);
+
+		/* Restore the last byte of the buffer */
+		lp->l_line[lp->l_len - 1] = tmp;
+
+		if (op == 'a') {
+			if (start > dlines->l_nblines ||
+			    start < 0 || *ep != 'a')
+				errx(1, "ed_patch_lines");
+		} else if (op == 'c') {
+			if (start > dlines->l_nblines ||
+			    start < 0 || (*ep != ',' && *ep != 'c'))
+				errx(1, "ed_patch_lines");
+
+			if (*ep == ',') {
+				ep++;
+				end = (int)strtol(ep, &ep, 10);
+				if (end < 0 || *ep != 'c')
+					errx(1, "ed_patch_lines");
+			} else {
+				end = start;
+			}
+		}
+
+
+		for (;;) {
+			if (dlp == NULL)
+				break;
+			if (dlp->l_lineno == start)
+				break;
+			if (dlp->l_lineno > start) {
+				dlp = TAILQ_PREV(dlp, tqh, l_list);
+			} else if (dlp->l_lineno < start) {
+				ndlp = TAILQ_NEXT(dlp, l_list);
+				if (ndlp->l_lineno > start)
+					break;
+				dlp = ndlp;
+			}
+		}
+
+		if (dlp == NULL)
+			errx(1, "ed_patch_lines");
+
+
+		if (op == 'c') {
+			insert_after = TAILQ_PREV(dlp, tqh, l_list);
+			for (i = 0; i <= (end - start); i++) {
+				ndlp = TAILQ_NEXT(dlp, l_list);
+				TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
+				dlp = ndlp;
+			}
+			dlp = insert_after;
+		}
+
+		if (op == 'a' || op == 'c') {
+			for (;;) {
+				ndlp = lp;
+				lp = TAILQ_NEXT(lp, l_list);
+				if (lp == NULL)
+					errx(1, "ed_patch_lines");
+
+				if (!memcmp(lp->l_line, ".", 1))
+					break;
+
+				TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
+				TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
+				    lp, l_list);
+				dlp = lp;
+
+				lp->l_lineno = start;
+				lp = ndlp;
+			}
+		}
+
+		/*
+		 * always resort lines as the markers might be put at the
+		 * same line as we first started editing.
+		 */
+		lineno = 0;
+		TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
+			sort->l_lineno = lineno++;
+		dlines->l_nblines = lineno - 1;
+	}
+
+	return (0);
+}
+
+/*
+ * Pick up the line numbers of all changes from one change file.
+ * (This puts the numbers in a vector, which is not strictly necessary,
+ * since the vector is processed in one sequential pass.
+ * The vector could be optimized out of existence)
+ */
+static ssize_t
+readin(char *name, struct diff **dd)
+{
+	int a, b, c, d;
+	char kind, *p;
+	size_t i;
+
+	fp[0] = fopen(name, "r");
+	if (fp[0] == NULL)
+		return (-1);
+	for (i = 0; (p = getchange(fp[0])); i++) {
+		if (i >= szchanges - 1)
+			increase();
+		a = b = number(&p);
+		if (*p == ',') {
+			p++;
+			b = number(&p);
+		}
+		kind = *p++;
+		c = d = number(&p);
+		if (*p==',') {
+			p++;
+			d = number(&p);
+		}
+		if (kind == 'a')
+			a++;
+		if (kind == 'd')
+			c++;
+		b++;
+		d++;
+		(*dd)[i].old.from = a;
+		(*dd)[i].old.to = b;
+		(*dd)[i].new.from = c;
+		(*dd)[i].new.to = d;
+	}
+
+	if (i) {
+		(*dd)[i].old.from = (*dd)[i-1].old.to;
+		(*dd)[i].new.from = (*dd)[i-1].new.to;
+	}
+
+	(void)fclose(fp[0]);
+
+	return (i);
+}
+
+static int
+number(char **lc)
+{
+	int nn;
+
+	nn = 0;
+	while (isdigit((unsigned char)(**lc)))
+		nn = nn*10 + *(*lc)++ - '0';
+
+	return (nn);
+}
+
+static char *
+getchange(FILE *b)
+{
+	char *line;
+
+	while ((line = get_line(b, NULL))) {
+		if (isdigit((unsigned char)line[0]))
+			return (line);
+	}
+
+	return (NULL);
+}
+
+static char *
+get_line(FILE *b, size_t *n)
+{
+	char *cp;
+	size_t len;
+	static char *buf;
+	static size_t bufsize;
+
+	if ((cp = fgetln(b, &len)) == NULL)
+		return (NULL);
+
+	if (cp[len - 1] != '\n')
+		len++;
+	if (len + 1 > bufsize) {
+		do {
+			bufsize += 1024;
+		} while (len + 1 > bufsize);
+		buf = xreallocarray(buf, 1, bufsize);
+	}
+	memcpy(buf, cp, len - 1);
+	buf[len - 1] = '\n';
+	buf[len] = '\0';
+	if (n != NULL)
+		*n = len;
+
+	return (buf);
+}
+
+static int
+merge(size_t m1, size_t m2)
+{
+	struct diff *d1, *d2, *d3;
+	int dpl, j, t1, t2;
+
+	d1 = d13;
+	d2 = d23;
+	j = 0;
+	for (;;) {
+		t1 = (d1 < d13 + m1);
+		t2 = (d2 < d23 + m2);
+		if (!t1 && !t2)
+			break;
+
+		if (debug) {
+			printf("%d,%d=%d,%d %d,%d=%d,%d\n",
+			d1->old.from, d1->old.to,
+			d1->new.from, d1->new.to,
+			d2->old.from, d2->old.to,
+			d2->new.from, d2->new.to);
+		}
+
+		/* first file is different from others */
+		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
+			/* stuff peculiar to 1st file */
+			if (eflag==0) {
+				separate("1");
+				change(1, &d1->old, 0);
+				keep(2, &d1->new);
+				change(3, &d1->new, 0);
+			}
+			d1++;
+			continue;
+		}
+
+		/* second file is different from others */
+		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
+			if (eflag==0) {
+				separate("2");
+				keep(1, &d2->new);
+				change(2, &d2->old, 0);
+				change(3, &d2->new, 0);
+			}
+			d2++;
+			continue;
+		}
+
+		/*
+		 * Merge overlapping changes in first file
+		 * this happens after extension (see below).
+		 */
+		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
+			d1[1].old.from = d1->old.from;
+			d1[1].new.from = d1->new.from;
+			d1++;
+			continue;
+		}
+
+		/* merge overlapping changes in second */
+		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
+			d2[1].old.from = d2->old.from;
+			d2[1].new.from = d2->new.from;
+			d2++;
+			continue;
+		}
+		/* stuff peculiar to third file or different in all */
+		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
+			dpl = duplicate(&d1->old,&d2->old);
+			if (dpl == -1)
+				return (-1);
+
+			/*
+			 * dpl = 0 means all files differ
+			 * dpl = 1 means files 1 and 2 identical
+			 */
+			if (eflag==0) {
+				separate(dpl ? "3" : "");
+				change(1, &d1->old, dpl);
+				change(2, &d2->old, 0);
+				d3 = d1->old.to > d1->old.from ? d1 : d2;
+				change(3, &d3->new, 0);
+			} else
+				j = edit(d1, dpl, j);
+			d1++;
+			d2++;
+			continue;
+		}
+
+		/*
+		 * Overlapping changes from file 1 and 2; extend changes
+		 * appropriately to make them coincide.
+		 */
+		if (d1->new.from < d2->new.from) {
+			d2->old.from -= d2->new.from-d1->new.from;
+			d2->new.from = d1->new.from;
+		} else if (d2->new.from < d1->new.from) {
+			d1->old.from -= d1->new.from-d2->new.from;
+			d1->new.from = d2->new.from;
+		}
+		if (d1->new.to > d2->new.to) {
+			d2->old.to += d1->new.to - d2->new.to;
+			d2->new.to = d1->new.to;
+		} else if (d2->new.to > d1->new.to) {
+			d1->old.to += d2->new.to - d1->new.to;
+			d1->new.to = d2->new.to;
+		}
+	}
+
+	return (edscript(j));
+}
+
+static void
+separate(const char *s)
+{
+	diff_output("====%s\n", s);
+}
+
+/*
+ * The range of lines rold.from thru rold.to in file i is to be changed.
+ * It is to be printed only if it does not duplicate something to be
+ * printed later.
+ */
+static void
+change(int i, struct range *rold, int fdup)
+{
+	diff_output("%d:", i);
+	last[i] = rold->to;
+	prange(rold);
+	if (fdup || debug)
+		return;
+	i--;
+	(void)skip(i, rold->from, NULL);
+	(void)skip(i, rold->to, "  ");
+}
+
+/*
+ * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
+ */
+static void
+prange(struct range *rold)
+{
+	if (rold->to <= rold->from)
+		diff_output("%da\n", rold->from - 1);
+	else {
+		diff_output("%d", rold->from);
+		if (rold->to > rold->from+1)
+			diff_output(",%d", rold->to - 1);
+		diff_output("c\n");
+	}
+}
+
+/*
+ * No difference was reported by diff between file 1 (or 2) and file 3,
+ * and an artificial dummy difference (trange) must be ginned up to
+ * correspond to the change reported in the other file.
+ */
+static void
+keep(int i, struct range *rnew)
+{
+	int delta;
+	struct range trange;
+
+	delta = last[3] - last[i];
+	trange.from = rnew->from - delta;
+	trange.to = rnew->to - delta;
+	change(i, &trange, 1);
+}
+
+/*
+ * skip to just before line number from in file "i".  If "pr" is non-NULL,
+ * print all skipped stuff with string pr as a prefix.
+ */
+static int
+skip(int i, int from, char *pr)
+{
+	size_t j, n;
+	char *line;
+
+	for (n = 0; cline[i] < from - 1; n += j) {
+		if ((line = get_line(fp[i], &j)) == NULL)
+			return (-1);
+		if (pr != NULL)
+			diff_output("%s%s", pr, line);
+		cline[i]++;
+	}
+	return ((int) n);
+}
+
+/*
+ * Return 1 or 0 according as the old range (in file 1) contains exactly
+ * the same data as the new range (in file 2).
+ */
+static int
+duplicate(struct range *r1, struct range *r2)
+{
+	int c,d;
+	int nchar;
+	int nline;
+
+	if (r1->to-r1->from != r2->to-r2->from)
+		return (0);
+	(void)skip(0, r1->from, NULL);
+	(void)skip(1, r2->from, NULL);
+	nchar = 0;
+	for (nline=0; nline < r1->to - r1->from; nline++) {
+		do {
+			c = getc(fp[0]);
+			d = getc(fp[1]);
+			if (c == -1 || d== -1)
+				return (-1);
+			nchar++;
+			if (c != d) {
+				repos(nchar);
+				return (0);
+			}
+		} while (c != '\n');
+	}
+	repos(nchar);
+	return (1);
+}
+
+static void
+repos(int nchar)
+{
+	int i;
+
+	for (i = 0; i < 2; i++)
+		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
+}
+
+/*
+ * collect an editing script for later regurgitation
+ */
+static int
+edit(struct diff *diff, int fdup, int j)
+{
+	if (((fdup + 1) & eflag) == 0)
+		return (j);
+	j++;
+	overlap[j] = !fdup;
+	if (!fdup)
+		overlapcnt++;
+	de[j].old.from = diff->old.from;
+	de[j].old.to = diff->old.to;
+	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
+	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
+	return (j);
+}
+
+/* regurgitate */
+static int
+edscript(int n)
+{
+	int j, k;
+	char block[BUFSIZ+1];
+
+	for (; n > 0; n--) {
+		if (!oflag || !overlap[n])
+			prange(&de[n].old);
+		else
+			diff_output("%da\n=======\n", de[n].old.to -1);
+		(void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
+		for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
+			j = k > BUFSIZ ? BUFSIZ : k;
+			if (fread(block, 1, j, fp[2]) != (size_t)j)
+				return (-1);
+			block[j] = '\0';
+			diff_output("%s", block);
+		}
+
+		if (!oflag || !overlap[n])
+			diff_output(".\n");
+		else {
+			diff_output("%s\n.\n", f3mark);
+			diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
+		}
+	}
+
+	return (overlapcnt);
+}
+
+static void
+increase(void)
+{
+	size_t newsz, incr;
+
+	/* are the memset(3) calls needed? */
+	newsz = szchanges == 0 ? 64 : 2 * szchanges;
+	incr = newsz - szchanges;
+
+	d13 = xreallocarray(d13, newsz, sizeof(*d13));
+	memset(d13 + szchanges, 0, incr * sizeof(*d13));
+	d23 = xreallocarray(d23, newsz, sizeof(*d23));
+	memset(d23 + szchanges, 0, incr * sizeof(*d23));
+	de = xreallocarray(de, newsz, sizeof(*de));
+	memset(de + szchanges, 0, incr * sizeof(*de));
+	overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
+	memset(overlap + szchanges, 0, incr * sizeof(*overlap));
+	szchanges = newsz;
+}