xref: /titanic_53/usr/src/cmd/split/split.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Portions Copyright (c) 1988,1996,1999 by Sun Microsystems, Inc. */
27*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved.					*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
34*7c478bd9Sstevel@tonic-gate #include <locale.h>
35*7c478bd9Sstevel@tonic-gate #include <malloc.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <wchar.h>
40*7c478bd9Sstevel@tonic-gate #include <widec.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <errno.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	DEFAULT_LINES	1000
46*7c478bd9Sstevel@tonic-gate #define	ONE_K		1024
47*7c478bd9Sstevel@tonic-gate #define	ONE_M		ONE_K*ONE_K
48*7c478bd9Sstevel@tonic-gate #ifndef	TRUE
49*7c478bd9Sstevel@tonic-gate #define	TRUE		1
50*7c478bd9Sstevel@tonic-gate #define	FALSE		0
51*7c478bd9Sstevel@tonic-gate #endif
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static	void	Usage();
55*7c478bd9Sstevel@tonic-gate static	void	next_file_name();
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static	char	*progname;
59*7c478bd9Sstevel@tonic-gate static	int	suffix_length = 2;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	long long	line_count = 0;
65*7c478bd9Sstevel@tonic-gate 	long long	byte_count = 0;
66*7c478bd9Sstevel@tonic-gate 	long long	out;
67*7c478bd9Sstevel@tonic-gate 	char	*fname = NULL;
68*7c478bd9Sstevel@tonic-gate 	char	head[MAXPATHLEN];
69*7c478bd9Sstevel@tonic-gate 	char	*output_file_name;
70*7c478bd9Sstevel@tonic-gate 	char	*tail;
71*7c478bd9Sstevel@tonic-gate 	char	*last;
72*7c478bd9Sstevel@tonic-gate 	FILE	*in_file = NULL;
73*7c478bd9Sstevel@tonic-gate 	FILE	*out_file = (FILE *)NULL;
74*7c478bd9Sstevel@tonic-gate 	int	i;
75*7c478bd9Sstevel@tonic-gate 	int	c;
76*7c478bd9Sstevel@tonic-gate 	wint_t	wc;
77*7c478bd9Sstevel@tonic-gate 	int	output_file_open;
78*7c478bd9Sstevel@tonic-gate 	struct	statvfs stbuf;
79*7c478bd9Sstevel@tonic-gate 	int	opt;
80*7c478bd9Sstevel@tonic-gate 	int	non_standard_line_count = FALSE;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
84*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
85*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	progname = argv[0];
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	/* check for explicit stdin "-" option */
92*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
93*7c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "-") == 0) {
94*7c478bd9Sstevel@tonic-gate 			in_file = stdin;
95*7c478bd9Sstevel@tonic-gate 			while (i < argc) {
96*7c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 				/* a "-" before "--" is an error */
99*7c478bd9Sstevel@tonic-gate 				if ((argv[i] != NULL) &&
100*7c478bd9Sstevel@tonic-gate 				    (strcmp(argv[i], "--") == 0)) {
101*7c478bd9Sstevel@tonic-gate 					Usage();
102*7c478bd9Sstevel@tonic-gate 				}
103*7c478bd9Sstevel@tonic-gate 				i++;
104*7c478bd9Sstevel@tonic-gate 			}
105*7c478bd9Sstevel@tonic-gate 			argc--;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	/* check for non-standard "-line-count" option */
110*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
111*7c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
115*7c478bd9Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
116*7c478bd9Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
117*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
118*7c478bd9Sstevel@tonic-gate 				    "%s: Badly formed number\n"), progname);
119*7c478bd9Sstevel@tonic-gate 				Usage();
120*7c478bd9Sstevel@tonic-gate 			}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 			line_count = (long long) strtoll(&argv[i][1],
123*7c478bd9Sstevel@tonic-gate 			    (char **)NULL, 10);
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 			non_standard_line_count = TRUE;
126*7c478bd9Sstevel@tonic-gate 			while (i < argc) {
127*7c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
128*7c478bd9Sstevel@tonic-gate 				i++;
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 			argc--;
131*7c478bd9Sstevel@tonic-gate 		}
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/* get options */
135*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "a:b:l:")) != EOF) {
136*7c478bd9Sstevel@tonic-gate 		switch (opt) {
137*7c478bd9Sstevel@tonic-gate 		case 'a':
138*7c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
139*7c478bd9Sstevel@tonic-gate 				Usage();
140*7c478bd9Sstevel@tonic-gate 			}
141*7c478bd9Sstevel@tonic-gate 			suffix_length = (long long) strtoll(optarg,
142*7c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
143*7c478bd9Sstevel@tonic-gate 			if (suffix_length <= 0) {
144*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
145*7c478bd9Sstevel@tonic-gate 				    "%s: Invalid \"-a %s\" option\n"),
146*7c478bd9Sstevel@tonic-gate 				    progname, optarg);
147*7c478bd9Sstevel@tonic-gate 				Usage();
148*7c478bd9Sstevel@tonic-gate 			}
149*7c478bd9Sstevel@tonic-gate 			break;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 		case 'b':
152*7c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
153*7c478bd9Sstevel@tonic-gate 				Usage();
154*7c478bd9Sstevel@tonic-gate 			}
155*7c478bd9Sstevel@tonic-gate 			byte_count = (long long) strtoll(optarg,
156*7c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
157*7c478bd9Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'k')
158*7c478bd9Sstevel@tonic-gate 				byte_count *= ONE_K;
159*7c478bd9Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'm')
160*7c478bd9Sstevel@tonic-gate 				byte_count *= ONE_M;
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		case 'l':
164*7c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
165*7c478bd9Sstevel@tonic-gate 				Usage();
166*7c478bd9Sstevel@tonic-gate 			}
167*7c478bd9Sstevel@tonic-gate 			if (non_standard_line_count == TRUE) {
168*7c478bd9Sstevel@tonic-gate 				Usage();
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 			line_count = (long long) strtoll(optarg,
171*7c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		default:
175*7c478bd9Sstevel@tonic-gate 			Usage();
176*7c478bd9Sstevel@tonic-gate 		}
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/* get input file */
180*7c478bd9Sstevel@tonic-gate 	if ((in_file == NULL) && (optind < argc)) {
181*7c478bd9Sstevel@tonic-gate 		if ((in_file = fopen(argv[optind++], "r")) == NULL) {
182*7c478bd9Sstevel@tonic-gate 			(void) perror("split");
183*7c478bd9Sstevel@tonic-gate 			return (1);
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 	if (in_file == NULL) {
187*7c478bd9Sstevel@tonic-gate 		in_file = stdin;
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	/* get output file name */
191*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
192*7c478bd9Sstevel@tonic-gate 		output_file_name = argv[optind];
193*7c478bd9Sstevel@tonic-gate 		if ((tail = strrchr(output_file_name, '/')) == NULL) {
194*7c478bd9Sstevel@tonic-gate 			tail = output_file_name;
195*7c478bd9Sstevel@tonic-gate 			(void) getcwd(head, sizeof (head));
196*7c478bd9Sstevel@tonic-gate 		} else {
197*7c478bd9Sstevel@tonic-gate 			tail++;
198*7c478bd9Sstevel@tonic-gate 			(void) strcpy(head, output_file_name);
199*7c478bd9Sstevel@tonic-gate 			last = strrchr(head, '/');
200*7c478bd9Sstevel@tonic-gate 			*++last = '\0';
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		if (statvfs(head, &stbuf) < 0) {
204*7c478bd9Sstevel@tonic-gate 			perror(head);
205*7c478bd9Sstevel@tonic-gate 			return (1);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		if (strlen(tail) > (stbuf.f_namemax - suffix_length)) {
209*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
210*7c478bd9Sstevel@tonic-gate 			    "%s: More than %d characters in file name\n"),
211*7c478bd9Sstevel@tonic-gate 			    progname, stbuf.f_namemax - suffix_length);
212*7c478bd9Sstevel@tonic-gate 			Usage();
213*7c478bd9Sstevel@tonic-gate 		}
214*7c478bd9Sstevel@tonic-gate 	} else
215*7c478bd9Sstevel@tonic-gate 		output_file_name = "x";
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/* check options */
218*7c478bd9Sstevel@tonic-gate 	if (((int) strlen(output_file_name) + suffix_length) > FILENAME_MAX) {
219*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
220*7c478bd9Sstevel@tonic-gate 		"%s: Output file name too long\n"), progname);
221*7c478bd9Sstevel@tonic-gate 		return (1);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	if (line_count && byte_count) {
225*7c478bd9Sstevel@tonic-gate 		    Usage();
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	/* use default line count if none specified */
229*7c478bd9Sstevel@tonic-gate 	if (line_count == 0) {
230*7c478bd9Sstevel@tonic-gate 		line_count = DEFAULT_LINES;
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	/*
234*7c478bd9Sstevel@tonic-gate 	 * allocate buffer for the filenames we'll construct; it must be
235*7c478bd9Sstevel@tonic-gate 	 * big enough to hold the name in 'output_file_name' + an n-char
236*7c478bd9Sstevel@tonic-gate 	 * suffix + NULL terminator
237*7c478bd9Sstevel@tonic-gate 	 */
238*7c478bd9Sstevel@tonic-gate 	if ((fname = (char *)malloc(strlen(output_file_name) +
239*7c478bd9Sstevel@tonic-gate 	    suffix_length + 1)) == NULL) {
240*7c478bd9Sstevel@tonic-gate 		(void) perror("split");
241*7c478bd9Sstevel@tonic-gate 		return (1);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	/* build first output file name */
245*7c478bd9Sstevel@tonic-gate 	for (i = 0; output_file_name[i]; i++) {
246*7c478bd9Sstevel@tonic-gate 		fname[i] = output_file_name[i];
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 	while (i < (int) strlen(output_file_name) + suffix_length) {
249*7c478bd9Sstevel@tonic-gate 		fname[i++] = 'a';
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 	if (suffix_length)
252*7c478bd9Sstevel@tonic-gate 		fname[i - 1] = 'a' - 1;
253*7c478bd9Sstevel@tonic-gate 	fname[i] = '\0';
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	for (; ; ) {
256*7c478bd9Sstevel@tonic-gate 	    output_file_open = FALSE;
257*7c478bd9Sstevel@tonic-gate 	    if (byte_count) {
258*7c478bd9Sstevel@tonic-gate 		for (out = 0; out < byte_count; out++) {
259*7c478bd9Sstevel@tonic-gate 		    errno = 0;
260*7c478bd9Sstevel@tonic-gate 		    c = getc(in_file);
261*7c478bd9Sstevel@tonic-gate 		    if (c == EOF) {
262*7c478bd9Sstevel@tonic-gate 			if (errno != 0) {
263*7c478bd9Sstevel@tonic-gate 			    int lerrno = errno;
264*7c478bd9Sstevel@tonic-gate 			    (void) fprintf(stderr, gettext(
265*7c478bd9Sstevel@tonic-gate 				"%s: Read error at file offset %lld: %s, "
266*7c478bd9Sstevel@tonic-gate 				"aborting split\n"),
267*7c478bd9Sstevel@tonic-gate 				    progname, ftello(in_file),
268*7c478bd9Sstevel@tonic-gate 				    strerror(lerrno));
269*7c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
270*7c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
271*7c478bd9Sstevel@tonic-gate 			    free(fname);
272*7c478bd9Sstevel@tonic-gate 			    return (1);
273*7c478bd9Sstevel@tonic-gate 			}
274*7c478bd9Sstevel@tonic-gate 			if (output_file_open == TRUE)
275*7c478bd9Sstevel@tonic-gate 			    (void) fclose(out_file);
276*7c478bd9Sstevel@tonic-gate 			free(fname);
277*7c478bd9Sstevel@tonic-gate 			return (0);
278*7c478bd9Sstevel@tonic-gate 		    }
279*7c478bd9Sstevel@tonic-gate 		    if (output_file_open == FALSE) {
280*7c478bd9Sstevel@tonic-gate 			next_file_name(fname);
281*7c478bd9Sstevel@tonic-gate 			if ((out_file = fopen(fname, "w")) == NULL) {
282*7c478bd9Sstevel@tonic-gate 			    (void) perror("split");
283*7c478bd9Sstevel@tonic-gate 			    free(fname);
284*7c478bd9Sstevel@tonic-gate 			    return (1);
285*7c478bd9Sstevel@tonic-gate 			}
286*7c478bd9Sstevel@tonic-gate 			output_file_open = TRUE;
287*7c478bd9Sstevel@tonic-gate 		    }
288*7c478bd9Sstevel@tonic-gate 		    if (putc(c, out_file) == EOF) {
289*7c478bd9Sstevel@tonic-gate 			perror("split");
290*7c478bd9Sstevel@tonic-gate 			if (output_file_open == TRUE)
291*7c478bd9Sstevel@tonic-gate 			    (void) fclose(out_file);
292*7c478bd9Sstevel@tonic-gate 			free(fname);
293*7c478bd9Sstevel@tonic-gate 			return (1);
294*7c478bd9Sstevel@tonic-gate 		    }
295*7c478bd9Sstevel@tonic-gate 		}
296*7c478bd9Sstevel@tonic-gate 	    } else {
297*7c478bd9Sstevel@tonic-gate 		for (out = 0; out < line_count; out++) {
298*7c478bd9Sstevel@tonic-gate 		    do {
299*7c478bd9Sstevel@tonic-gate 			errno = 0;
300*7c478bd9Sstevel@tonic-gate 			wc = getwc(in_file);
301*7c478bd9Sstevel@tonic-gate 			if (wc == WEOF) {
302*7c478bd9Sstevel@tonic-gate 			    if (errno != 0) {
303*7c478bd9Sstevel@tonic-gate 				if (errno == EILSEQ) {
304*7c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr, gettext(
305*7c478bd9Sstevel@tonic-gate 					"%s: Invalid multibyte sequence "
306*7c478bd9Sstevel@tonic-gate 					"encountered at file offset %lld, "
307*7c478bd9Sstevel@tonic-gate 					"aborting split\n"),
308*7c478bd9Sstevel@tonic-gate 					    progname, ftello(in_file));
309*7c478bd9Sstevel@tonic-gate 				} else {
310*7c478bd9Sstevel@tonic-gate 				    (void) perror("split");
311*7c478bd9Sstevel@tonic-gate 				}
312*7c478bd9Sstevel@tonic-gate 				if (output_file_open == TRUE)
313*7c478bd9Sstevel@tonic-gate 				    (void) fclose(out_file);
314*7c478bd9Sstevel@tonic-gate 				free(fname);
315*7c478bd9Sstevel@tonic-gate 				return (1);
316*7c478bd9Sstevel@tonic-gate 			    }
317*7c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
318*7c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
319*7c478bd9Sstevel@tonic-gate 			    free(fname);
320*7c478bd9Sstevel@tonic-gate 			    return (0);
321*7c478bd9Sstevel@tonic-gate 			}
322*7c478bd9Sstevel@tonic-gate 			if (output_file_open == FALSE) {
323*7c478bd9Sstevel@tonic-gate 			    next_file_name(fname);
324*7c478bd9Sstevel@tonic-gate 			    if ((out_file = fopen(fname, "w")) == NULL) {
325*7c478bd9Sstevel@tonic-gate 				(void) perror("split");
326*7c478bd9Sstevel@tonic-gate 				free(fname);
327*7c478bd9Sstevel@tonic-gate 				return (1);
328*7c478bd9Sstevel@tonic-gate 			    }
329*7c478bd9Sstevel@tonic-gate 			    output_file_open = TRUE;
330*7c478bd9Sstevel@tonic-gate 			}
331*7c478bd9Sstevel@tonic-gate 			if (putwc(wc, out_file) == WEOF) {
332*7c478bd9Sstevel@tonic-gate 			    (void) perror("split");
333*7c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
334*7c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
335*7c478bd9Sstevel@tonic-gate 			    free(fname);
336*7c478bd9Sstevel@tonic-gate 			    return (1);
337*7c478bd9Sstevel@tonic-gate 			}
338*7c478bd9Sstevel@tonic-gate 		    } while (wc != '\n');
339*7c478bd9Sstevel@tonic-gate 		}
340*7c478bd9Sstevel@tonic-gate 	    }
341*7c478bd9Sstevel@tonic-gate 	    if (output_file_open == TRUE)
342*7c478bd9Sstevel@tonic-gate 		(void) fclose(out_file);
343*7c478bd9Sstevel@tonic-gate 	}
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
346*7c478bd9Sstevel@tonic-gate }
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate static	void
350*7c478bd9Sstevel@tonic-gate next_file_name(char * name)
351*7c478bd9Sstevel@tonic-gate {
352*7c478bd9Sstevel@tonic-gate 	int	i;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	i = strlen(name) - 1;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	while (i >= (int)(strlen(name) - suffix_length)) {
357*7c478bd9Sstevel@tonic-gate 		if (++name[i] <= 'z')
358*7c478bd9Sstevel@tonic-gate 			return;
359*7c478bd9Sstevel@tonic-gate 		name[i--] = 'a';
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
362*7c478bd9Sstevel@tonic-gate 		"%s: Exhausted output file names, aborting split\n"),
363*7c478bd9Sstevel@tonic-gate 		progname);
364*7c478bd9Sstevel@tonic-gate 	exit(1);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate static	void
369*7c478bd9Sstevel@tonic-gate Usage()
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
372*7c478bd9Sstevel@tonic-gate 		"Usage: %s [-l #] [-a #] [file [name]]\n"
373*7c478bd9Sstevel@tonic-gate 		"       %s [-b #[k|m]] [-a #] [file [name]]\n"
374*7c478bd9Sstevel@tonic-gate 		"       %s [-#] [-a #] [file [name]]\n"),
375*7c478bd9Sstevel@tonic-gate 		progname, progname, progname);
376*7c478bd9Sstevel@tonic-gate 	exit(1);
377*7c478bd9Sstevel@tonic-gate }
378