xref: /onnv-gate/usr/src/cmd/split/split.c (revision 213:552475a8865e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*213Smuffin 
23*213Smuffin /*
24*213Smuffin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
25*213Smuffin  * Use is subject to license terms.
26*213Smuffin  */
27*213Smuffin 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved	*/
300Sstevel@tonic-gate 
31*213Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/statvfs.h>
360Sstevel@tonic-gate #include <locale.h>
370Sstevel@tonic-gate #include <malloc.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <wchar.h>
420Sstevel@tonic-gate #include <widec.h>
430Sstevel@tonic-gate #include <ctype.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	DEFAULT_LINES	1000
480Sstevel@tonic-gate #define	ONE_K		1024
490Sstevel@tonic-gate #define	ONE_M		ONE_K*ONE_K
500Sstevel@tonic-gate #ifndef	TRUE
510Sstevel@tonic-gate #define	TRUE		1
520Sstevel@tonic-gate #define	FALSE		0
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static	void	Usage();
570Sstevel@tonic-gate static	void	next_file_name();
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static	char	*progname;
610Sstevel@tonic-gate static	int	suffix_length = 2;
620Sstevel@tonic-gate 
63*213Smuffin int
main(int argc,char ** argv)640Sstevel@tonic-gate main(int argc, char **argv)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	long long	line_count = 0;
670Sstevel@tonic-gate 	long long	byte_count = 0;
680Sstevel@tonic-gate 	long long	out;
690Sstevel@tonic-gate 	char	*fname = NULL;
700Sstevel@tonic-gate 	char	head[MAXPATHLEN];
710Sstevel@tonic-gate 	char	*output_file_name;
720Sstevel@tonic-gate 	char	*tail;
730Sstevel@tonic-gate 	char	*last;
740Sstevel@tonic-gate 	FILE	*in_file = NULL;
750Sstevel@tonic-gate 	FILE	*out_file = (FILE *)NULL;
760Sstevel@tonic-gate 	int	i;
770Sstevel@tonic-gate 	int	c;
780Sstevel@tonic-gate 	wint_t	wc;
790Sstevel@tonic-gate 	int	output_file_open;
800Sstevel@tonic-gate 	struct	statvfs stbuf;
810Sstevel@tonic-gate 	int	opt;
820Sstevel@tonic-gate 	int	non_standard_line_count = FALSE;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
860Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
870Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	progname = argv[0];
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* check for explicit stdin "-" option */
940Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
950Sstevel@tonic-gate 		if (strcmp(argv[i], "-") == 0) {
960Sstevel@tonic-gate 			in_file = stdin;
970Sstevel@tonic-gate 			while (i < argc) {
980Sstevel@tonic-gate 				argv[i] = argv[i + 1];
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 				/* a "-" before "--" is an error */
1010Sstevel@tonic-gate 				if ((argv[i] != NULL) &&
1020Sstevel@tonic-gate 				    (strcmp(argv[i], "--") == 0)) {
1030Sstevel@tonic-gate 					Usage();
1040Sstevel@tonic-gate 				}
1050Sstevel@tonic-gate 				i++;
1060Sstevel@tonic-gate 			}
1070Sstevel@tonic-gate 			argc--;
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/* check for non-standard "-line-count" option */
1120Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1130Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
1170Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
1180Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
1190Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1200Sstevel@tonic-gate 				    "%s: Badly formed number\n"), progname);
1210Sstevel@tonic-gate 				Usage();
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 			line_count = (long long) strtoll(&argv[i][1],
1250Sstevel@tonic-gate 			    (char **)NULL, 10);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 			non_standard_line_count = TRUE;
1280Sstevel@tonic-gate 			while (i < argc) {
1290Sstevel@tonic-gate 				argv[i] = argv[i + 1];
1300Sstevel@tonic-gate 				i++;
1310Sstevel@tonic-gate 			}
1320Sstevel@tonic-gate 			argc--;
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/* get options */
1370Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "a:b:l:")) != EOF) {
1380Sstevel@tonic-gate 		switch (opt) {
1390Sstevel@tonic-gate 		case 'a':
1400Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1410Sstevel@tonic-gate 				Usage();
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 			suffix_length = (long long) strtoll(optarg,
1440Sstevel@tonic-gate 					(char **)NULL, 10);
1450Sstevel@tonic-gate 			if (suffix_length <= 0) {
1460Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1470Sstevel@tonic-gate 				    "%s: Invalid \"-a %s\" option\n"),
1480Sstevel@tonic-gate 				    progname, optarg);
1490Sstevel@tonic-gate 				Usage();
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 		case 'b':
1540Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1550Sstevel@tonic-gate 				Usage();
1560Sstevel@tonic-gate 			}
1570Sstevel@tonic-gate 			byte_count = (long long) strtoll(optarg,
1580Sstevel@tonic-gate 					(char **)NULL, 10);
1590Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'k')
1600Sstevel@tonic-gate 				byte_count *= ONE_K;
1610Sstevel@tonic-gate 			if (*(optarg + strspn(optarg, "0123456789")) == 'm')
1620Sstevel@tonic-gate 				byte_count *= ONE_M;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		case 'l':
1660Sstevel@tonic-gate 			if (strcmp(optarg, "--") == 0) {
1670Sstevel@tonic-gate 				Usage();
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 			if (non_standard_line_count == TRUE) {
1700Sstevel@tonic-gate 				Usage();
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			line_count = (long long) strtoll(optarg,
1730Sstevel@tonic-gate 					(char **)NULL, 10);
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		default:
1770Sstevel@tonic-gate 			Usage();
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/* get input file */
1820Sstevel@tonic-gate 	if ((in_file == NULL) && (optind < argc)) {
1830Sstevel@tonic-gate 		if ((in_file = fopen(argv[optind++], "r")) == NULL) {
1840Sstevel@tonic-gate 			(void) perror("split");
1850Sstevel@tonic-gate 			return (1);
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	if (in_file == NULL) {
1890Sstevel@tonic-gate 		in_file = stdin;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/* get output file name */
1930Sstevel@tonic-gate 	if (optind < argc) {
1940Sstevel@tonic-gate 		output_file_name = argv[optind];
1950Sstevel@tonic-gate 		if ((tail = strrchr(output_file_name, '/')) == NULL) {
1960Sstevel@tonic-gate 			tail = output_file_name;
1970Sstevel@tonic-gate 			(void) getcwd(head, sizeof (head));
1980Sstevel@tonic-gate 		} else {
1990Sstevel@tonic-gate 			tail++;
2000Sstevel@tonic-gate 			(void) strcpy(head, output_file_name);
2010Sstevel@tonic-gate 			last = strrchr(head, '/');
2020Sstevel@tonic-gate 			*++last = '\0';
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		if (statvfs(head, &stbuf) < 0) {
2060Sstevel@tonic-gate 			perror(head);
2070Sstevel@tonic-gate 			return (1);
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		if (strlen(tail) > (stbuf.f_namemax - suffix_length)) {
2110Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2120Sstevel@tonic-gate 			    "%s: More than %d characters in file name\n"),
2130Sstevel@tonic-gate 			    progname, stbuf.f_namemax - suffix_length);
2140Sstevel@tonic-gate 			Usage();
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	} else
2170Sstevel@tonic-gate 		output_file_name = "x";
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/* check options */
220*213Smuffin 	if (((int)strlen(output_file_name) + suffix_length) > FILENAME_MAX) {
2210Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2220Sstevel@tonic-gate 		"%s: Output file name too long\n"), progname);
2230Sstevel@tonic-gate 		return (1);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (line_count && byte_count) {
2270Sstevel@tonic-gate 		    Usage();
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/* use default line count if none specified */
2310Sstevel@tonic-gate 	if (line_count == 0) {
2320Sstevel@tonic-gate 		line_count = DEFAULT_LINES;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * allocate buffer for the filenames we'll construct; it must be
2370Sstevel@tonic-gate 	 * big enough to hold the name in 'output_file_name' + an n-char
2380Sstevel@tonic-gate 	 * suffix + NULL terminator
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 	if ((fname = (char *)malloc(strlen(output_file_name) +
2410Sstevel@tonic-gate 	    suffix_length + 1)) == NULL) {
2420Sstevel@tonic-gate 		(void) perror("split");
2430Sstevel@tonic-gate 		return (1);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* build first output file name */
2470Sstevel@tonic-gate 	for (i = 0; output_file_name[i]; i++) {
2480Sstevel@tonic-gate 		fname[i] = output_file_name[i];
2490Sstevel@tonic-gate 	}
250*213Smuffin 	while (i < (int)strlen(output_file_name) + suffix_length) {
2510Sstevel@tonic-gate 		fname[i++] = 'a';
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	if (suffix_length)
2540Sstevel@tonic-gate 		fname[i - 1] = 'a' - 1;
2550Sstevel@tonic-gate 	fname[i] = '\0';
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	for (; ; ) {
2580Sstevel@tonic-gate 	    output_file_open = FALSE;
2590Sstevel@tonic-gate 	    if (byte_count) {
2600Sstevel@tonic-gate 		for (out = 0; out < byte_count; out++) {
2610Sstevel@tonic-gate 		    errno = 0;
2620Sstevel@tonic-gate 		    c = getc(in_file);
2630Sstevel@tonic-gate 		    if (c == EOF) {
2640Sstevel@tonic-gate 			if (errno != 0) {
2650Sstevel@tonic-gate 			    int lerrno = errno;
2660Sstevel@tonic-gate 			    (void) fprintf(stderr, gettext(
2670Sstevel@tonic-gate 				"%s: Read error at file offset %lld: %s, "
2680Sstevel@tonic-gate 				"aborting split\n"),
2690Sstevel@tonic-gate 				    progname, ftello(in_file),
2700Sstevel@tonic-gate 				    strerror(lerrno));
2710Sstevel@tonic-gate 			    if (output_file_open == TRUE)
2720Sstevel@tonic-gate 				(void) fclose(out_file);
2730Sstevel@tonic-gate 			    free(fname);
2740Sstevel@tonic-gate 			    return (1);
2750Sstevel@tonic-gate 			}
2760Sstevel@tonic-gate 			if (output_file_open == TRUE)
2770Sstevel@tonic-gate 			    (void) fclose(out_file);
2780Sstevel@tonic-gate 			free(fname);
2790Sstevel@tonic-gate 			return (0);
2800Sstevel@tonic-gate 		    }
2810Sstevel@tonic-gate 		    if (output_file_open == FALSE) {
2820Sstevel@tonic-gate 			next_file_name(fname);
2830Sstevel@tonic-gate 			if ((out_file = fopen(fname, "w")) == NULL) {
2840Sstevel@tonic-gate 			    (void) perror("split");
2850Sstevel@tonic-gate 			    free(fname);
2860Sstevel@tonic-gate 			    return (1);
2870Sstevel@tonic-gate 			}
2880Sstevel@tonic-gate 			output_file_open = TRUE;
2890Sstevel@tonic-gate 		    }
2900Sstevel@tonic-gate 		    if (putc(c, out_file) == EOF) {
2910Sstevel@tonic-gate 			perror("split");
2920Sstevel@tonic-gate 			if (output_file_open == TRUE)
2930Sstevel@tonic-gate 			    (void) fclose(out_file);
2940Sstevel@tonic-gate 			free(fname);
2950Sstevel@tonic-gate 			return (1);
2960Sstevel@tonic-gate 		    }
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 	    } else {
2990Sstevel@tonic-gate 		for (out = 0; out < line_count; out++) {
3000Sstevel@tonic-gate 		    do {
3010Sstevel@tonic-gate 			errno = 0;
3020Sstevel@tonic-gate 			wc = getwc(in_file);
3030Sstevel@tonic-gate 			if (wc == WEOF) {
3040Sstevel@tonic-gate 			    if (errno != 0) {
3050Sstevel@tonic-gate 				if (errno == EILSEQ) {
3060Sstevel@tonic-gate 				    (void) fprintf(stderr, gettext(
3070Sstevel@tonic-gate 					"%s: Invalid multibyte sequence "
3080Sstevel@tonic-gate 					"encountered at file offset %lld, "
3090Sstevel@tonic-gate 					"aborting split\n"),
3100Sstevel@tonic-gate 					    progname, ftello(in_file));
3110Sstevel@tonic-gate 				} else {
3120Sstevel@tonic-gate 				    (void) perror("split");
3130Sstevel@tonic-gate 				}
3140Sstevel@tonic-gate 				if (output_file_open == TRUE)
3150Sstevel@tonic-gate 				    (void) fclose(out_file);
3160Sstevel@tonic-gate 				free(fname);
3170Sstevel@tonic-gate 				return (1);
3180Sstevel@tonic-gate 			    }
3190Sstevel@tonic-gate 			    if (output_file_open == TRUE)
3200Sstevel@tonic-gate 				(void) fclose(out_file);
3210Sstevel@tonic-gate 			    free(fname);
3220Sstevel@tonic-gate 			    return (0);
3230Sstevel@tonic-gate 			}
3240Sstevel@tonic-gate 			if (output_file_open == FALSE) {
3250Sstevel@tonic-gate 			    next_file_name(fname);
3260Sstevel@tonic-gate 			    if ((out_file = fopen(fname, "w")) == NULL) {
3270Sstevel@tonic-gate 				(void) perror("split");
3280Sstevel@tonic-gate 				free(fname);
3290Sstevel@tonic-gate 				return (1);
3300Sstevel@tonic-gate 			    }
3310Sstevel@tonic-gate 			    output_file_open = TRUE;
3320Sstevel@tonic-gate 			}
3330Sstevel@tonic-gate 			if (putwc(wc, out_file) == WEOF) {
3340Sstevel@tonic-gate 			    (void) perror("split");
3350Sstevel@tonic-gate 			    if (output_file_open == TRUE)
3360Sstevel@tonic-gate 				(void) fclose(out_file);
3370Sstevel@tonic-gate 			    free(fname);
3380Sstevel@tonic-gate 			    return (1);
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 		    } while (wc != '\n');
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate 	    }
3430Sstevel@tonic-gate 	    if (output_file_open == TRUE)
3440Sstevel@tonic-gate 		(void) fclose(out_file);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*NOTREACHED*/
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate static	void
next_file_name(char * name)352*213Smuffin next_file_name(char *name)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	int	i;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	i = strlen(name) - 1;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	while (i >= (int)(strlen(name) - suffix_length)) {
3590Sstevel@tonic-gate 		if (++name[i] <= 'z')
3600Sstevel@tonic-gate 			return;
3610Sstevel@tonic-gate 		name[i--] = 'a';
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3640Sstevel@tonic-gate 		"%s: Exhausted output file names, aborting split\n"),
3650Sstevel@tonic-gate 		progname);
3660Sstevel@tonic-gate 	exit(1);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate static	void
Usage()3710Sstevel@tonic-gate Usage()
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3740Sstevel@tonic-gate 		"Usage: %s [-l #] [-a #] [file [name]]\n"
3750Sstevel@tonic-gate 		"       %s [-b #[k|m]] [-a #] [file [name]]\n"
3760Sstevel@tonic-gate 		"       %s [-#] [-a #] [file [name]]\n"),
3770Sstevel@tonic-gate 		progname, progname, progname);
3780Sstevel@tonic-gate 	exit(1);
3790Sstevel@tonic-gate }
380