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