xref: /freebsd-13.1/usr.bin/split/split.c (revision bcd34af4)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1987, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
32f1d37c20SMark Murray #include <sys/cdefs.h>
33f1d37c20SMark Murray __FBSDID("$FreeBSD$");
34f1d37c20SMark Murray 
359b50d902SRodney W. Grimes #ifndef lint
36c002b33bSPhilippe Charnier static const char copyright[] =
379b50d902SRodney W. Grimes "@(#) Copyright (c) 1987, 1993, 1994\n\
389b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
39f1d37c20SMark Murray #endif
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #ifndef lint
42f1d37c20SMark Murray static const char sccsid[] = "@(#)split.c	8.2 (Berkeley) 4/16/94";
43c002b33bSPhilippe Charnier #endif
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/param.h>
460e286f08SDavid Schultz #include <sys/types.h>
470e286f08SDavid Schultz #include <sys/stat.h>
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <ctype.h>
509b50d902SRodney W. Grimes #include <err.h>
51c81180b8STim J. Robbins #include <errno.h>
529b50d902SRodney W. Grimes #include <fcntl.h>
53c81180b8STim J. Robbins #include <inttypes.h>
543ebd4af7SEitan Adler #include <libutil.h>
55c81180b8STim J. Robbins #include <limits.h>
560742d4edSTim J. Robbins #include <locale.h>
577f418e34SEitan Adler #include <stdbool.h>
58c81180b8STim J. Robbins #include <stdint.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <stdlib.h>
619b50d902SRodney W. Grimes #include <string.h>
629b50d902SRodney W. Grimes #include <unistd.h>
632fa6610fSArchie Cobbs #include <regex.h>
642fa6610fSArchie Cobbs #include <sysexits.h>
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes #define DEFLINE	1000			/* Default num lines per file. */
679b50d902SRodney W. Grimes 
68973aa6bcSEd Schouten static off_t	 bytecnt;		/* Byte count to split on. */
69973aa6bcSEd Schouten static off_t	 chunks = 0;		/* Chunks count to split into. */
70973aa6bcSEd Schouten static long	 numlines;		/* Line count to split on. */
71973aa6bcSEd Schouten static int	 file_open;		/* If a file open. */
72973aa6bcSEd Schouten static int	 ifd = -1, ofd = -1;	/* Input/output file descriptors. */
73973aa6bcSEd Schouten static char	 bfr[MAXBSIZE];		/* I/O buffer. */
74973aa6bcSEd Schouten static char	 fname[MAXPATHLEN];	/* File name prefix. */
75973aa6bcSEd Schouten static regex_t	 rgx;
76973aa6bcSEd Schouten static int	 pflag;
777f418e34SEitan Adler static bool	 dflag;
78973aa6bcSEd Schouten static long	 sufflen = 2;		/* File name suffix length. */
799b50d902SRodney W. Grimes 
800e286f08SDavid Schultz static void newfile(void);
810e286f08SDavid Schultz static void split1(void);
820e286f08SDavid Schultz static void split2(void);
830e286f08SDavid Schultz static void split3(void);
843f330d7dSWarner Losh static void usage(void);
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes int
main(int argc,char ** argv)872c69ee9bSJuli Mallett main(int argc, char **argv)
889b50d902SRodney W. Grimes {
899b50d902SRodney W. Grimes 	int ch;
903ebd4af7SEitan Adler 	int error;
919b50d902SRodney W. Grimes 	char *ep, *p;
929b50d902SRodney W. Grimes 
930742d4edSTim J. Robbins 	setlocale(LC_ALL, "");
940742d4edSTim J. Robbins 
957f418e34SEitan Adler 	dflag = false;
967f418e34SEitan Adler 	while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1)
979b50d902SRodney W. Grimes 		switch (ch) {
989b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3': case '4':
999b50d902SRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
1009b50d902SRodney W. Grimes 			/*
1019b50d902SRodney W. Grimes 			 * Undocumented kludge: split was originally designed
1029b50d902SRodney W. Grimes 			 * to take a number after a dash.
1039b50d902SRodney W. Grimes 			 */
1049b50d902SRodney W. Grimes 			if (numlines == 0) {
1059b50d902SRodney W. Grimes 				p = argv[optind - 1];
1069b50d902SRodney W. Grimes 				if (p[0] == '-' && p[1] == ch && !p[2])
1079b50d902SRodney W. Grimes 					numlines = strtol(++p, &ep, 10);
1089b50d902SRodney W. Grimes 				else
1099b50d902SRodney W. Grimes 					numlines =
1109b50d902SRodney W. Grimes 					    strtol(argv[optind] + 1, &ep, 10);
1119b50d902SRodney W. Grimes 				if (numlines <= 0 || *ep)
1122fa6610fSArchie Cobbs 					errx(EX_USAGE,
1132fa6610fSArchie Cobbs 					    "%s: illegal line count", optarg);
1149b50d902SRodney W. Grimes 			}
1159b50d902SRodney W. Grimes 			break;
11641850495SMike Barcroft 		case 'a':		/* Suffix length */
11741850495SMike Barcroft 			if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
11841850495SMike Barcroft 				errx(EX_USAGE,
11941850495SMike Barcroft 				    "%s: illegal suffix length", optarg);
12041850495SMike Barcroft 			break;
1219b50d902SRodney W. Grimes 		case 'b':		/* Byte count. */
122c81180b8STim J. Robbins 			errno = 0;
1233ebd4af7SEitan Adler 			error = expand_number(optarg, &bytecnt);
1243ebd4af7SEitan Adler 			if (error == -1)
125c81180b8STim J. Robbins 				errx(EX_USAGE, "%s: offset too large", optarg);
1269b50d902SRodney W. Grimes 			break;
1277f418e34SEitan Adler 		case 'd':		/* Decimal suffix */
1287f418e34SEitan Adler 			dflag = true;
1297f418e34SEitan Adler 			break;
1309b50d902SRodney W. Grimes 		case 'l':		/* Line count. */
1319b50d902SRodney W. Grimes 			if (numlines != 0)
1329b50d902SRodney W. Grimes 				usage();
133a3f8d23aSRodney W. Grimes 			if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
1342fa6610fSArchie Cobbs 				errx(EX_USAGE,
1352fa6610fSArchie Cobbs 				    "%s: illegal line count", optarg);
1369b50d902SRodney W. Grimes 			break;
1370e286f08SDavid Schultz 		case 'n':		/* Chunks. */
1380e286f08SDavid Schultz 			if (!isdigit((unsigned char)optarg[0]) ||
1390e286f08SDavid Schultz 			    (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
1400e286f08SDavid Schultz 			    *ep != '\0') {
1410e286f08SDavid Schultz 				errx(EX_USAGE, "%s: illegal number of chunks",
1420e286f08SDavid Schultz 				     optarg);
1430e286f08SDavid Schultz 			}
1440e286f08SDavid Schultz 			break;
1450e286f08SDavid Schultz 
14649198c42SGiorgos Keramidas 		case 'p':		/* pattern matching. */
14749198c42SGiorgos Keramidas 			if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
14849198c42SGiorgos Keramidas 				errx(EX_USAGE, "%s: illegal regexp", optarg);
14949198c42SGiorgos Keramidas 			pflag = 1;
15049198c42SGiorgos Keramidas 			break;
1519b50d902SRodney W. Grimes 		default:
1529b50d902SRodney W. Grimes 			usage();
1539b50d902SRodney W. Grimes 		}
1549b50d902SRodney W. Grimes 	argv += optind;
1559b50d902SRodney W. Grimes 	argc -= optind;
1569b50d902SRodney W. Grimes 
157bb78dba4STim J. Robbins 	if (*argv != NULL) {			/* Input file. */
1583e4228c3STim J. Robbins 		if (strcmp(*argv, "-") == 0)
1593e4228c3STim J. Robbins 			ifd = STDIN_FILENO;
1603e4228c3STim J. Robbins 		else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
1612fa6610fSArchie Cobbs 			err(EX_NOINPUT, "%s", *argv);
1629b50d902SRodney W. Grimes 		++argv;
1639b50d902SRodney W. Grimes 	}
1649b50d902SRodney W. Grimes 	if (*argv != NULL)			/* File name prefix. */
1653f5869d0STim J. Robbins 		if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
1663f5869d0STim J. Robbins 			errx(EX_USAGE, "file name prefix is too long");
1679b50d902SRodney W. Grimes 	if (*argv != NULL)
1689b50d902SRodney W. Grimes 		usage();
1699b50d902SRodney W. Grimes 
17041850495SMike Barcroft 	if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
17141850495SMike Barcroft 		errx(EX_USAGE, "suffix is too long");
1720e286f08SDavid Schultz 	if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
1732fa6610fSArchie Cobbs 		usage();
1742fa6610fSArchie Cobbs 
1759b50d902SRodney W. Grimes 	if (numlines == 0)
1769b50d902SRodney W. Grimes 		numlines = DEFLINE;
1770e286f08SDavid Schultz 	else if (bytecnt != 0 || chunks != 0)
1780e286f08SDavid Schultz 		usage();
1790e286f08SDavid Schultz 
1800e286f08SDavid Schultz 	if (bytecnt && chunks)
1819b50d902SRodney W. Grimes 		usage();
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes 	if (ifd == -1)				/* Stdin by default. */
1849b50d902SRodney W. Grimes 		ifd = 0;
1859b50d902SRodney W. Grimes 
1869b50d902SRodney W. Grimes 	if (bytecnt) {
1879b50d902SRodney W. Grimes 		split1();
1889b50d902SRodney W. Grimes 		exit (0);
1890e286f08SDavid Schultz 	} else if (chunks) {
1900e286f08SDavid Schultz 		split3();
1910e286f08SDavid Schultz 		exit (0);
1929b50d902SRodney W. Grimes 	}
1939b50d902SRodney W. Grimes 	split2();
1942fa6610fSArchie Cobbs 	if (pflag)
1952fa6610fSArchie Cobbs 		regfree(&rgx);
1969b50d902SRodney W. Grimes 	exit(0);
1979b50d902SRodney W. Grimes }
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes /*
2009b50d902SRodney W. Grimes  * split1 --
2019b50d902SRodney W. Grimes  *	Split the input by bytes.
2029b50d902SRodney W. Grimes  */
2030e286f08SDavid Schultz static void
split1(void)2042c69ee9bSJuli Mallett split1(void)
2059b50d902SRodney W. Grimes {
206c81180b8STim J. Robbins 	off_t bcnt;
2079b50d902SRodney W. Grimes 	char *C;
208c81180b8STim J. Robbins 	ssize_t dist, len;
2090e286f08SDavid Schultz 	int nfiles;
2100e286f08SDavid Schultz 
2110e286f08SDavid Schultz 	nfiles = 0;
2129b50d902SRodney W. Grimes 
2139b50d902SRodney W. Grimes 	for (bcnt = 0;;)
2142fa6610fSArchie Cobbs 		switch ((len = read(ifd, bfr, MAXBSIZE))) {
2159b50d902SRodney W. Grimes 		case 0:
2169b50d902SRodney W. Grimes 			exit(0);
2179b50d902SRodney W. Grimes 		case -1:
2182fa6610fSArchie Cobbs 			err(EX_IOERR, "read");
2199b50d902SRodney W. Grimes 			/* NOTREACHED */
2209b50d902SRodney W. Grimes 		default:
2210e286f08SDavid Schultz 			if (!file_open) {
2220e286f08SDavid Schultz 				if (!chunks || (nfiles < chunks)) {
2239b50d902SRodney W. Grimes 					newfile();
2240e286f08SDavid Schultz 					nfiles++;
2250e286f08SDavid Schultz 				}
2260e286f08SDavid Schultz 			}
227c81180b8STim J. Robbins 			if (bcnt + len >= bytecnt) {
2289b50d902SRodney W. Grimes 				dist = bytecnt - bcnt;
2299b50d902SRodney W. Grimes 				if (write(ofd, bfr, dist) != dist)
2302fa6610fSArchie Cobbs 					err(EX_IOERR, "write");
2319b50d902SRodney W. Grimes 				len -= dist;
2329b50d902SRodney W. Grimes 				for (C = bfr + dist; len >= bytecnt;
2339b50d902SRodney W. Grimes 				    len -= bytecnt, C += bytecnt) {
2340e286f08SDavid Schultz 					if (!chunks || (nfiles < chunks)) {
2359b50d902SRodney W. Grimes 					newfile();
2360e286f08SDavid Schultz 						nfiles++;
2370e286f08SDavid Schultz 					}
2389b50d902SRodney W. Grimes 					if (write(ofd,
239f1d37c20SMark Murray 					    C, bytecnt) != bytecnt)
2402fa6610fSArchie Cobbs 						err(EX_IOERR, "write");
2419b50d902SRodney W. Grimes 				}
2422fa6610fSArchie Cobbs 				if (len != 0) {
2430e286f08SDavid Schultz 					if (!chunks || (nfiles < chunks)) {
2449b50d902SRodney W. Grimes 					newfile();
2450e286f08SDavid Schultz 						nfiles++;
2460e286f08SDavid Schultz 					}
2479b50d902SRodney W. Grimes 					if (write(ofd, C, len) != len)
2482fa6610fSArchie Cobbs 						err(EX_IOERR, "write");
2499b50d902SRodney W. Grimes 				} else
2509b50d902SRodney W. Grimes 					file_open = 0;
2519b50d902SRodney W. Grimes 				bcnt = len;
2529b50d902SRodney W. Grimes 			} else {
2539b50d902SRodney W. Grimes 				bcnt += len;
2549b50d902SRodney W. Grimes 				if (write(ofd, bfr, len) != len)
2552fa6610fSArchie Cobbs 					err(EX_IOERR, "write");
2569b50d902SRodney W. Grimes 			}
2579b50d902SRodney W. Grimes 		}
2589b50d902SRodney W. Grimes }
2599b50d902SRodney W. Grimes 
2609b50d902SRodney W. Grimes /*
2619b50d902SRodney W. Grimes  * split2 --
2629b50d902SRodney W. Grimes  *	Split the input by lines.
2639b50d902SRodney W. Grimes  */
2640e286f08SDavid Schultz static void
split2(void)2652c69ee9bSJuli Mallett split2(void)
2669b50d902SRodney W. Grimes {
2672fa6610fSArchie Cobbs 	long lcnt = 0;
2682fa6610fSArchie Cobbs 	FILE *infp;
2699b50d902SRodney W. Grimes 
2702fa6610fSArchie Cobbs 	/* Stick a stream on top of input file descriptor */
2712fa6610fSArchie Cobbs 	if ((infp = fdopen(ifd, "r")) == NULL)
2722fa6610fSArchie Cobbs 		err(EX_NOINPUT, "fdopen");
2732fa6610fSArchie Cobbs 
2742fa6610fSArchie Cobbs 	/* Process input one line at a time */
2752fa6610fSArchie Cobbs 	while (fgets(bfr, sizeof(bfr), infp) != NULL) {
2762fa6610fSArchie Cobbs 		const int len = strlen(bfr);
2772fa6610fSArchie Cobbs 
2782fa6610fSArchie Cobbs 		/* If line is too long to deal with, just write it out */
2792fa6610fSArchie Cobbs 		if (bfr[len - 1] != '\n')
2802fa6610fSArchie Cobbs 			goto writeit;
2812fa6610fSArchie Cobbs 
2822fa6610fSArchie Cobbs 		/* Check if we need to start a new file */
2832fa6610fSArchie Cobbs 		if (pflag) {
284*bcd34af4SAlexander Kabaev 			regmatch_t pmatch;
2852fa6610fSArchie Cobbs 
286*bcd34af4SAlexander Kabaev 			pmatch.rm_so = 0;
287*bcd34af4SAlexander Kabaev 			pmatch.rm_eo = len - 1;
288*bcd34af4SAlexander Kabaev 			if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
2899b50d902SRodney W. Grimes 				newfile();
2902fa6610fSArchie Cobbs 		} else if (lcnt++ == numlines) {
2912fa6610fSArchie Cobbs 			newfile();
2922fa6610fSArchie Cobbs 			lcnt = 1;
2939b50d902SRodney W. Grimes 		}
2942fa6610fSArchie Cobbs 
2952fa6610fSArchie Cobbs writeit:
2962fa6610fSArchie Cobbs 		/* Open output file if needed */
2972fa6610fSArchie Cobbs 		if (!file_open)
2989b50d902SRodney W. Grimes 			newfile();
2992fa6610fSArchie Cobbs 
3002fa6610fSArchie Cobbs 		/* Write out line */
3012fa6610fSArchie Cobbs 		if (write(ofd, bfr, len) != len)
3022fa6610fSArchie Cobbs 			err(EX_IOERR, "write");
3032fa6610fSArchie Cobbs 	}
3042fa6610fSArchie Cobbs 
3052fa6610fSArchie Cobbs 	/* EOF or error? */
3062fa6610fSArchie Cobbs 	if (ferror(infp))
3072fa6610fSArchie Cobbs 		err(EX_IOERR, "read");
3089b50d902SRodney W. Grimes 	else
3092fa6610fSArchie Cobbs 		exit(0);
3109b50d902SRodney W. Grimes }
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes /*
3130e286f08SDavid Schultz  * split3 --
3140e286f08SDavid Schultz  *	Split the input into specified number of chunks
3150e286f08SDavid Schultz  */
3160e286f08SDavid Schultz static void
split3(void)3170e286f08SDavid Schultz split3(void)
3180e286f08SDavid Schultz {
3190e286f08SDavid Schultz 	struct stat sb;
3200e286f08SDavid Schultz 
3210e286f08SDavid Schultz 	if (fstat(ifd, &sb) == -1) {
3220e286f08SDavid Schultz 		err(1, "stat");
3230e286f08SDavid Schultz 		/* NOTREACHED */
3240e286f08SDavid Schultz 	}
3250e286f08SDavid Schultz 
3260e286f08SDavid Schultz 	if (chunks > sb.st_size) {
3270e286f08SDavid Schultz 		errx(1, "can't split into more than %d files",
3280e286f08SDavid Schultz 		    (int)sb.st_size);
3290e286f08SDavid Schultz 		/* NOTREACHED */
3300e286f08SDavid Schultz 	}
3310e286f08SDavid Schultz 
3320e286f08SDavid Schultz 	bytecnt = sb.st_size / chunks;
3330e286f08SDavid Schultz 	split1();
3340e286f08SDavid Schultz }
3350e286f08SDavid Schultz 
3360e286f08SDavid Schultz 
3370e286f08SDavid Schultz /*
3389b50d902SRodney W. Grimes  * newfile --
3399b50d902SRodney W. Grimes  *	Open a new output file.
3409b50d902SRodney W. Grimes  */
3410e286f08SDavid Schultz static void
newfile(void)3422c69ee9bSJuli Mallett newfile(void)
3439b50d902SRodney W. Grimes {
34441850495SMike Barcroft 	long i, maxfiles, tfnum;
3459b50d902SRodney W. Grimes 	static long fnum;
3469b50d902SRodney W. Grimes 	static char *fpnt;
3477f418e34SEitan Adler 	char beg, end;
3487f418e34SEitan Adler 	int pattlen;
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	if (ofd == -1) {
3519b50d902SRodney W. Grimes 		if (fname[0] == '\0') {
3529b50d902SRodney W. Grimes 			fname[0] = 'x';
3539b50d902SRodney W. Grimes 			fpnt = fname + 1;
3549b50d902SRodney W. Grimes 		} else {
3559b50d902SRodney W. Grimes 			fpnt = fname + strlen(fname);
3569b50d902SRodney W. Grimes 		}
3579b50d902SRodney W. Grimes 		ofd = fileno(stdout);
3589b50d902SRodney W. Grimes 	}
35941850495SMike Barcroft 
3607f418e34SEitan Adler 	if (dflag) {
3617f418e34SEitan Adler 		beg = '0';
3627f418e34SEitan Adler 		end = '9';
3637f418e34SEitan Adler 	}
3647f418e34SEitan Adler 	else {
3657f418e34SEitan Adler 		beg = 'a';
3667f418e34SEitan Adler 		end = 'z';
3677f418e34SEitan Adler 	}
3687f418e34SEitan Adler 	pattlen = end - beg + 1;
3697f418e34SEitan Adler 
3707f418e34SEitan Adler 	/* maxfiles = pattlen^sufflen, but don't use libm. */
37141850495SMike Barcroft 	for (maxfiles = 1, i = 0; i < sufflen; i++)
37289c5c3aaSEitan Adler 		if (LONG_MAX / pattlen < maxfiles)
37341850495SMike Barcroft 			errx(EX_USAGE, "suffix is too long (max %ld)", i);
37489c5c3aaSEitan Adler 		else
37589c5c3aaSEitan Adler 			maxfiles *= pattlen;
37641850495SMike Barcroft 
377e93586dfSTim J. Robbins 	if (fnum == maxfiles)
3782fa6610fSArchie Cobbs 		errx(EX_DATAERR, "too many files");
37941850495SMike Barcroft 
38041850495SMike Barcroft 	/* Generate suffix of sufflen letters */
38141850495SMike Barcroft 	tfnum = fnum;
38241850495SMike Barcroft 	i = sufflen - 1;
38341850495SMike Barcroft 	do {
3847f418e34SEitan Adler 		fpnt[i] = tfnum % pattlen + beg;
3857f418e34SEitan Adler 		tfnum /= pattlen;
38641850495SMike Barcroft 	} while (i-- > 0);
38741850495SMike Barcroft 	fpnt[sufflen] = '\0';
38841850495SMike Barcroft 
3899b50d902SRodney W. Grimes 	++fnum;
3909b50d902SRodney W. Grimes 	if (!freopen(fname, "w", stdout))
3912fa6610fSArchie Cobbs 		err(EX_IOERR, "%s", fname);
3922fa6610fSArchie Cobbs 	file_open = 1;
3939b50d902SRodney W. Grimes }
3949b50d902SRodney W. Grimes 
395c002b33bSPhilippe Charnier static void
usage(void)3962c69ee9bSJuli Mallett usage(void)
3979b50d902SRodney W. Grimes {
398d37fcb98SArchie Cobbs 	(void)fprintf(stderr,
39949198c42SGiorgos Keramidas "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
400a6dd1c93SGiorgos Keramidas "       split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
4010e286f08SDavid Schultz "       split -n chunk_count [-a suffix_length] [file [prefix]]\n"
40249198c42SGiorgos Keramidas "       split -p pattern [-a suffix_length] [file [prefix]]\n");
403d37fcb98SArchie Cobbs 	exit(EX_USAGE);
4049b50d902SRodney W. Grimes }
405