xref: /titanic_44/usr/src/cmd/split/split.c (revision 4fce32e15d9c284a06aea2e40852f6674f03b63e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*4fce32e1Smuffin 
23*4fce32e1Smuffin /*
24*4fce32e1Smuffin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
25*4fce32e1Smuffin  * Use is subject to license terms.
26*4fce32e1Smuffin  */
27*4fce32e1Smuffin 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
31*4fce32e1Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <malloc.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <wchar.h>
427c478bd9Sstevel@tonic-gate #include <widec.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	DEFAULT_LINES	1000
487c478bd9Sstevel@tonic-gate #define	ONE_K		1024
497c478bd9Sstevel@tonic-gate #define	ONE_M		ONE_K*ONE_K
507c478bd9Sstevel@tonic-gate #ifndef	TRUE
517c478bd9Sstevel@tonic-gate #define	TRUE		1
527c478bd9Sstevel@tonic-gate #define	FALSE		0
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static	void	Usage();
577c478bd9Sstevel@tonic-gate static	void	next_file_name();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static	char	*progname;
617c478bd9Sstevel@tonic-gate static	int	suffix_length = 2;
627c478bd9Sstevel@tonic-gate 
63*4fce32e1Smuffin int
main(int argc,char ** argv)647c478bd9Sstevel@tonic-gate main(int argc, char **argv)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	long long	line_count = 0;
677c478bd9Sstevel@tonic-gate 	long long	byte_count = 0;
687c478bd9Sstevel@tonic-gate 	long long	out;
697c478bd9Sstevel@tonic-gate 	char	*fname = NULL;
707c478bd9Sstevel@tonic-gate 	char	head[MAXPATHLEN];
717c478bd9Sstevel@tonic-gate 	char	*output_file_name;
727c478bd9Sstevel@tonic-gate 	char	*tail;
737c478bd9Sstevel@tonic-gate 	char	*last;
747c478bd9Sstevel@tonic-gate 	FILE	*in_file = NULL;
757c478bd9Sstevel@tonic-gate 	FILE	*out_file = (FILE *)NULL;
767c478bd9Sstevel@tonic-gate 	int	i;
777c478bd9Sstevel@tonic-gate 	int	c;
787c478bd9Sstevel@tonic-gate 	wint_t	wc;
797c478bd9Sstevel@tonic-gate 	int	output_file_open;
807c478bd9Sstevel@tonic-gate 	struct	statvfs stbuf;
817c478bd9Sstevel@tonic-gate 	int	opt;
827c478bd9Sstevel@tonic-gate 	int	non_standard_line_count = FALSE;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
867c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
877c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	progname = argv[0];
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/* check for explicit stdin "-" option */
947c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
957c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "-") == 0) {
967c478bd9Sstevel@tonic-gate 			in_file = stdin;
977c478bd9Sstevel@tonic-gate 			while (i < argc) {
987c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 				/* a "-" before "--" is an error */
1017c478bd9Sstevel@tonic-gate 				if ((argv[i] != NULL) &&
1027c478bd9Sstevel@tonic-gate 				    (strcmp(argv[i], "--") == 0)) {
1037c478bd9Sstevel@tonic-gate 					Usage();
1047c478bd9Sstevel@tonic-gate 				}
1057c478bd9Sstevel@tonic-gate 				i++;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 			argc--;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* check for non-standard "-line-count" option */
1127c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1137c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
1177c478bd9Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
1187c478bd9Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
1197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1207c478bd9Sstevel@tonic-gate 				    "%s: Badly formed number\n"), progname);
1217c478bd9Sstevel@tonic-gate 				Usage();
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 			line_count = (long long) strtoll(&argv[i][1],
1257c478bd9Sstevel@tonic-gate 			    (char **)NULL, 10);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 			non_standard_line_count = TRUE;
1287c478bd9Sstevel@tonic-gate 			while (i < argc) {
1297c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
1307c478bd9Sstevel@tonic-gate 				i++;
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 			argc--;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/* get options */
1377c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "a:b:l:")) != EOF) {
1387c478bd9Sstevel@tonic-gate 		switch (opt) {
1397c478bd9Sstevel@tonic-gate 		case 'a':
1407c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1417c478bd9Sstevel@tonic-gate 				Usage();
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 			suffix_length = (long long) strtoll(optarg,
1447c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
1457c478bd9Sstevel@tonic-gate 			if (suffix_length <= 0) {
1467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1477c478bd9Sstevel@tonic-gate 				    "%s: Invalid \"-a %s\" option\n"),
1487c478bd9Sstevel@tonic-gate 				    progname, optarg);
1497c478bd9Sstevel@tonic-gate 				Usage();
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case 'b':
1547c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1557c478bd9Sstevel@tonic-gate 				Usage();
1567c478bd9Sstevel@tonic-gate 			}
1577c478bd9Sstevel@tonic-gate 			byte_count = (long long) strtoll(optarg,
1587c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
1597c478bd9Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'k')
1607c478bd9Sstevel@tonic-gate 				byte_count *= ONE_K;
1617c478bd9Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'm')
1627c478bd9Sstevel@tonic-gate 				byte_count *= ONE_M;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		case 'l':
1667c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1677c478bd9Sstevel@tonic-gate 				Usage();
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			if (non_standard_line_count == TRUE) {
1707c478bd9Sstevel@tonic-gate 				Usage();
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			line_count = (long long) strtoll(optarg,
1737c478bd9Sstevel@tonic-gate 					(char **)NULL, 10);
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		default:
1777c478bd9Sstevel@tonic-gate 			Usage();
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* get input file */
1827c478bd9Sstevel@tonic-gate 	if ((in_file == NULL) && (optind < argc)) {
1837c478bd9Sstevel@tonic-gate 		if ((in_file = fopen(argv[optind++], "r")) == NULL) {
1847c478bd9Sstevel@tonic-gate 			(void) perror("split");
1857c478bd9Sstevel@tonic-gate 			return (1);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	if (in_file == NULL) {
1897c478bd9Sstevel@tonic-gate 		in_file = stdin;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* get output file name */
1937c478bd9Sstevel@tonic-gate 	if (optind < argc) {
1947c478bd9Sstevel@tonic-gate 		output_file_name = argv[optind];
1957c478bd9Sstevel@tonic-gate 		if ((tail = strrchr(output_file_name, '/')) == NULL) {
1967c478bd9Sstevel@tonic-gate 			tail = output_file_name;
1977c478bd9Sstevel@tonic-gate 			(void) getcwd(head, sizeof (head));
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			tail++;
2007c478bd9Sstevel@tonic-gate 			(void) strcpy(head, output_file_name);
2017c478bd9Sstevel@tonic-gate 			last = strrchr(head, '/');
2027c478bd9Sstevel@tonic-gate 			*++last = '\0';
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		if (statvfs(head, &stbuf) < 0) {
2067c478bd9Sstevel@tonic-gate 			perror(head);
2077c478bd9Sstevel@tonic-gate 			return (1);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (strlen(tail) > (stbuf.f_namemax - suffix_length)) {
2117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2127c478bd9Sstevel@tonic-gate 			    "%s: More than %d characters in file name\n"),
2137c478bd9Sstevel@tonic-gate 			    progname, stbuf.f_namemax - suffix_length);
2147c478bd9Sstevel@tonic-gate 			Usage();
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 	} else
2177c478bd9Sstevel@tonic-gate 		output_file_name = "x";
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* check options */
2207c478bd9Sstevel@tonic-gate 	if (((int)strlen(output_file_name) + suffix_length) > FILENAME_MAX) {
2217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2227c478bd9Sstevel@tonic-gate 		"%s: Output file name too long\n"), progname);
2237c478bd9Sstevel@tonic-gate 		return (1);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (line_count && byte_count) {
2277c478bd9Sstevel@tonic-gate 		    Usage();
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* use default line count if none specified */
2317c478bd9Sstevel@tonic-gate 	if (line_count == 0) {
2327c478bd9Sstevel@tonic-gate 		line_count = DEFAULT_LINES;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * allocate buffer for the filenames we'll construct; it must be
2377c478bd9Sstevel@tonic-gate 	 * big enough to hold the name in 'output_file_name' + an n-char
2387c478bd9Sstevel@tonic-gate 	 * suffix + NULL terminator
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	if ((fname = (char *)malloc(strlen(output_file_name) +
2417c478bd9Sstevel@tonic-gate 	    suffix_length + 1)) == NULL) {
2427c478bd9Sstevel@tonic-gate 		(void) perror("split");
2437c478bd9Sstevel@tonic-gate 		return (1);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* build first output file name */
2477c478bd9Sstevel@tonic-gate 	for (i = 0; output_file_name[i]; i++) {
2487c478bd9Sstevel@tonic-gate 		fname[i] = output_file_name[i];
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	while (i < (int)strlen(output_file_name) + suffix_length) {
2517c478bd9Sstevel@tonic-gate 		fname[i++] = 'a';
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	if (suffix_length)
2547c478bd9Sstevel@tonic-gate 		fname[i - 1] = 'a' - 1;
2557c478bd9Sstevel@tonic-gate 	fname[i] = '\0';
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	for (; ; ) {
2587c478bd9Sstevel@tonic-gate 	    output_file_open = FALSE;
2597c478bd9Sstevel@tonic-gate 	    if (byte_count) {
2607c478bd9Sstevel@tonic-gate 		for (out = 0; out < byte_count; out++) {
2617c478bd9Sstevel@tonic-gate 		    errno = 0;
2627c478bd9Sstevel@tonic-gate 		    c = getc(in_file);
2637c478bd9Sstevel@tonic-gate 		    if (c == EOF) {
2647c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2657c478bd9Sstevel@tonic-gate 			    int lerrno = errno;
2667c478bd9Sstevel@tonic-gate 			    (void) fprintf(stderr, gettext(
2677c478bd9Sstevel@tonic-gate 				"%s: Read error at file offset %lld: %s, "
2687c478bd9Sstevel@tonic-gate 				"aborting split\n"),
2697c478bd9Sstevel@tonic-gate 				    progname, ftello(in_file),
2707c478bd9Sstevel@tonic-gate 				    strerror(lerrno));
2717c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
2727c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
2737c478bd9Sstevel@tonic-gate 			    free(fname);
2747c478bd9Sstevel@tonic-gate 			    return (1);
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 			if (output_file_open == TRUE)
2777c478bd9Sstevel@tonic-gate 			    (void) fclose(out_file);
2787c478bd9Sstevel@tonic-gate 			free(fname);
2797c478bd9Sstevel@tonic-gate 			return (0);
2807c478bd9Sstevel@tonic-gate 		    }
2817c478bd9Sstevel@tonic-gate 		    if (output_file_open == FALSE) {
2827c478bd9Sstevel@tonic-gate 			next_file_name(fname);
2837c478bd9Sstevel@tonic-gate 			if ((out_file = fopen(fname, "w")) == NULL) {
2847c478bd9Sstevel@tonic-gate 			    (void) perror("split");
2857c478bd9Sstevel@tonic-gate 			    free(fname);
2867c478bd9Sstevel@tonic-gate 			    return (1);
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 			output_file_open = TRUE;
2897c478bd9Sstevel@tonic-gate 		    }
2907c478bd9Sstevel@tonic-gate 		    if (putc(c, out_file) == EOF) {
2917c478bd9Sstevel@tonic-gate 			perror("split");
2927c478bd9Sstevel@tonic-gate 			if (output_file_open == TRUE)
2937c478bd9Sstevel@tonic-gate 			    (void) fclose(out_file);
2947c478bd9Sstevel@tonic-gate 			free(fname);
2957c478bd9Sstevel@tonic-gate 			return (1);
2967c478bd9Sstevel@tonic-gate 		    }
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	    } else {
2997c478bd9Sstevel@tonic-gate 		for (out = 0; out < line_count; out++) {
3007c478bd9Sstevel@tonic-gate 		    do {
3017c478bd9Sstevel@tonic-gate 			errno = 0;
3027c478bd9Sstevel@tonic-gate 			wc = getwc(in_file);
3037c478bd9Sstevel@tonic-gate 			if (wc == WEOF) {
3047c478bd9Sstevel@tonic-gate 			    if (errno != 0) {
3057c478bd9Sstevel@tonic-gate 				if (errno == EILSEQ) {
3067c478bd9Sstevel@tonic-gate 				    (void) fprintf(stderr, gettext(
3077c478bd9Sstevel@tonic-gate 					"%s: Invalid multibyte sequence "
3087c478bd9Sstevel@tonic-gate 					"encountered at file offset %lld, "
3097c478bd9Sstevel@tonic-gate 					"aborting split\n"),
3107c478bd9Sstevel@tonic-gate 					    progname, ftello(in_file));
3117c478bd9Sstevel@tonic-gate 				} else {
3127c478bd9Sstevel@tonic-gate 				    (void) perror("split");
3137c478bd9Sstevel@tonic-gate 				}
3147c478bd9Sstevel@tonic-gate 				if (output_file_open == TRUE)
3157c478bd9Sstevel@tonic-gate 				    (void) fclose(out_file);
3167c478bd9Sstevel@tonic-gate 				free(fname);
3177c478bd9Sstevel@tonic-gate 				return (1);
3187c478bd9Sstevel@tonic-gate 			    }
3197c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
3207c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
3217c478bd9Sstevel@tonic-gate 			    free(fname);
3227c478bd9Sstevel@tonic-gate 			    return (0);
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			if (output_file_open == FALSE) {
3257c478bd9Sstevel@tonic-gate 			    next_file_name(fname);
3267c478bd9Sstevel@tonic-gate 			    if ((out_file = fopen(fname, "w")) == NULL) {
3277c478bd9Sstevel@tonic-gate 				(void) perror("split");
3287c478bd9Sstevel@tonic-gate 				free(fname);
3297c478bd9Sstevel@tonic-gate 				return (1);
3307c478bd9Sstevel@tonic-gate 			    }
3317c478bd9Sstevel@tonic-gate 			    output_file_open = TRUE;
3327c478bd9Sstevel@tonic-gate 			}
3337c478bd9Sstevel@tonic-gate 			if (putwc(wc, out_file) == WEOF) {
3347c478bd9Sstevel@tonic-gate 			    (void) perror("split");
3357c478bd9Sstevel@tonic-gate 			    if (output_file_open == TRUE)
3367c478bd9Sstevel@tonic-gate 				(void) fclose(out_file);
3377c478bd9Sstevel@tonic-gate 			    free(fname);
3387c478bd9Sstevel@tonic-gate 			    return (1);
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 		    } while (wc != '\n');
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	    }
3437c478bd9Sstevel@tonic-gate 	    if (output_file_open == TRUE)
3447c478bd9Sstevel@tonic-gate 		(void) fclose(out_file);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static	void
next_file_name(char * name)3527c478bd9Sstevel@tonic-gate next_file_name(char *name)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	int	i;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	i = strlen(name) - 1;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	while (i >= (int)(strlen(name) - suffix_length)) {
3597c478bd9Sstevel@tonic-gate 		if (++name[i] <= 'z')
3607c478bd9Sstevel@tonic-gate 			return;
3617c478bd9Sstevel@tonic-gate 		name[i--] = 'a';
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3647c478bd9Sstevel@tonic-gate 		"%s: Exhausted output file names, aborting split\n"),
3657c478bd9Sstevel@tonic-gate 		progname);
3667c478bd9Sstevel@tonic-gate 	exit(1);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static	void
Usage()3717c478bd9Sstevel@tonic-gate Usage()
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3747c478bd9Sstevel@tonic-gate 		"Usage: %s [-l #] [-a #] [file [name]]\n"
3757c478bd9Sstevel@tonic-gate 		"       %s [-b #[k|m]] [-a #] [file [name]]\n"
3767c478bd9Sstevel@tonic-gate 		"       %s [-#] [-a #] [file [name]]\n"),
3777c478bd9Sstevel@tonic-gate 		progname, progname, progname);
3787c478bd9Sstevel@tonic-gate 	exit(1);
3797c478bd9Sstevel@tonic-gate }
380