xref: /illumos-gate/usr/src/cmd/bdiff/bdiff.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <fatal.h>
32*7c478bd9Sstevel@tonic-gate #include <signal.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <wait.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	ONSIG	16
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  *	This program segments two files into pieces of <= seglim lines
45*7c478bd9Sstevel@tonic-gate  *	(which is passed as a third argument or defaulted to some number)
46*7c478bd9Sstevel@tonic-gate  *	and then executes diff upon the pieces. The output of
47*7c478bd9Sstevel@tonic-gate  *	'diff' is then processed to make it look as if 'diff' had
48*7c478bd9Sstevel@tonic-gate  *	processed the files whole. The reason for all this is that seglim
49*7c478bd9Sstevel@tonic-gate  *	is a reasonable upper limit on the size of files that diff can
50*7c478bd9Sstevel@tonic-gate  *	process.
51*7c478bd9Sstevel@tonic-gate  *	NOTE -- by segmenting the files in this manner, it cannot be
52*7c478bd9Sstevel@tonic-gate  *	guaranteed that the 'diffing' of the segments will generate
53*7c478bd9Sstevel@tonic-gate  *	a minimal set of differences.
54*7c478bd9Sstevel@tonic-gate  *	This process is most definitely not equivalent to 'diffing'
55*7c478bd9Sstevel@tonic-gate  *	the files whole, assuming 'diff' could handle such large files.
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  *	'diff' is executed by a child process, generated by forking,
58*7c478bd9Sstevel@tonic-gate  *	and communicates with this program through pipes.
59*7c478bd9Sstevel@tonic-gate  */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static char Error[128];
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static int seglim;	/* limit of size of file segment to be generated */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static char diff[]  =  "/usr/bin/diff";
66*7c478bd9Sstevel@tonic-gate static char tempskel[] = "/tmp/bdXXXXXX"; /* used to generate temp file names */
67*7c478bd9Sstevel@tonic-gate static char tempfile[32];
68*7c478bd9Sstevel@tonic-gate static char otmp[32], ntmp[32];
69*7c478bd9Sstevel@tonic-gate static int	fflags;
70*7c478bd9Sstevel@tonic-gate static int	fatal_num = 1;		/* exit number for fatal exit */
71*7c478bd9Sstevel@tonic-gate static offset_t	linenum;
72*7c478bd9Sstevel@tonic-gate static size_t obufsiz, nbufsiz, dbufsiz;
73*7c478bd9Sstevel@tonic-gate static char *readline(char **, size_t *, FILE *);
74*7c478bd9Sstevel@tonic-gate static void addgen(char **, size_t *, FILE *);
75*7c478bd9Sstevel@tonic-gate static void delgen(char **, size_t *, FILE *);
76*7c478bd9Sstevel@tonic-gate static void fixnum(char *);
77*7c478bd9Sstevel@tonic-gate static void fatal(char *);
78*7c478bd9Sstevel@tonic-gate static void setsig(void);
79*7c478bd9Sstevel@tonic-gate static void setsig1(int);
80*7c478bd9Sstevel@tonic-gate static char *satoi(char *, offset_t *);
81*7c478bd9Sstevel@tonic-gate static FILE *maket(char *);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static char *prognam;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])86*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	FILE *poldfile, *pnewfile;
89*7c478bd9Sstevel@tonic-gate 	char *oline, *nline, *diffline;
90*7c478bd9Sstevel@tonic-gate 	char *olp, *nlp, *dp;
91*7c478bd9Sstevel@tonic-gate 	int otcnt, ntcnt;
92*7c478bd9Sstevel@tonic-gate 	pid_t i;
93*7c478bd9Sstevel@tonic-gate 	int pfd[2];
94*7c478bd9Sstevel@tonic-gate 	FILE *poldtemp, *pnewtemp, *pipeinp;
95*7c478bd9Sstevel@tonic-gate 	int status;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	prognam = argv[0];
98*7c478bd9Sstevel@tonic-gate 	/*
99*7c478bd9Sstevel@tonic-gate 	 * Set flags for 'fatal' so that it will clean up,
100*7c478bd9Sstevel@tonic-gate 	 * produce a message, and terminate.
101*7c478bd9Sstevel@tonic-gate 	 */
102*7c478bd9Sstevel@tonic-gate 	fflags = FTLMSG | FTLCLN | FTLEXIT;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	setsig();
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if (argc < 3 || argc > 5)
107*7c478bd9Sstevel@tonic-gate 		fatal("arg count");
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "-") == 0 && strcmp(argv[2], "-") == 0)
110*7c478bd9Sstevel@tonic-gate 		fatal("both files standard input");
111*7c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], "-") == 0)
112*7c478bd9Sstevel@tonic-gate 		poldfile = stdin;
113*7c478bd9Sstevel@tonic-gate 	else
114*7c478bd9Sstevel@tonic-gate 		if ((poldfile = fopen(argv[1], "r")) == NULL) {
115*7c478bd9Sstevel@tonic-gate 			(void) snprintf(Error, sizeof (Error),
116*7c478bd9Sstevel@tonic-gate 				"Can not open '%s'", argv[1]);
117*7c478bd9Sstevel@tonic-gate 			fatal(Error);
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 	if (strcmp(argv[2], "-") == 0)
120*7c478bd9Sstevel@tonic-gate 		pnewfile = stdin;
121*7c478bd9Sstevel@tonic-gate 	else
122*7c478bd9Sstevel@tonic-gate 		if ((pnewfile = fopen(argv[2], "r")) == NULL) {
123*7c478bd9Sstevel@tonic-gate 			(void) snprintf(Error, sizeof (Error),
124*7c478bd9Sstevel@tonic-gate 				"Can not open '%s'", argv[2]);
125*7c478bd9Sstevel@tonic-gate 			fatal(Error);
126*7c478bd9Sstevel@tonic-gate 		}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	seglim = 3500;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (argc > 3) {
131*7c478bd9Sstevel@tonic-gate 		if (argv[3][0] == '-' && argv[3][1] == 's')
132*7c478bd9Sstevel@tonic-gate 			fflags &= ~FTLMSG;
133*7c478bd9Sstevel@tonic-gate 		else {
134*7c478bd9Sstevel@tonic-gate 			if ((seglim = atoi(argv[3])) == 0)
135*7c478bd9Sstevel@tonic-gate 				fatal("non-numeric limit");
136*7c478bd9Sstevel@tonic-gate 			if (argc == 5 && argv[4][0] == '-' &&
137*7c478bd9Sstevel@tonic-gate 					argv[4][1] == 's')
138*7c478bd9Sstevel@tonic-gate 				fflags &= ~FTLMSG;
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	linenum = 0;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/* Allocate the buffers and initialize their lengths */
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	obufsiz = BUFSIZ;
147*7c478bd9Sstevel@tonic-gate 	nbufsiz = BUFSIZ;
148*7c478bd9Sstevel@tonic-gate 	dbufsiz = BUFSIZ;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if ((oline = (char *)malloc(obufsiz)) == NULL ||
151*7c478bd9Sstevel@tonic-gate 	    (nline = (char *)malloc(nbufsiz)) == NULL ||
152*7c478bd9Sstevel@tonic-gate 	    (diffline = (char *)malloc(dbufsiz)) == NULL)
153*7c478bd9Sstevel@tonic-gate 		fatal("Out of memory");
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	/*
156*7c478bd9Sstevel@tonic-gate 	 * The following while-loop will prevent any lines
157*7c478bd9Sstevel@tonic-gate 	 * common to the beginning of both files from being
158*7c478bd9Sstevel@tonic-gate 	 * sent to 'diff'. Since the running time of 'diff' is
159*7c478bd9Sstevel@tonic-gate 	 * non-linear, this will help improve performance.
160*7c478bd9Sstevel@tonic-gate 	 * If, during this process, both files reach EOF, then
161*7c478bd9Sstevel@tonic-gate 	 * the files are equal and the program will terminate.
162*7c478bd9Sstevel@tonic-gate 	 * If either file reaches EOF before the other, the
163*7c478bd9Sstevel@tonic-gate 	 * program will generate the appropriate 'diff' output
164*7c478bd9Sstevel@tonic-gate 	 * itself, since this can be easily determined and will
165*7c478bd9Sstevel@tonic-gate 	 * avoid executing 'diff' completely.
166*7c478bd9Sstevel@tonic-gate 	 */
167*7c478bd9Sstevel@tonic-gate 	for (;;) {
168*7c478bd9Sstevel@tonic-gate 		olp = readline(&oline, &obufsiz, poldfile);
169*7c478bd9Sstevel@tonic-gate 		nlp = readline(&nline, &nbufsiz, pnewfile);
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		if (!olp && !nlp)	/* EOF found on both:  files equal */
172*7c478bd9Sstevel@tonic-gate 			return (0);
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		if (!olp) {
175*7c478bd9Sstevel@tonic-gate 			/*
176*7c478bd9Sstevel@tonic-gate 			 * The entire old file is a prefix of the
177*7c478bd9Sstevel@tonic-gate 			 * new file. Generate the appropriate "append"
178*7c478bd9Sstevel@tonic-gate 			 * 'diff'-like output, which is of the form:
179*7c478bd9Sstevel@tonic-gate 			 * 		nan, n
180*7c478bd9Sstevel@tonic-gate 			 * where 'n' represents a line-number.
181*7c478bd9Sstevel@tonic-gate 			 */
182*7c478bd9Sstevel@tonic-gate 			addgen(&nline, &nbufsiz, pnewfile);
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		if (!nlp) {
186*7c478bd9Sstevel@tonic-gate 			/*
187*7c478bd9Sstevel@tonic-gate 			 * The entire new file is a prefix of the
188*7c478bd9Sstevel@tonic-gate 			 * old file. Generate the appropriate "delete"
189*7c478bd9Sstevel@tonic-gate 			 * 'diff'-like output, which is of the form:
190*7c478bd9Sstevel@tonic-gate 			 * 		n, ndn
191*7c478bd9Sstevel@tonic-gate 			 * where 'n' represents a line-number.
192*7c478bd9Sstevel@tonic-gate 			 */
193*7c478bd9Sstevel@tonic-gate 			delgen(&oline, &obufsiz, poldfile);
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 		if (strcmp(olp, nlp) == 0)
197*7c478bd9Sstevel@tonic-gate 			linenum++;
198*7c478bd9Sstevel@tonic-gate 		else
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	/*
203*7c478bd9Sstevel@tonic-gate 	 * Here, first 'linenum' lines are equal.
204*7c478bd9Sstevel@tonic-gate 	 * The following while-loop segments both files into
205*7c478bd9Sstevel@tonic-gate 	 * seglim segments, forks and executes 'diff' on the
206*7c478bd9Sstevel@tonic-gate 	 * segments, and processes the resulting output of
207*7c478bd9Sstevel@tonic-gate 	 * 'diff', which is read from a pipe.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	for (;;) {
210*7c478bd9Sstevel@tonic-gate 		/* If both files are at EOF, everything is done. */
211*7c478bd9Sstevel@tonic-gate 		if (!olp && !nlp)	/* finished */
212*7c478bd9Sstevel@tonic-gate 			return (0);
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 		if (!olp) {
215*7c478bd9Sstevel@tonic-gate 			/*
216*7c478bd9Sstevel@tonic-gate 			 * Generate appropriate "append"
217*7c478bd9Sstevel@tonic-gate 			 * output without executing 'diff'.
218*7c478bd9Sstevel@tonic-gate 			 */
219*7c478bd9Sstevel@tonic-gate 			addgen(&nline, &nbufsiz, pnewfile);
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		if (!nlp) {
223*7c478bd9Sstevel@tonic-gate 			/*
224*7c478bd9Sstevel@tonic-gate 			 * Generate appropriate "delete"
225*7c478bd9Sstevel@tonic-gate 			 * output without executing 'diff'.
226*7c478bd9Sstevel@tonic-gate 			 */
227*7c478bd9Sstevel@tonic-gate 			delgen(&oline, &obufsiz, poldfile);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		/*
231*7c478bd9Sstevel@tonic-gate 		 * Create a temporary file to hold a segment
232*7c478bd9Sstevel@tonic-gate 		 * from the old file, and write it.
233*7c478bd9Sstevel@tonic-gate 		 */
234*7c478bd9Sstevel@tonic-gate 		poldtemp = maket(otmp);
235*7c478bd9Sstevel@tonic-gate 		otcnt = 0;
236*7c478bd9Sstevel@tonic-gate 		while (olp && otcnt < seglim) {
237*7c478bd9Sstevel@tonic-gate 			(void) fputs(oline, poldtemp);
238*7c478bd9Sstevel@tonic-gate 			if (ferror(poldtemp) != 0) {
239*7c478bd9Sstevel@tonic-gate 				fflags |= FTLMSG;
240*7c478bd9Sstevel@tonic-gate 				fatal("Can not write to temporary file");
241*7c478bd9Sstevel@tonic-gate 			}
242*7c478bd9Sstevel@tonic-gate 			olp = readline(&oline, &obufsiz, poldfile);
243*7c478bd9Sstevel@tonic-gate 			otcnt++;
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 		(void) fclose(poldtemp);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 		/*
248*7c478bd9Sstevel@tonic-gate 		 * Create a temporary file to hold a segment
249*7c478bd9Sstevel@tonic-gate 		 * from the new file, and write it.
250*7c478bd9Sstevel@tonic-gate 		 */
251*7c478bd9Sstevel@tonic-gate 		pnewtemp = maket(ntmp);
252*7c478bd9Sstevel@tonic-gate 		ntcnt = 0;
253*7c478bd9Sstevel@tonic-gate 		while (nlp && ntcnt < seglim) {
254*7c478bd9Sstevel@tonic-gate 			(void) fputs(nline, pnewtemp);
255*7c478bd9Sstevel@tonic-gate 			if (ferror(pnewtemp) != 0) {
256*7c478bd9Sstevel@tonic-gate 				fflags |= FTLMSG;
257*7c478bd9Sstevel@tonic-gate 				fatal("Can not write to temporary file");
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 			nlp = readline(&nline, &nbufsiz, pnewfile);
260*7c478bd9Sstevel@tonic-gate 			ntcnt++;
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 		(void) fclose(pnewtemp);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 		/* Create pipes and fork.  */
265*7c478bd9Sstevel@tonic-gate 		if ((pipe(pfd)) == -1)
266*7c478bd9Sstevel@tonic-gate 			fatal("Can not create pipe");
267*7c478bd9Sstevel@tonic-gate 		if ((i = fork()) < (pid_t)0) {
268*7c478bd9Sstevel@tonic-gate 			(void) close(pfd[0]);
269*7c478bd9Sstevel@tonic-gate 			(void) close(pfd[1]);
270*7c478bd9Sstevel@tonic-gate 			fatal("Can not fork, try again");
271*7c478bd9Sstevel@tonic-gate 		} else if (i == (pid_t)0) {	/* child process */
272*7c478bd9Sstevel@tonic-gate 			(void) close(pfd[0]);
273*7c478bd9Sstevel@tonic-gate 			(void) close(1);
274*7c478bd9Sstevel@tonic-gate 			(void) dup(pfd[1]);
275*7c478bd9Sstevel@tonic-gate 			(void) close(pfd[1]);
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 			/* Execute 'diff' on the segment files. */
278*7c478bd9Sstevel@tonic-gate 			(void) execlp(diff, diff, otmp, ntmp, 0);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 			/*
281*7c478bd9Sstevel@tonic-gate 			 * Exit code here must be > 1.
282*7c478bd9Sstevel@tonic-gate 			 * Parent process treats exit code of 1 from the child
283*7c478bd9Sstevel@tonic-gate 			 * as non-error because the child process "diff" exits
284*7c478bd9Sstevel@tonic-gate 			 * with a status of 1 when a difference is encountered.
285*7c478bd9Sstevel@tonic-gate 			 * The error here is a true error--the parent process
286*7c478bd9Sstevel@tonic-gate 			 * needs to detect it and exit with a non-zero status.
287*7c478bd9Sstevel@tonic-gate 			 */
288*7c478bd9Sstevel@tonic-gate 			(void) close(1);
289*7c478bd9Sstevel@tonic-gate 			(void) snprintf(Error, sizeof (Error),
290*7c478bd9Sstevel@tonic-gate 			    "Can not execute '%s'", diff);
291*7c478bd9Sstevel@tonic-gate 			fatal_num = 2;
292*7c478bd9Sstevel@tonic-gate 			fatal(Error);
293*7c478bd9Sstevel@tonic-gate 		} else {			/* parent process */
294*7c478bd9Sstevel@tonic-gate 			(void) close(pfd[1]);
295*7c478bd9Sstevel@tonic-gate 			pipeinp = fdopen(pfd[0], "r");
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 			/* Process 'diff' output. */
298*7c478bd9Sstevel@tonic-gate 			while ((dp = readline(&diffline, &dbufsiz, pipeinp))) {
299*7c478bd9Sstevel@tonic-gate 				if (isdigit(*dp))
300*7c478bd9Sstevel@tonic-gate 					fixnum(diffline);
301*7c478bd9Sstevel@tonic-gate 				else
302*7c478bd9Sstevel@tonic-gate 					(void) printf("%s", diffline);
303*7c478bd9Sstevel@tonic-gate 			}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 			(void) fclose(pipeinp);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 			/* EOF on pipe. */
308*7c478bd9Sstevel@tonic-gate 			(void) wait(&status);
309*7c478bd9Sstevel@tonic-gate 			if (status&~0x100) {
310*7c478bd9Sstevel@tonic-gate 				(void) snprintf(Error, sizeof (Error),
311*7c478bd9Sstevel@tonic-gate 				    "'%s' failed", diff);
312*7c478bd9Sstevel@tonic-gate 				fatal(Error);
313*7c478bd9Sstevel@tonic-gate 			}
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 		linenum += seglim;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 		/* Remove temporary files. */
318*7c478bd9Sstevel@tonic-gate 		(void) unlink(otmp);
319*7c478bd9Sstevel@tonic-gate 		(void) unlink(ntmp);
320*7c478bd9Sstevel@tonic-gate 	}
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate /* Routine to save remainder of a file. */
324*7c478bd9Sstevel@tonic-gate static void
saverest(char ** linep,size_t * bufsizp,FILE * iptr)325*7c478bd9Sstevel@tonic-gate saverest(char **linep, size_t *bufsizp, FILE *iptr)
326*7c478bd9Sstevel@tonic-gate {
327*7c478bd9Sstevel@tonic-gate 	char *lp;
328*7c478bd9Sstevel@tonic-gate 	FILE *temptr;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	temptr = maket(tempfile);
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	lp = *linep;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	while (lp) {
335*7c478bd9Sstevel@tonic-gate 		(void) fputs(*linep, temptr);
336*7c478bd9Sstevel@tonic-gate 		linenum++;
337*7c478bd9Sstevel@tonic-gate 		lp = readline(linep, bufsizp, iptr);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 	(void) fclose(temptr);
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /* Routine to write out data saved by 'saverest' and to remove the file. */
343*7c478bd9Sstevel@tonic-gate static void
putsave(char ** linep,size_t * bufsizp,char type)344*7c478bd9Sstevel@tonic-gate putsave(char **linep, size_t *bufsizp, char type)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	FILE *temptr;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	if ((temptr = fopen(tempfile, "r")) == NULL) {
349*7c478bd9Sstevel@tonic-gate 		(void) snprintf(Error, sizeof (Error),
350*7c478bd9Sstevel@tonic-gate 		    "Can not open tempfile ('%s')", tempfile); fatal(Error);
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	while (readline(linep, bufsizp, temptr))
354*7c478bd9Sstevel@tonic-gate 		(void) printf("%c %s", type, *linep);
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	(void) fclose(temptr);
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	(void) unlink(tempfile);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate static void
fixnum(char * lp)362*7c478bd9Sstevel@tonic-gate fixnum(char *lp)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	offset_t num;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	while (*lp) {
367*7c478bd9Sstevel@tonic-gate 		switch (*lp) {
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 		case 'a':
370*7c478bd9Sstevel@tonic-gate 		case 'c':
371*7c478bd9Sstevel@tonic-gate 		case 'd':
372*7c478bd9Sstevel@tonic-gate 		case ',':
373*7c478bd9Sstevel@tonic-gate 		case '\n':
374*7c478bd9Sstevel@tonic-gate 			(void) printf("%c", *lp);
375*7c478bd9Sstevel@tonic-gate 			lp++;
376*7c478bd9Sstevel@tonic-gate 			break;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 		default:
379*7c478bd9Sstevel@tonic-gate 			lp = satoi(lp, &num);
380*7c478bd9Sstevel@tonic-gate 			num += linenum;
381*7c478bd9Sstevel@tonic-gate 			(void) printf("%lld", num);
382*7c478bd9Sstevel@tonic-gate 		}
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate static void
addgen(char ** lpp,size_t * bufsizp,FILE * fp)387*7c478bd9Sstevel@tonic-gate addgen(char **lpp, size_t *bufsizp, FILE *fp)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	offset_t oldline;
390*7c478bd9Sstevel@tonic-gate 	(void) printf("%llda%lld", linenum, linenum+1);
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/* Save lines of new file. */
393*7c478bd9Sstevel@tonic-gate 	oldline = linenum + 1;
394*7c478bd9Sstevel@tonic-gate 	saverest(lpp, bufsizp, fp);
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	if (oldline < linenum)
397*7c478bd9Sstevel@tonic-gate 		(void) printf(",%lld\n", linenum);
398*7c478bd9Sstevel@tonic-gate 	else
399*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	/* Output saved lines, as 'diff' would. */
402*7c478bd9Sstevel@tonic-gate 	putsave(lpp, bufsizp, '>');
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	exit(0);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate static void
delgen(char ** lpp,size_t * bufsizp,FILE * fp)408*7c478bd9Sstevel@tonic-gate delgen(char **lpp, size_t *bufsizp, FILE *fp)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	offset_t savenum;
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	(void) printf("%lld", linenum+1);
413*7c478bd9Sstevel@tonic-gate 	savenum = linenum;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/* Save lines of old file. */
416*7c478bd9Sstevel@tonic-gate 	saverest(lpp, bufsizp, fp);
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	if (savenum +1 != linenum)
419*7c478bd9Sstevel@tonic-gate 		(void) printf(",%lldd%lld\n", linenum, savenum);
420*7c478bd9Sstevel@tonic-gate 	else
421*7c478bd9Sstevel@tonic-gate 		(void) printf("d%lld\n", savenum);
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	/* Output saved lines, as 'diff' would.  */
424*7c478bd9Sstevel@tonic-gate 	putsave(lpp, bufsizp, '<');
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	exit(0);
427*7c478bd9Sstevel@tonic-gate }
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate static void
clean_up()430*7c478bd9Sstevel@tonic-gate clean_up()
431*7c478bd9Sstevel@tonic-gate {
432*7c478bd9Sstevel@tonic-gate 	(void) unlink(tempfile);
433*7c478bd9Sstevel@tonic-gate 	(void) unlink(otmp);
434*7c478bd9Sstevel@tonic-gate 	(void) unlink(ntmp);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate static FILE *
maket(char * file)438*7c478bd9Sstevel@tonic-gate maket(char *file)
439*7c478bd9Sstevel@tonic-gate {
440*7c478bd9Sstevel@tonic-gate 	FILE *iop;
441*7c478bd9Sstevel@tonic-gate 	int fd;
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 	(void) strcpy(file, tempskel);
444*7c478bd9Sstevel@tonic-gate 	if ((fd = mkstemp(file)) == -1 ||
445*7c478bd9Sstevel@tonic-gate 		(iop = fdopen(fd, "w+")) == NULL) {
446*7c478bd9Sstevel@tonic-gate 		(void) snprintf(Error, sizeof (Error),
447*7c478bd9Sstevel@tonic-gate 		    "Can not open/create temp file ('%s')", file);
448*7c478bd9Sstevel@tonic-gate 		fatal(Error);
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate 	return (iop);
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate static void
fatal(char * msg)454*7c478bd9Sstevel@tonic-gate fatal(char *msg)
455*7c478bd9Sstevel@tonic-gate /*
456*7c478bd9Sstevel@tonic-gate  *	General purpose error handler.
457*7c478bd9Sstevel@tonic-gate  *
458*7c478bd9Sstevel@tonic-gate  *	The argument to fatal is a pointer to an error message string.
459*7c478bd9Sstevel@tonic-gate  *	The action of this routine is driven completely from
460*7c478bd9Sstevel@tonic-gate  *	the "fflags" global word (see <fatal.h>).
461*7c478bd9Sstevel@tonic-gate  *
462*7c478bd9Sstevel@tonic-gate  *	The FTLMSG bit controls the writing of the error
463*7c478bd9Sstevel@tonic-gate  *	message on file descriptor 2.  A newline is written
464*7c478bd9Sstevel@tonic-gate  *	after the user supplied message.
465*7c478bd9Sstevel@tonic-gate  *
466*7c478bd9Sstevel@tonic-gate  *	If the FTLCLN bit is on, clean_up is called.
467*7c478bd9Sstevel@tonic-gate  */
468*7c478bd9Sstevel@tonic-gate {
469*7c478bd9Sstevel@tonic-gate 	if (fflags & FTLMSG)
470*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", prognam, msg);
471*7c478bd9Sstevel@tonic-gate 	if (fflags & FTLCLN)
472*7c478bd9Sstevel@tonic-gate 		clean_up();
473*7c478bd9Sstevel@tonic-gate 	if (fflags & FTLEXIT)
474*7c478bd9Sstevel@tonic-gate 		exit(fatal_num);
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate static void
setsig()478*7c478bd9Sstevel@tonic-gate setsig()
479*7c478bd9Sstevel@tonic-gate /*
480*7c478bd9Sstevel@tonic-gate  *	General-purpose signal setting routine.
481*7c478bd9Sstevel@tonic-gate  *	All non-ignored, non-caught signals are caught.
482*7c478bd9Sstevel@tonic-gate  *	If a signal other than hangup, interrupt, or quit is caught,
483*7c478bd9Sstevel@tonic-gate  *	a "user-oriented" message is printed on file descriptor 2.
484*7c478bd9Sstevel@tonic-gate  *	If hangup, interrupt or quit is caught, that signal
485*7c478bd9Sstevel@tonic-gate  *	is set to ignore.
486*7c478bd9Sstevel@tonic-gate  *	Termination is like that of "fatal",
487*7c478bd9Sstevel@tonic-gate  *	via "clean_up()"
488*7c478bd9Sstevel@tonic-gate  */
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate 	void (*act)(int);
491*7c478bd9Sstevel@tonic-gate 	int j;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	for (j = 1; j < ONSIG; j++) {
494*7c478bd9Sstevel@tonic-gate 		act = signal(j, setsig1);
495*7c478bd9Sstevel@tonic-gate 		if (act == SIG_ERR)
496*7c478bd9Sstevel@tonic-gate 			continue;
497*7c478bd9Sstevel@tonic-gate 		if (act == SIG_DFL)
498*7c478bd9Sstevel@tonic-gate 			continue;
499*7c478bd9Sstevel@tonic-gate 		(void) signal(j, act);
500*7c478bd9Sstevel@tonic-gate 	}
501*7c478bd9Sstevel@tonic-gate }
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate static void
setsig1(int sig)504*7c478bd9Sstevel@tonic-gate setsig1(int sig)
505*7c478bd9Sstevel@tonic-gate {
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	(void) signal(sig, SIG_IGN);
508*7c478bd9Sstevel@tonic-gate 	clean_up();
509*7c478bd9Sstevel@tonic-gate 	exit(1);
510*7c478bd9Sstevel@tonic-gate }
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate static char *
satoi(char * p,offset_t * ip)513*7c478bd9Sstevel@tonic-gate satoi(char *p, offset_t *ip)
514*7c478bd9Sstevel@tonic-gate {
515*7c478bd9Sstevel@tonic-gate 	offset_t sum;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	sum = 0;
518*7c478bd9Sstevel@tonic-gate 	while (isdigit(*p))
519*7c478bd9Sstevel@tonic-gate 		sum = sum * 10 + (*p++ - '0');
520*7c478bd9Sstevel@tonic-gate 	*ip = sum;
521*7c478bd9Sstevel@tonic-gate 	return (p);
522*7c478bd9Sstevel@tonic-gate }
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate /*
525*7c478bd9Sstevel@tonic-gate  * Read a line of data from a file.  If the current buffer is not large enough
526*7c478bd9Sstevel@tonic-gate  * to contain the line, double the size of the buffer and continue reading.
527*7c478bd9Sstevel@tonic-gate  * Loop until either the entire line is read or until there is no more space
528*7c478bd9Sstevel@tonic-gate  * to be malloc'd.
529*7c478bd9Sstevel@tonic-gate  */
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate static char *
readline(char ** bufferp,size_t * bufsizp,FILE * filep)532*7c478bd9Sstevel@tonic-gate readline(char **bufferp, size_t *bufsizp, FILE *filep)
533*7c478bd9Sstevel@tonic-gate {
534*7c478bd9Sstevel@tonic-gate 	char *bufp;
535*7c478bd9Sstevel@tonic-gate 	size_t newsize;		/* number of bytes to make buffer */
536*7c478bd9Sstevel@tonic-gate 	size_t oldsize;
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	(*bufferp)[*bufsizp - 1] = '\t'; /* arbitrary non-zero character */
539*7c478bd9Sstevel@tonic-gate 	(*bufferp)[*bufsizp - 2] = ' ';	/* arbitrary non-newline char */
540*7c478bd9Sstevel@tonic-gate 	bufp = fgets(*bufferp, *bufsizp, filep);
541*7c478bd9Sstevel@tonic-gate 	if (bufp == NULL)
542*7c478bd9Sstevel@tonic-gate 		return (bufp);
543*7c478bd9Sstevel@tonic-gate 	while ((*bufferp)[*bufsizp -1] == '\0' &&
544*7c478bd9Sstevel@tonic-gate 	    (*bufferp)[*bufsizp - 2] != '\n' &&
545*7c478bd9Sstevel@tonic-gate 	    strlen(*bufferp) == *bufsizp - 1) {
546*7c478bd9Sstevel@tonic-gate 		newsize = 2 * (*bufsizp);
547*7c478bd9Sstevel@tonic-gate 		bufp = (char *)realloc((void *)*bufferp, newsize);
548*7c478bd9Sstevel@tonic-gate 		if (bufp == NULL)
549*7c478bd9Sstevel@tonic-gate 			fatal("Out of memory");
550*7c478bd9Sstevel@tonic-gate 		oldsize = *bufsizp;
551*7c478bd9Sstevel@tonic-gate 		*bufsizp = newsize;
552*7c478bd9Sstevel@tonic-gate 		*bufferp = bufp;
553*7c478bd9Sstevel@tonic-gate 		(*bufferp)[*bufsizp - 1] = '\t';
554*7c478bd9Sstevel@tonic-gate 		(*bufferp)[*bufsizp - 2] = ' ';
555*7c478bd9Sstevel@tonic-gate 		bufp = fgets(*bufferp + oldsize -1, oldsize + 1, filep);
556*7c478bd9Sstevel@tonic-gate 		if (bufp == NULL) {
557*7c478bd9Sstevel@tonic-gate 			if (filep->_flag & _IOEOF) {
558*7c478bd9Sstevel@tonic-gate 				bufp = *bufferp;
559*7c478bd9Sstevel@tonic-gate 				break;
560*7c478bd9Sstevel@tonic-gate 			} else
561*7c478bd9Sstevel@tonic-gate 				fatal("Read error");
562*7c478bd9Sstevel@tonic-gate 		} else
563*7c478bd9Sstevel@tonic-gate 			bufp = *bufferp;
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 	return (bufp);
566*7c478bd9Sstevel@tonic-gate }
567