xref: /onnv-gate/usr/src/cmd/csplit/csplit.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * csplit - Context or line file splitter
35*0Sstevel@tonic-gate  * Compile: cc -O -s -o csplit csplit.c
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <unistd.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <ctype.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <limits.h>
45*0Sstevel@tonic-gate #include <regexpr.h>
46*0Sstevel@tonic-gate #include <signal.h>
47*0Sstevel@tonic-gate #include <locale.h>
48*0Sstevel@tonic-gate #include <libintl.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	LAST	0LL
51*0Sstevel@tonic-gate #define	ERR	-1
52*0Sstevel@tonic-gate #define	FALSE	0
53*0Sstevel@tonic-gate #define	TRUE	1
54*0Sstevel@tonic-gate #define	EXPMODE	2
55*0Sstevel@tonic-gate #define	LINMODE	3
56*0Sstevel@tonic-gate #define	LINSIZ	LINE_MAX	/* POSIX.2 - read lines LINE_MAX long */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	/* Globals */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate char linbuf[LINSIZ];		/* Input line buffer */
61*0Sstevel@tonic-gate char *expbuf;
62*0Sstevel@tonic-gate char tmpbuf[BUFSIZ];		/* Temporary buffer for stdin */
63*0Sstevel@tonic-gate char file[8192] = "xx";		/* File name buffer */
64*0Sstevel@tonic-gate char *targ;			/* Arg ptr for error messages */
65*0Sstevel@tonic-gate char *sptr;
66*0Sstevel@tonic-gate FILE *infile, *outfile;		/* I/O file streams */
67*0Sstevel@tonic-gate int silent, keep, create;	/* Flags: -s(ilent), -k(eep), (create) */
68*0Sstevel@tonic-gate int errflg;
69*0Sstevel@tonic-gate int fiwidth = 2;		/* file index width (output file names) */
70*0Sstevel@tonic-gate extern int optind;
71*0Sstevel@tonic-gate extern char *optarg;
72*0Sstevel@tonic-gate offset_t offset;		/* Regular expression offset value */
73*0Sstevel@tonic-gate offset_t curline;		/* Current line in input file */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * These defines are needed for regexp handling(see regexp(7))
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate #define	PERROR(x)	fatal("%s: Illegal Regular Expression\n", targ);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate static int asc_to_ll(char *, long long *);
81*0Sstevel@tonic-gate static void closefile(void);
82*0Sstevel@tonic-gate static void fatal(char *, char *);
83*0Sstevel@tonic-gate static offset_t findline(char *, offset_t);
84*0Sstevel@tonic-gate static void flush(void);
85*0Sstevel@tonic-gate static FILE *getfile(void);
86*0Sstevel@tonic-gate static char *getline(int);
87*0Sstevel@tonic-gate static void line_arg(char *);
88*0Sstevel@tonic-gate static void num_arg(char *, int);
89*0Sstevel@tonic-gate static void re_arg(char *);
90*0Sstevel@tonic-gate static void sig(int);
91*0Sstevel@tonic-gate static void to_line(offset_t);
92*0Sstevel@tonic-gate static void usage(void);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate int
95*0Sstevel@tonic-gate main(int argc, char **argv)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	int ch, mode;
98*0Sstevel@tonic-gate 	char *ptr;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
101*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
102*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
103*0Sstevel@tonic-gate #endif
104*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "skf:n:")) != EOF) {
107*0Sstevel@tonic-gate 		switch (ch) {
108*0Sstevel@tonic-gate 			case 'f':
109*0Sstevel@tonic-gate 				(void) strcpy(file, optarg);
110*0Sstevel@tonic-gate 				if ((ptr = strrchr(optarg, '/')) == NULL)
111*0Sstevel@tonic-gate 					ptr = optarg;
112*0Sstevel@tonic-gate 				else
113*0Sstevel@tonic-gate 					ptr++;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 				break;
116*0Sstevel@tonic-gate 			case 'n':		/* POSIX.2 */
117*0Sstevel@tonic-gate 				for (ptr = optarg; *ptr != NULL; ptr++)
118*0Sstevel@tonic-gate 					if (!isdigit((int)*ptr))
119*0Sstevel@tonic-gate 						fatal("-n num\n", NULL);
120*0Sstevel@tonic-gate 				fiwidth = atoi(optarg);
121*0Sstevel@tonic-gate 				break;
122*0Sstevel@tonic-gate 			case 'k':
123*0Sstevel@tonic-gate 				keep++;
124*0Sstevel@tonic-gate 				break;
125*0Sstevel@tonic-gate 			case 's':
126*0Sstevel@tonic-gate 				silent++;
127*0Sstevel@tonic-gate 				break;
128*0Sstevel@tonic-gate 			case '?':
129*0Sstevel@tonic-gate 				errflg++;
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	argv = &argv[optind];
134*0Sstevel@tonic-gate 	argc -= optind;
135*0Sstevel@tonic-gate 	if (argc <= 1 || errflg)
136*0Sstevel@tonic-gate 		usage();
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	if (strcmp(*argv, "-") == 0) {
139*0Sstevel@tonic-gate 		infile = tmpfile();
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		while (fread(tmpbuf, 1, BUFSIZ, stdin) != 0) {
142*0Sstevel@tonic-gate 			if (fwrite(tmpbuf, 1, BUFSIZ, infile) == 0)
143*0Sstevel@tonic-gate 				if (errno == ENOSPC) {
144*0Sstevel@tonic-gate 					(void) fprintf(stderr, "csplit: ");
145*0Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
146*0Sstevel@tonic-gate 						"No space left on device\n"));
147*0Sstevel@tonic-gate 					exit(1);
148*0Sstevel@tonic-gate 				} else {
149*0Sstevel@tonic-gate 					(void) fprintf(stderr, "csplit: ");
150*0Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
151*0Sstevel@tonic-gate 						"Bad write to temporary "
152*0Sstevel@tonic-gate 							"file\n"));
153*0Sstevel@tonic-gate 					exit(1);
154*0Sstevel@tonic-gate 				}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* clear the buffer to get correct size when writing buffer */
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 			(void) memset(tmpbuf, '\0', sizeof (tmpbuf));
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 		rewind(infile);
161*0Sstevel@tonic-gate 	} else if ((infile = fopen(*argv, "r")) == NULL)
162*0Sstevel@tonic-gate 		fatal("Cannot open %s\n", *argv);
163*0Sstevel@tonic-gate 	++argv;
164*0Sstevel@tonic-gate 	curline = (offset_t)1;
165*0Sstevel@tonic-gate 	(void) signal(SIGINT, sig);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * The following for loop handles the different argument types.
169*0Sstevel@tonic-gate 	 * A switch is performed on the first character of the argument
170*0Sstevel@tonic-gate 	 * and each case calls the appropriate argument handling routine.
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	for (; *argv; ++argv) {
174*0Sstevel@tonic-gate 		targ = *argv;
175*0Sstevel@tonic-gate 		switch (**argv) {
176*0Sstevel@tonic-gate 		case '/':
177*0Sstevel@tonic-gate 			mode = EXPMODE;
178*0Sstevel@tonic-gate 			create = TRUE;
179*0Sstevel@tonic-gate 			re_arg(*argv);
180*0Sstevel@tonic-gate 			break;
181*0Sstevel@tonic-gate 		case '%':
182*0Sstevel@tonic-gate 			mode = EXPMODE;
183*0Sstevel@tonic-gate 			create = FALSE;
184*0Sstevel@tonic-gate 			re_arg(*argv);
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 		case '{':
187*0Sstevel@tonic-gate 			num_arg(*argv, mode);
188*0Sstevel@tonic-gate 			mode = FALSE;
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 		default:
191*0Sstevel@tonic-gate 			mode = LINMODE;
192*0Sstevel@tonic-gate 			create = TRUE;
193*0Sstevel@tonic-gate 			line_arg(*argv);
194*0Sstevel@tonic-gate 			break;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	create = TRUE;
198*0Sstevel@tonic-gate 	to_line(LAST);
199*0Sstevel@tonic-gate 	return (0);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * asc_to_ll takes an ascii argument(str) and converts it to a long long(plc)
204*0Sstevel@tonic-gate  * It returns ERR if an illegal character.  The reason that asc_to_ll
205*0Sstevel@tonic-gate  * does not return an answer(long long) is that any value for the long
206*0Sstevel@tonic-gate  * long is legal, and this version of asc_to_ll detects error strings.
207*0Sstevel@tonic-gate  */
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate static int
210*0Sstevel@tonic-gate asc_to_ll(char *str, long long *plc)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	int f;
213*0Sstevel@tonic-gate 	*plc = 0;
214*0Sstevel@tonic-gate 	f = 0;
215*0Sstevel@tonic-gate 	for (; ; str++) {
216*0Sstevel@tonic-gate 		switch (*str) {
217*0Sstevel@tonic-gate 		case ' ':
218*0Sstevel@tonic-gate 		case '\t':
219*0Sstevel@tonic-gate 			continue;
220*0Sstevel@tonic-gate 		case '-':
221*0Sstevel@tonic-gate 			f++;
222*0Sstevel@tonic-gate 			/* FALLTHROUGH */
223*0Sstevel@tonic-gate 		case '+':
224*0Sstevel@tonic-gate 			str++;
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 		break;
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	for (; *str != NULL; str++)
229*0Sstevel@tonic-gate 		if (*str >= '0' && *str <= '9')
230*0Sstevel@tonic-gate 			*plc = *plc * 10 + *str - '0';
231*0Sstevel@tonic-gate 		else
232*0Sstevel@tonic-gate 			return (ERR);
233*0Sstevel@tonic-gate 	if (f)
234*0Sstevel@tonic-gate 		*plc = -(*plc);
235*0Sstevel@tonic-gate 	return (TRUE);	/* not error */
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * Closefile prints the byte count of the file created,(via fseeko
240*0Sstevel@tonic-gate  * and ftello), if the create flag is on and the silent flag is not on.
241*0Sstevel@tonic-gate  * If the create flag is on closefile then closes the file(fclose).
242*0Sstevel@tonic-gate  */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate static void
245*0Sstevel@tonic-gate closefile()
246*0Sstevel@tonic-gate {
247*0Sstevel@tonic-gate 	if (!silent && create) {
248*0Sstevel@tonic-gate 		(void) fseeko(outfile, (offset_t)0, SEEK_END);
249*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%lld\n", (offset_t)ftello(outfile));
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 	if (create)
252*0Sstevel@tonic-gate 		(void) fclose(outfile);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /*
256*0Sstevel@tonic-gate  * Fatal handles error messages and cleanup.
257*0Sstevel@tonic-gate  * Because "arg" can be the global file, and the cleanup processing
258*0Sstevel@tonic-gate  * uses the global file, the error message is printed first.  If the
259*0Sstevel@tonic-gate  * "keep" flag is not set, fatal unlinks all created files.  If the
260*0Sstevel@tonic-gate  * "keep" flag is set, fatal closes the current file(if there is one).
261*0Sstevel@tonic-gate  * Fatal exits with a value of 1.
262*0Sstevel@tonic-gate  */
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate static void
265*0Sstevel@tonic-gate fatal(char *string, char *arg)
266*0Sstevel@tonic-gate {
267*0Sstevel@tonic-gate 	char *fls;
268*0Sstevel@tonic-gate 	int num;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	(void) fprintf(stderr, "csplit: ");
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	/* gettext dynamically replaces string */
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(string), arg);
275*0Sstevel@tonic-gate 	if (!keep) {
276*0Sstevel@tonic-gate 		if (outfile) {
277*0Sstevel@tonic-gate 			(void) fclose(outfile);
278*0Sstevel@tonic-gate 			for (fls = file; *fls != '\0'; fls++)
279*0Sstevel@tonic-gate 				continue;
280*0Sstevel@tonic-gate 			fls -= fiwidth;
281*0Sstevel@tonic-gate 			for (num = atoi(fls); num >= 0; num--) {
282*0Sstevel@tonic-gate 				(void) sprintf(fls, "%.*d", fiwidth, num);
283*0Sstevel@tonic-gate 				(void) unlink(file);
284*0Sstevel@tonic-gate 			}
285*0Sstevel@tonic-gate 		}
286*0Sstevel@tonic-gate 	} else
287*0Sstevel@tonic-gate 		if (outfile)
288*0Sstevel@tonic-gate 			closefile();
289*0Sstevel@tonic-gate 	exit(1);
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate /*
293*0Sstevel@tonic-gate  * Findline returns the line number referenced by the current argument.
294*0Sstevel@tonic-gate  * Its arguments are a pointer to the compiled regular expression(expr),
295*0Sstevel@tonic-gate  * and an offset(oset).  The variable lncnt is used to count the number
296*0Sstevel@tonic-gate  * of lines searched.  First the current stream location is saved via
297*0Sstevel@tonic-gate  * ftello(), and getline is called so that R.E. searching starts at the
298*0Sstevel@tonic-gate  * line after the previously referenced line.  The while loop checks
299*0Sstevel@tonic-gate  * that there are more lines(error if none), bumps the line count, and
300*0Sstevel@tonic-gate  * checks for the R.E. on each line.  If the R.E. matches on one of the
301*0Sstevel@tonic-gate  * lines the old stream location is restored, and the line number
302*0Sstevel@tonic-gate  * referenced by the R.E. and the offset is returned.
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate static offset_t
306*0Sstevel@tonic-gate findline(char *expr, offset_t oset)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	static int benhere = 0;
309*0Sstevel@tonic-gate 	offset_t lncnt = 0, saveloc;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	saveloc = ftello(infile);
312*0Sstevel@tonic-gate 	if (curline != (offset_t)1 || benhere)	/* If first line, first time, */
313*0Sstevel@tonic-gate 		(void) getline(FALSE);		/* then don't skip */
314*0Sstevel@tonic-gate 	else
315*0Sstevel@tonic-gate 		lncnt--;
316*0Sstevel@tonic-gate 	benhere = 1;
317*0Sstevel@tonic-gate 	while (getline(FALSE) != NULL) {
318*0Sstevel@tonic-gate 		lncnt++;
319*0Sstevel@tonic-gate 		if ((sptr = strrchr(linbuf, '\n')) != NULL)
320*0Sstevel@tonic-gate 			*sptr = '\0';
321*0Sstevel@tonic-gate 		if (step(linbuf, expr)) {
322*0Sstevel@tonic-gate 			(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
323*0Sstevel@tonic-gate 			return (curline+lncnt+oset);
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 	(void) fseeko(infile, (offset_t)saveloc, SEEK_SET);
327*0Sstevel@tonic-gate 	return (curline+lncnt+oset+2);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate /*
331*0Sstevel@tonic-gate  * Flush uses fputs to put lines on the output file stream(outfile)
332*0Sstevel@tonic-gate  * Since fputs does its own buffering, flush doesn't need to.
333*0Sstevel@tonic-gate  * Flush does nothing if the create flag is not set.
334*0Sstevel@tonic-gate  */
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate static void
337*0Sstevel@tonic-gate flush()
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	if (create)
340*0Sstevel@tonic-gate 		(void) fputs(linbuf, outfile);
341*0Sstevel@tonic-gate }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate /*
344*0Sstevel@tonic-gate  * Getfile does nothing if the create flag is not set.  If the create
345*0Sstevel@tonic-gate  * flag is set, getfile positions the file pointer(fptr) at the end of
346*0Sstevel@tonic-gate  * the file name prefix on the first call(fptr=0).  The file counter is
347*0Sstevel@tonic-gate  * stored in the file name and incremented.  If the subsequent fopen
348*0Sstevel@tonic-gate  * fails, the file name is copied to tfile for the error message, the
349*0Sstevel@tonic-gate  * previous file name is restored for cleanup, and fatal is called.  If
350*0Sstevel@tonic-gate  * the fopen succeeds, the stream(opfil) is returned.
351*0Sstevel@tonic-gate  */
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate FILE *
354*0Sstevel@tonic-gate getfile()
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	static char *fptr;
357*0Sstevel@tonic-gate 	static int ctr;
358*0Sstevel@tonic-gate 	FILE *opfil;
359*0Sstevel@tonic-gate 	char tfile[15];
360*0Sstevel@tonic-gate 	char *delim;
361*0Sstevel@tonic-gate 	char savedelim;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	if (create) {
364*0Sstevel@tonic-gate 		if (fptr == 0)
365*0Sstevel@tonic-gate 			for (fptr = file; *fptr != NULL; fptr++);
366*0Sstevel@tonic-gate 		(void) sprintf(fptr, "%.*d", fiwidth, ctr++);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 		/* check for suffix length overflow */
369*0Sstevel@tonic-gate 		if (strlen(fptr) > fiwidth) {
370*0Sstevel@tonic-gate 			fatal("Suffix longer than %ld chars; increase -n\n",
371*0Sstevel@tonic-gate 			    (char *)fiwidth);
372*0Sstevel@tonic-gate 		}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 		/* check for filename length overflow */
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 		delim = strrchr(file, '/');
377*0Sstevel@tonic-gate 		if (delim == (char *)NULL) {
378*0Sstevel@tonic-gate 			if (strlen(file) > pathconf(".", _PC_NAME_MAX)) {
379*0Sstevel@tonic-gate 				fatal("Name too long: %s\n", file);
380*0Sstevel@tonic-gate 			}
381*0Sstevel@tonic-gate 		} else {
382*0Sstevel@tonic-gate 			/* truncate file at pathname delim to do pathconf */
383*0Sstevel@tonic-gate 			savedelim = *delim;
384*0Sstevel@tonic-gate 			*delim = '\0';
385*0Sstevel@tonic-gate 			/*
386*0Sstevel@tonic-gate 			 * file: pppppppp\0fffff\0
387*0Sstevel@tonic-gate 			 * ..... ^ file
388*0Sstevel@tonic-gate 			 * ............. ^ delim
389*0Sstevel@tonic-gate 			 */
390*0Sstevel@tonic-gate 			if (strlen(delim + 1) > pathconf(file, _PC_NAME_MAX)) {
391*0Sstevel@tonic-gate 				fatal("Name too long: %s\n", delim + 1);
392*0Sstevel@tonic-gate 			}
393*0Sstevel@tonic-gate 			*delim = savedelim;
394*0Sstevel@tonic-gate 		}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		if ((opfil = fopen(file, "w")) == NULL) {
397*0Sstevel@tonic-gate 			(void) strcpy(tfile, file);
398*0Sstevel@tonic-gate 			(void) sprintf(fptr, "%.*d", fiwidth, (ctr-2));
399*0Sstevel@tonic-gate 			fatal("Cannot create %s\n", tfile);
400*0Sstevel@tonic-gate 		}
401*0Sstevel@tonic-gate 		return (opfil);
402*0Sstevel@tonic-gate 	}
403*0Sstevel@tonic-gate 	return (NULL);
404*0Sstevel@tonic-gate }
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate /*
407*0Sstevel@tonic-gate  * Getline gets a line via fgets from the input stream "infile".
408*0Sstevel@tonic-gate  * The line is put into linbuf and may not be larger than LINSIZ.
409*0Sstevel@tonic-gate  * If getline is called with a non-zero value, the current line
410*0Sstevel@tonic-gate  * is bumped, otherwise it is not(for R.E. searching).
411*0Sstevel@tonic-gate  */
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate static char *
414*0Sstevel@tonic-gate getline(int bumpcur)
415*0Sstevel@tonic-gate {
416*0Sstevel@tonic-gate 	char *ret;
417*0Sstevel@tonic-gate 	if (bumpcur)
418*0Sstevel@tonic-gate 		curline++;
419*0Sstevel@tonic-gate 	ret = fgets(linbuf, LINSIZ, infile);
420*0Sstevel@tonic-gate 	return (ret);
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate /*
424*0Sstevel@tonic-gate  * Line_arg handles line number arguments.
425*0Sstevel@tonic-gate  * line_arg takes as its argument a pointer to a character string
426*0Sstevel@tonic-gate  * (assumed to be a line number).  If that character string can be
427*0Sstevel@tonic-gate  * converted to a number(long long), to_line is called with that number,
428*0Sstevel@tonic-gate  * otherwise error.
429*0Sstevel@tonic-gate  */
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate static void
432*0Sstevel@tonic-gate line_arg(char *line)
433*0Sstevel@tonic-gate {
434*0Sstevel@tonic-gate 	long long to;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if (asc_to_ll(line, &to) == ERR)
437*0Sstevel@tonic-gate 		fatal("%s: bad line number\n", line);
438*0Sstevel@tonic-gate 	to_line(to);
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate /*
442*0Sstevel@tonic-gate  * Num_arg handles repeat arguments.
443*0Sstevel@tonic-gate  * Num_arg copies the numeric argument to "rep" (error if number is
444*0Sstevel@tonic-gate  * larger than 20 characters or } is left off).  Num_arg then converts
445*0Sstevel@tonic-gate  * the number and checks for validity.  Next num_arg checks the mode
446*0Sstevel@tonic-gate  * of the previous argument, and applys the argument the correct number
447*0Sstevel@tonic-gate  * of times. If the mode is not set properly its an error.
448*0Sstevel@tonic-gate  */
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate static void
451*0Sstevel@tonic-gate num_arg(char *arg, int md)
452*0Sstevel@tonic-gate {
453*0Sstevel@tonic-gate 	offset_t repeat, toline;
454*0Sstevel@tonic-gate 	char rep[21];
455*0Sstevel@tonic-gate 	char *ptr;
456*0Sstevel@tonic-gate 	int		len;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	ptr = rep;
459*0Sstevel@tonic-gate 	for (++arg; *arg != '}'; arg += len) {
460*0Sstevel@tonic-gate 		if (*arg == NULL)
461*0Sstevel@tonic-gate 			fatal("%s: missing '}'\n", targ);
462*0Sstevel@tonic-gate 		if ((len = mblen(arg, MB_LEN_MAX)) <= 0)
463*0Sstevel@tonic-gate 			len = 1;
464*0Sstevel@tonic-gate 		if ((ptr + len) >= &rep[20])
465*0Sstevel@tonic-gate 			fatal("%s: Repeat count too large\n", targ);
466*0Sstevel@tonic-gate 		(void) memcpy(ptr, arg, len);
467*0Sstevel@tonic-gate 		ptr += len;
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 	*ptr = NULL;
470*0Sstevel@tonic-gate 	if ((asc_to_ll(rep, &repeat) == ERR) || repeat < 0L)
471*0Sstevel@tonic-gate 		fatal("Illegal repeat count: %s\n", targ);
472*0Sstevel@tonic-gate 	if (md == LINMODE) {
473*0Sstevel@tonic-gate 		toline = offset = curline;
474*0Sstevel@tonic-gate 		for (; repeat > 0LL; repeat--) {
475*0Sstevel@tonic-gate 			toline += offset;
476*0Sstevel@tonic-gate 			to_line(toline);
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 	} else	if (md == EXPMODE)
479*0Sstevel@tonic-gate 			for (; repeat > 0LL; repeat--)
480*0Sstevel@tonic-gate 				to_line(findline(expbuf, offset));
481*0Sstevel@tonic-gate 		else
482*0Sstevel@tonic-gate 			fatal("No operation for %s\n", targ);
483*0Sstevel@tonic-gate }
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate /*
486*0Sstevel@tonic-gate  * Re_arg handles regular expression arguments.
487*0Sstevel@tonic-gate  * Re_arg takes a csplit regular expression argument.  It checks for
488*0Sstevel@tonic-gate  * delimiter balance, computes any offset, and compiles the regular
489*0Sstevel@tonic-gate  * expression.  Findline is called with the compiled expression and
490*0Sstevel@tonic-gate  * offset, and returns the corresponding line number, which is used
491*0Sstevel@tonic-gate  * as input to the to_line function.
492*0Sstevel@tonic-gate  */
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate static void
495*0Sstevel@tonic-gate re_arg(char *string)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	char *ptr;
498*0Sstevel@tonic-gate 	char ch;
499*0Sstevel@tonic-gate 	int		len;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	ch = *string;
502*0Sstevel@tonic-gate 	ptr = string;
503*0Sstevel@tonic-gate 	ptr++;
504*0Sstevel@tonic-gate 	while (*ptr != ch) {
505*0Sstevel@tonic-gate 		if (*ptr == '\\')
506*0Sstevel@tonic-gate 			++ptr;
507*0Sstevel@tonic-gate 
508*0Sstevel@tonic-gate 		if (*ptr == NULL)
509*0Sstevel@tonic-gate 			fatal("%s: missing delimiter\n", targ);
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 		if ((len = mblen(ptr, MB_LEN_MAX)) <= 0)
512*0Sstevel@tonic-gate 			len = 1;
513*0Sstevel@tonic-gate 		ptr += len;
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	/*
517*0Sstevel@tonic-gate 	 * The line below was added because compile no longer supports
518*0Sstevel@tonic-gate 	 * the fourth argument being passed.  The fourth argument used
519*0Sstevel@tonic-gate 	 * to be '/' or '%'.
520*0Sstevel@tonic-gate 	 */
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	*ptr = NULL;
523*0Sstevel@tonic-gate 	if (asc_to_ll(++ptr, &offset) == ERR)
524*0Sstevel@tonic-gate 		fatal("%s: illegal offset\n", string);
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	/*
527*0Sstevel@tonic-gate 	 * The line below was added because INIT which did this for us
528*0Sstevel@tonic-gate 	 * was removed from compile in regexp.h
529*0Sstevel@tonic-gate 	 */
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	string++;
532*0Sstevel@tonic-gate 	expbuf = compile(string, (char *)0, (char *)0);
533*0Sstevel@tonic-gate 	if (regerrno)
534*0Sstevel@tonic-gate 		PERROR(regerrno);
535*0Sstevel@tonic-gate 	to_line(findline(expbuf, offset));
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate /*
539*0Sstevel@tonic-gate  * Sig handles breaks.  When a break occurs the signal is reset,
540*0Sstevel@tonic-gate  * and fatal is called to clean up and print the argument which
541*0Sstevel@tonic-gate  * was being processed at the time the interrupt occured.
542*0Sstevel@tonic-gate  */
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate /* ARGSUSED */
545*0Sstevel@tonic-gate static void
546*0Sstevel@tonic-gate sig(int s)
547*0Sstevel@tonic-gate {
548*0Sstevel@tonic-gate 	(void) signal(SIGINT, sig);
549*0Sstevel@tonic-gate 	fatal("Interrupt - program aborted at arg '%s'\n", targ);
550*0Sstevel@tonic-gate }
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate /*
553*0Sstevel@tonic-gate  * To_line creates split files.
554*0Sstevel@tonic-gate  * To_line gets as its argument the line which the current argument
555*0Sstevel@tonic-gate  * referenced.  To_line calls getfile for a new output stream, which
556*0Sstevel@tonic-gate  * does nothing if create is False.  If to_line's argument is not LAST
557*0Sstevel@tonic-gate  * it checks that the current line is not greater than its argument.
558*0Sstevel@tonic-gate  * While the current line is less than the desired line to_line gets
559*0Sstevel@tonic-gate  * lines and flushes(error if EOF is reached).
560*0Sstevel@tonic-gate  * If to_line's argument is LAST, it checks for more lines, and gets
561*0Sstevel@tonic-gate  * and flushes lines till the end of file.
562*0Sstevel@tonic-gate  * Finally, to_line calls closefile to close the output stream.
563*0Sstevel@tonic-gate  */
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate static void
566*0Sstevel@tonic-gate to_line(offset_t ln)
567*0Sstevel@tonic-gate {
568*0Sstevel@tonic-gate 	outfile = getfile();
569*0Sstevel@tonic-gate 	if (ln != LAST) {
570*0Sstevel@tonic-gate 		if (curline > ln)
571*0Sstevel@tonic-gate 			fatal("%s - out of range\n", targ);
572*0Sstevel@tonic-gate 		while (curline < ln) {
573*0Sstevel@tonic-gate 			if (getline(TRUE) == NULL)
574*0Sstevel@tonic-gate 				fatal("%s - out of range\n", targ);
575*0Sstevel@tonic-gate 			flush();
576*0Sstevel@tonic-gate 		}
577*0Sstevel@tonic-gate 	} else		/* last file */
578*0Sstevel@tonic-gate 		if (getline(TRUE) != NULL) {
579*0Sstevel@tonic-gate 			flush();
580*0Sstevel@tonic-gate 			for (;;) {
581*0Sstevel@tonic-gate 				if (getline(TRUE) == NULL)
582*0Sstevel@tonic-gate 					break;
583*0Sstevel@tonic-gate 				flush();
584*0Sstevel@tonic-gate 			}
585*0Sstevel@tonic-gate 		} else
586*0Sstevel@tonic-gate 			fatal("%s - out of range\n", targ);
587*0Sstevel@tonic-gate 	closefile();
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate static void
591*0Sstevel@tonic-gate usage()
592*0Sstevel@tonic-gate {
593*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
594*0Sstevel@tonic-gate 		"usage: csplit [-ks] [-f prefix] [-n number] "
595*0Sstevel@tonic-gate 			"file arg1 ...argn\n"));
596*0Sstevel@tonic-gate 	exit(1);
597*0Sstevel@tonic-gate }
598