xref: /freebsd-14.2/usr.bin/split/split.c (revision 45764d1d)
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>
339b50d902SRodney W. Grimes #ifndef lint
34c002b33bSPhilippe Charnier static const char copyright[] =
359b50d902SRodney W. Grimes "@(#) Copyright (c) 1987, 1993, 1994\n\
369b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
37f1d37c20SMark Murray #endif
389b50d902SRodney W. Grimes 
399b50d902SRodney W. Grimes #ifndef lint
40f1d37c20SMark Murray static const char sccsid[] = "@(#)split.c	8.2 (Berkeley) 4/16/94";
41c002b33bSPhilippe Charnier #endif
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <sys/param.h>
440e286f08SDavid Schultz #include <sys/stat.h>
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <ctype.h>
479b50d902SRodney W. Grimes #include <err.h>
48c81180b8STim J. Robbins #include <errno.h>
499b50d902SRodney W. Grimes #include <fcntl.h>
50c81180b8STim J. Robbins #include <inttypes.h>
513ebd4af7SEitan Adler #include <libutil.h>
52c81180b8STim J. Robbins #include <limits.h>
530742d4edSTim J. Robbins #include <locale.h>
547f418e34SEitan Adler #include <stdbool.h>
55c81180b8STim J. Robbins #include <stdint.h>
569b50d902SRodney W. Grimes #include <stdio.h>
579b50d902SRodney W. Grimes #include <stdlib.h>
589b50d902SRodney W. Grimes #include <string.h>
599b50d902SRodney W. Grimes #include <unistd.h>
602fa6610fSArchie Cobbs #include <regex.h>
612fa6610fSArchie Cobbs #include <sysexits.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #define DEFLINE	1000			/* Default num lines per file. */
649b50d902SRodney W. Grimes 
65973aa6bcSEd Schouten static off_t	 bytecnt;		/* Byte count to split on. */
66b3cef2d2SDag-Erling Smørgrav static long	 chunks;		/* Chunks count to split into. */
67ac17fc81SJan Schaumann static bool      clobber = true;        /* Whether to overwrite existing output files. */
68973aa6bcSEd Schouten static long	 numlines;		/* Line count to split on. */
69973aa6bcSEd Schouten static int	 file_open;		/* If a file open. */
70973aa6bcSEd Schouten static int	 ifd = -1, ofd = -1;	/* Input/output file descriptors. */
71973aa6bcSEd Schouten static char	 fname[MAXPATHLEN];	/* File name prefix. */
72973aa6bcSEd Schouten static regex_t	 rgx;
73973aa6bcSEd Schouten static int	 pflag;
747f418e34SEitan Adler static bool	 dflag;
75973aa6bcSEd Schouten static long	 sufflen = 2;		/* File name suffix length. */
76b3cef2d2SDag-Erling Smørgrav static bool	 autosfx = true;	/* Whether to auto-extend the suffix length. */
779b50d902SRodney W. Grimes 
780e286f08SDavid Schultz static void newfile(void);
790e286f08SDavid Schultz static void split1(void);
800e286f08SDavid Schultz static void split2(void);
810e286f08SDavid Schultz static void split3(void);
82cccdaf50SAlfonso Gregory static void usage(void) __dead2;
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes int
main(int argc,char ** argv)852c69ee9bSJuli Mallett main(int argc, char **argv)
869b50d902SRodney W. Grimes {
87b3cef2d2SDag-Erling Smørgrav 	char errbuf[64];
88b3cef2d2SDag-Erling Smørgrav 	const char *p, *errstr;
8943e87327SDag-Erling Smørgrav 	int ch, error;
909b50d902SRodney W. Grimes 
910742d4edSTim J. Robbins 	setlocale(LC_ALL, "");
920742d4edSTim J. Robbins 
937f418e34SEitan Adler 	dflag = false;
9443e87327SDag-Erling Smørgrav 	while ((ch = getopt(argc, argv, "0::1::2::3::4::5::6::7::8::9::a:b:cdl:n:p:")) != -1)
959b50d902SRodney W. Grimes 		switch (ch) {
969b50d902SRodney W. Grimes 		case '0': case '1': case '2': case '3': case '4':
979b50d902SRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
989b50d902SRodney W. Grimes 			/*
999b50d902SRodney W. Grimes 			 * Undocumented kludge: split was originally designed
1009b50d902SRodney W. Grimes 			 * to take a number after a dash.
1019b50d902SRodney W. Grimes 			 */
10243e87327SDag-Erling Smørgrav 			if (numlines != 0)
10343e87327SDag-Erling Smørgrav 				usage();
10443e87327SDag-Erling Smørgrav 			numlines = ch - '0';
10543e87327SDag-Erling Smørgrav 			p = optarg ? optarg : "";
10643e87327SDag-Erling Smørgrav 			while (numlines >= 0 && *p >= '0' && *p <= '9')
10743e87327SDag-Erling Smørgrav 				numlines = numlines * 10 + *p++ - '0';
10843e87327SDag-Erling Smørgrav 			if (numlines <= 0 || *p != '\0')
109b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%c%s: line count is invalid",
110b3cef2d2SDag-Erling Smørgrav 				    ch, optarg ? optarg : "");
1119b50d902SRodney W. Grimes 			break;
11241850495SMike Barcroft 		case 'a':		/* Suffix length */
113b3cef2d2SDag-Erling Smørgrav 			sufflen = strtonum(optarg, 0, INT_MAX, &errstr);
114b3cef2d2SDag-Erling Smørgrav 			if (errstr != NULL) {
115b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%s: suffix length is %s",
116b3cef2d2SDag-Erling Smørgrav 				    optarg, errstr);
117b3cef2d2SDag-Erling Smørgrav 			}
118b3cef2d2SDag-Erling Smørgrav 			if (sufflen == 0) {
119b3cef2d2SDag-Erling Smørgrav 				sufflen = 2;
120b3cef2d2SDag-Erling Smørgrav 				autosfx = true;
121b3cef2d2SDag-Erling Smørgrav 			} else {
122b3cef2d2SDag-Erling Smørgrav 				autosfx = false;
123b3cef2d2SDag-Erling Smørgrav 			}
12441850495SMike Barcroft 			break;
1259b50d902SRodney W. Grimes 		case 'b':		/* Byte count. */
126b3cef2d2SDag-Erling Smørgrav 			if (expand_number(optarg, &bytecnt) != 0) {
127b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%s: byte count is invalid",
128b3cef2d2SDag-Erling Smørgrav 				    optarg);
129b3cef2d2SDag-Erling Smørgrav 			}
1309b50d902SRodney W. Grimes 			break;
131ac17fc81SJan Schaumann 		case 'c':               /* Continue, don't overwrite output files. */
132ac17fc81SJan Schaumann 			clobber = false;
133ac17fc81SJan Schaumann 			break;
1347f418e34SEitan Adler 		case 'd':		/* Decimal suffix */
1357f418e34SEitan Adler 			dflag = true;
1367f418e34SEitan Adler 			break;
1379b50d902SRodney W. Grimes 		case 'l':		/* Line count. */
1389b50d902SRodney W. Grimes 			if (numlines != 0)
1399b50d902SRodney W. Grimes 				usage();
140b3cef2d2SDag-Erling Smørgrav 			numlines = strtonum(optarg, 1, LONG_MAX, &errstr);
141b3cef2d2SDag-Erling Smørgrav 			if (errstr != NULL) {
142b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%s: line count is %s",
143b3cef2d2SDag-Erling Smørgrav 				    optarg, errstr);
144b3cef2d2SDag-Erling Smørgrav 			}
1459b50d902SRodney W. Grimes 			break;
1460e286f08SDavid Schultz 		case 'n':		/* Chunks. */
147b3cef2d2SDag-Erling Smørgrav 			chunks = strtonum(optarg, 1, LONG_MAX, &errstr);
148b3cef2d2SDag-Erling Smørgrav 			if (errstr != NULL) {
149b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%s: number of chunks is %s",
150b3cef2d2SDag-Erling Smørgrav 				    optarg, errstr);
1510e286f08SDavid Schultz 			}
1520e286f08SDavid Schultz 			break;
1530e286f08SDavid Schultz 
15449198c42SGiorgos Keramidas 		case 'p':		/* pattern matching. */
155b3cef2d2SDag-Erling Smørgrav 			error = regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB);
156b3cef2d2SDag-Erling Smørgrav 			if (error != 0) {
157b3cef2d2SDag-Erling Smørgrav 				regerror(error, &rgx, errbuf, sizeof(errbuf));
158b3cef2d2SDag-Erling Smørgrav 				errx(EX_USAGE, "%s: regex is invalid: %s",
159b3cef2d2SDag-Erling Smørgrav 				    optarg, errbuf);
160b3cef2d2SDag-Erling Smørgrav 			}
16149198c42SGiorgos Keramidas 			pflag = 1;
16249198c42SGiorgos Keramidas 			break;
1639b50d902SRodney W. Grimes 		default:
1649b50d902SRodney W. Grimes 			usage();
1659b50d902SRodney W. Grimes 		}
1669b50d902SRodney W. Grimes 	argv += optind;
1679b50d902SRodney W. Grimes 	argc -= optind;
1689b50d902SRodney W. Grimes 
16943e87327SDag-Erling Smørgrav 	if (argc > 0) {			/* Input file. */
1703e4228c3STim J. Robbins 		if (strcmp(*argv, "-") == 0)
1713e4228c3STim J. Robbins 			ifd = STDIN_FILENO;
1723e4228c3STim J. Robbins 		else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
1732fa6610fSArchie Cobbs 			err(EX_NOINPUT, "%s", *argv);
1749b50d902SRodney W. Grimes 		++argv;
17543e87327SDag-Erling Smørgrav 		--argc;
1769b50d902SRodney W. Grimes 	}
17743e87327SDag-Erling Smørgrav 	if (argc > 0) {			/* File name prefix. */
178b3cef2d2SDag-Erling Smørgrav 		if (strlcpy(fname, *argv, sizeof(fname)) >= sizeof(fname)) {
179b3cef2d2SDag-Erling Smørgrav 			errx(EX_USAGE, "%s: file name prefix is too long",
18043e87327SDag-Erling Smørgrav 			    *argv);
181b3cef2d2SDag-Erling Smørgrav 		}
18243e87327SDag-Erling Smørgrav 		++argv;
18343e87327SDag-Erling Smørgrav 		--argc;
18443e87327SDag-Erling Smørgrav 	}
18543e87327SDag-Erling Smørgrav 	if (argc > 0)
1869b50d902SRodney W. Grimes 		usage();
1879b50d902SRodney W. Grimes 
18841850495SMike Barcroft 	if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
18941850495SMike Barcroft 		errx(EX_USAGE, "suffix is too long");
1900e286f08SDavid Schultz 	if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
1912fa6610fSArchie Cobbs 		usage();
1922fa6610fSArchie Cobbs 
1939b50d902SRodney W. Grimes 	if (numlines == 0)
1949b50d902SRodney W. Grimes 		numlines = DEFLINE;
1950e286f08SDavid Schultz 	else if (bytecnt != 0 || chunks != 0)
1960e286f08SDavid Schultz 		usage();
1970e286f08SDavid Schultz 
198b3cef2d2SDag-Erling Smørgrav 	if (bytecnt != 0 && chunks != 0)
1999b50d902SRodney W. Grimes 		usage();
2009b50d902SRodney W. Grimes 
2019b50d902SRodney W. Grimes 	if (ifd == -1)				/* Stdin by default. */
2029b50d902SRodney W. Grimes 		ifd = 0;
2039b50d902SRodney W. Grimes 
204b3cef2d2SDag-Erling Smørgrav 	if (bytecnt != 0) {
2059b50d902SRodney W. Grimes 		split1();
2069b50d902SRodney W. Grimes 		exit (0);
207b3cef2d2SDag-Erling Smørgrav 	} else if (chunks != 0) {
2080e286f08SDavid Schultz 		split3();
2090e286f08SDavid Schultz 		exit (0);
2109b50d902SRodney W. Grimes 	}
2119b50d902SRodney W. Grimes 	split2();
2122fa6610fSArchie Cobbs 	if (pflag)
2132fa6610fSArchie Cobbs 		regfree(&rgx);
2149b50d902SRodney W. Grimes 	exit(0);
2159b50d902SRodney W. Grimes }
2169b50d902SRodney W. Grimes 
2179b50d902SRodney W. Grimes /*
2189b50d902SRodney W. Grimes  * split1 --
2199b50d902SRodney W. Grimes  *	Split the input by bytes.
2209b50d902SRodney W. Grimes  */
2210e286f08SDavid Schultz static void
split1(void)2222c69ee9bSJuli Mallett split1(void)
2239b50d902SRodney W. Grimes {
2245c053aa3SKyle Evans 	static char bfr[MAXBSIZE];
225c81180b8STim J. Robbins 	off_t bcnt;
2269b50d902SRodney W. Grimes 	char *C;
227c81180b8STim J. Robbins 	ssize_t dist, len;
2280e286f08SDavid Schultz 	int nfiles;
2290e286f08SDavid Schultz 
2300e286f08SDavid Schultz 	nfiles = 0;
2319b50d902SRodney W. Grimes 
2329b50d902SRodney W. Grimes 	for (bcnt = 0;;)
2335c053aa3SKyle Evans 		switch ((len = read(ifd, bfr, sizeof(bfr)))) {
2349b50d902SRodney W. Grimes 		case 0:
2359b50d902SRodney W. Grimes 			exit(0);
2369b50d902SRodney W. Grimes 		case -1:
2372fa6610fSArchie Cobbs 			err(EX_IOERR, "read");
2389b50d902SRodney W. Grimes 			/* NOTREACHED */
2399b50d902SRodney W. Grimes 		default:
2400e286f08SDavid Schultz 			if (!file_open) {
241b3cef2d2SDag-Erling Smørgrav 				if (chunks == 0 || nfiles < chunks) {
2429b50d902SRodney W. Grimes 					newfile();
2430e286f08SDavid Schultz 					nfiles++;
2440e286f08SDavid Schultz 				}
2450e286f08SDavid Schultz 			}
246c81180b8STim J. Robbins 			if (bcnt + len >= bytecnt) {
2479b50d902SRodney W. Grimes 				dist = bytecnt - bcnt;
2489b50d902SRodney W. Grimes 				if (write(ofd, bfr, dist) != dist)
2492fa6610fSArchie Cobbs 					err(EX_IOERR, "write");
2509b50d902SRodney W. Grimes 				len -= dist;
2519b50d902SRodney W. Grimes 				for (C = bfr + dist; len >= bytecnt;
2529b50d902SRodney W. Grimes 				     len -= bytecnt, C += bytecnt) {
253b3cef2d2SDag-Erling Smørgrav 					if (chunks == 0 || nfiles < chunks) {
2549b50d902SRodney W. Grimes 						newfile();
2550e286f08SDavid Schultz 						nfiles++;
2560e286f08SDavid Schultz 					}
257b3cef2d2SDag-Erling Smørgrav 					if (write(ofd, C, bytecnt) != bytecnt)
2582fa6610fSArchie Cobbs 						err(EX_IOERR, "write");
2599b50d902SRodney W. Grimes 				}
2602fa6610fSArchie Cobbs 				if (len != 0) {
261b3cef2d2SDag-Erling Smørgrav 					if (chunks == 0 || nfiles < chunks) {
2629b50d902SRodney W. Grimes 						newfile();
2630e286f08SDavid Schultz 						nfiles++;
2640e286f08SDavid Schultz 					}
2659b50d902SRodney W. Grimes 					if (write(ofd, C, len) != len)
2662fa6610fSArchie Cobbs 						err(EX_IOERR, "write");
267b3cef2d2SDag-Erling Smørgrav 				} else {
2689b50d902SRodney W. Grimes 					file_open = 0;
269b3cef2d2SDag-Erling Smørgrav 				}
2709b50d902SRodney W. Grimes 				bcnt = len;
2719b50d902SRodney W. Grimes 			} else {
2729b50d902SRodney W. Grimes 				bcnt += len;
2739b50d902SRodney W. Grimes 				if (write(ofd, bfr, len) != len)
2742fa6610fSArchie Cobbs 					err(EX_IOERR, "write");
2759b50d902SRodney W. Grimes 			}
2769b50d902SRodney W. Grimes 		}
2779b50d902SRodney W. Grimes }
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes /*
2809b50d902SRodney W. Grimes  * split2 --
2819b50d902SRodney W. Grimes  *	Split the input by lines.
2829b50d902SRodney W. Grimes  */
2830e286f08SDavid Schultz static void
split2(void)2842c69ee9bSJuli Mallett split2(void)
2859b50d902SRodney W. Grimes {
2865c053aa3SKyle Evans 	char *buf;
2875c053aa3SKyle Evans 	size_t bufsize;
2885c053aa3SKyle Evans 	ssize_t len;
2892fa6610fSArchie Cobbs 	long lcnt = 0;
2902fa6610fSArchie Cobbs 	FILE *infp;
2919b50d902SRodney W. Grimes 
2925c053aa3SKyle Evans 	buf = NULL;
2935c053aa3SKyle Evans 	bufsize = 0;
2945c053aa3SKyle Evans 
2952fa6610fSArchie Cobbs 	/* Stick a stream on top of input file descriptor */
2962fa6610fSArchie Cobbs 	if ((infp = fdopen(ifd, "r")) == NULL)
2972fa6610fSArchie Cobbs 		err(EX_NOINPUT, "fdopen");
2982fa6610fSArchie Cobbs 
2992fa6610fSArchie Cobbs 	/* Process input one line at a time */
300172be864SMath Ieu 	while ((errno = 0, len = getline(&buf, &bufsize, infp)) > 0) {
3012fa6610fSArchie Cobbs 		/* Check if we need to start a new file */
3022fa6610fSArchie Cobbs 		if (pflag) {
303bcd34af4SAlexander Kabaev 			regmatch_t pmatch;
3042fa6610fSArchie Cobbs 
305bcd34af4SAlexander Kabaev 			pmatch.rm_so = 0;
306bcd34af4SAlexander Kabaev 			pmatch.rm_eo = len - 1;
3075c053aa3SKyle Evans 			if (regexec(&rgx, buf, 0, &pmatch, REG_STARTEND) == 0)
3089b50d902SRodney W. Grimes 				newfile();
3092fa6610fSArchie Cobbs 		} else if (lcnt++ == numlines) {
3102fa6610fSArchie Cobbs 			newfile();
3112fa6610fSArchie Cobbs 			lcnt = 1;
3129b50d902SRodney W. Grimes 		}
3132fa6610fSArchie Cobbs 
3142fa6610fSArchie Cobbs 		/* Open output file if needed */
3152fa6610fSArchie Cobbs 		if (!file_open)
3169b50d902SRodney W. Grimes 			newfile();
3172fa6610fSArchie Cobbs 
3182fa6610fSArchie Cobbs 		/* Write out line */
3195c053aa3SKyle Evans 		if (write(ofd, buf, len) != len)
3202fa6610fSArchie Cobbs 			err(EX_IOERR, "write");
3212fa6610fSArchie Cobbs 	}
3222fa6610fSArchie Cobbs 
3232fa6610fSArchie Cobbs 	/* EOF or error? */
3245c053aa3SKyle Evans 	if ((len == -1 && errno != 0) || ferror(infp))
3252fa6610fSArchie Cobbs 		err(EX_IOERR, "read");
3269b50d902SRodney W. Grimes 	else
3272fa6610fSArchie Cobbs 		exit(0);
3289b50d902SRodney W. Grimes }
3299b50d902SRodney W. Grimes 
3309b50d902SRodney W. Grimes /*
3310e286f08SDavid Schultz  * split3 --
3320e286f08SDavid Schultz  *	Split the input into specified number of chunks
3330e286f08SDavid Schultz  */
3340e286f08SDavid Schultz static void
split3(void)3350e286f08SDavid Schultz split3(void)
3360e286f08SDavid Schultz {
3370e286f08SDavid Schultz 	struct stat sb;
3380e286f08SDavid Schultz 
3390e286f08SDavid Schultz 	if (fstat(ifd, &sb) == -1) {
3400e286f08SDavid Schultz 		err(1, "stat");
3410e286f08SDavid Schultz 		/* NOTREACHED */
3420e286f08SDavid Schultz 	}
3430e286f08SDavid Schultz 
3440e286f08SDavid Schultz 	if (chunks > sb.st_size) {
3450e286f08SDavid Schultz 		errx(1, "can't split into more than %d files",
3460e286f08SDavid Schultz 		    (int)sb.st_size);
3470e286f08SDavid Schultz 		/* NOTREACHED */
3480e286f08SDavid Schultz 	}
3490e286f08SDavid Schultz 
3500e286f08SDavid Schultz 	bytecnt = sb.st_size / chunks;
3510e286f08SDavid Schultz 	split1();
3520e286f08SDavid Schultz }
3530e286f08SDavid Schultz 
3540e286f08SDavid Schultz 
3550e286f08SDavid Schultz /*
3569b50d902SRodney W. Grimes  * newfile --
3579b50d902SRodney W. Grimes  *	Open a new output file.
3589b50d902SRodney W. Grimes  */
3590e286f08SDavid Schultz static void
newfile(void)3602c69ee9bSJuli Mallett newfile(void)
3619b50d902SRodney W. Grimes {
36241850495SMike Barcroft 	long i, maxfiles, tfnum;
3639b50d902SRodney W. Grimes 	static long fnum;
3649b50d902SRodney W. Grimes 	static char *fpnt;
3657f418e34SEitan Adler 	char beg, end;
3667f418e34SEitan Adler 	int pattlen;
367ac17fc81SJan Schaumann 	int flags = O_WRONLY | O_CREAT | O_TRUNC;
368ac17fc81SJan Schaumann 
369ac17fc81SJan Schaumann 	if (!clobber)
370ac17fc81SJan Schaumann 		flags |= O_EXCL;
3719b50d902SRodney W. Grimes 
3729b50d902SRodney W. Grimes 	if (ofd == -1) {
3739b50d902SRodney W. Grimes 		if (fname[0] == '\0') {
3749b50d902SRodney W. Grimes 			fname[0] = 'x';
3759b50d902SRodney W. Grimes 			fpnt = fname + 1;
3769b50d902SRodney W. Grimes 		} else {
3779b50d902SRodney W. Grimes 			fpnt = fname + strlen(fname);
3789b50d902SRodney W. Grimes 		}
379ac17fc81SJan Schaumann 	} else if (close(ofd) != 0)
380ac17fc81SJan Schaumann 		err(1, "%s", fname);
38141850495SMike Barcroft 
382ac17fc81SJan Schaumann 	again:
3837f418e34SEitan Adler 	if (dflag) {
3847f418e34SEitan Adler 		beg = '0';
3857f418e34SEitan Adler 		end = '9';
3867f418e34SEitan Adler 	}
3877f418e34SEitan Adler 	else {
3887f418e34SEitan Adler 		beg = 'a';
3897f418e34SEitan Adler 		end = 'z';
3907f418e34SEitan Adler 	}
3917f418e34SEitan Adler 	pattlen = end - beg + 1;
3927f418e34SEitan Adler 
393c4f7198fSJan Schaumann 	/*
394c4f7198fSJan Schaumann 	 * If '-a' is not specified, then we automatically expand the
395c4f7198fSJan Schaumann 	 * suffix length to accomodate splitting all input.  We do this
396c4f7198fSJan Schaumann 	 * by moving the suffix pointer (fpnt) forward and incrementing
397c4f7198fSJan Schaumann 	 * sufflen by one, thereby yielding an additional two characters
398c4f7198fSJan Schaumann 	 * and allowing all output files to sort such that 'cat *' yields
399c4f7198fSJan Schaumann 	 * the input in order.  I.e., the order is '... xyy xyz xzaaa
400c4f7198fSJan Schaumann 	 * xzaab ... xzyzy, xzyzz, xzzaaaa, xzzaaab' and so on.
401c4f7198fSJan Schaumann 	 */
402c4f7198fSJan Schaumann 	if (!dflag && autosfx && (fpnt[0] == 'y') &&
403c4f7198fSJan Schaumann 			strspn(fpnt+1, "z") == strlen(fpnt+1)) {
404*45764d1dSShawn Bayern 		/* Ensure the generated filenames will fit into the buffer. */
405*45764d1dSShawn Bayern 		if (strlen(fname) + 2 >= sizeof(fname))
406*45764d1dSShawn Bayern 			errx(EX_USAGE, "combined filenames would be too long");
407*45764d1dSShawn Bayern 
408c4f7198fSJan Schaumann 		fpnt = fname + strlen(fname) - sufflen;
409c4f7198fSJan Schaumann 		fpnt[sufflen + 2] = '\0';
410c4f7198fSJan Schaumann 		fpnt[0] = end;
411c4f7198fSJan Schaumann 		fpnt[1] = beg;
412c4f7198fSJan Schaumann 
413c4f7198fSJan Schaumann 		/*  Basename | Suffix
414c4f7198fSJan Schaumann 		 *  before:
415c4f7198fSJan Schaumann 		 *  x        | yz
416c4f7198fSJan Schaumann 		 *  after:
417c4f7198fSJan Schaumann 		 *  xz       | a.. */
418c4f7198fSJan Schaumann 		fpnt++;
419c4f7198fSJan Schaumann 		sufflen++;
420c4f7198fSJan Schaumann 
421c4f7198fSJan Schaumann 		/* Reset so we start back at all 'a's in our extended suffix. */
422c4f7198fSJan Schaumann 		fnum = 0;
423c4f7198fSJan Schaumann 	}
424c4f7198fSJan Schaumann 
4257f418e34SEitan Adler 	/* maxfiles = pattlen^sufflen, but don't use libm. */
42641850495SMike Barcroft 	for (maxfiles = 1, i = 0; i < sufflen; i++)
42789c5c3aaSEitan Adler 		if (LONG_MAX / pattlen < maxfiles)
42841850495SMike Barcroft 			errx(EX_USAGE, "suffix is too long (max %ld)", i);
42989c5c3aaSEitan Adler 		else
43089c5c3aaSEitan Adler 			maxfiles *= pattlen;
43141850495SMike Barcroft 
432e93586dfSTim J. Robbins 	if (fnum == maxfiles)
4332fa6610fSArchie Cobbs 		errx(EX_DATAERR, "too many files");
43441850495SMike Barcroft 
43541850495SMike Barcroft 	/* Generate suffix of sufflen letters */
43641850495SMike Barcroft 	tfnum = fnum;
43741850495SMike Barcroft 	i = sufflen - 1;
43841850495SMike Barcroft 	do {
4397f418e34SEitan Adler 		fpnt[i] = tfnum % pattlen + beg;
4407f418e34SEitan Adler 		tfnum /= pattlen;
44141850495SMike Barcroft 	} while (i-- > 0);
44241850495SMike Barcroft 	fpnt[sufflen] = '\0';
44341850495SMike Barcroft 
4449b50d902SRodney W. Grimes 	++fnum;
445ac17fc81SJan Schaumann 	if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) {
446ac17fc81SJan Schaumann 		if (!clobber && errno == EEXIST)
447ac17fc81SJan Schaumann 			goto again;
4482fa6610fSArchie Cobbs 		err(EX_IOERR, "%s", fname);
449ac17fc81SJan Schaumann 	}
4502fa6610fSArchie Cobbs 	file_open = 1;
4519b50d902SRodney W. Grimes }
4529b50d902SRodney W. Grimes 
453c002b33bSPhilippe Charnier static void
usage(void)4542c69ee9bSJuli Mallett usage(void)
4559b50d902SRodney W. Grimes {
456d37fcb98SArchie Cobbs 	(void)fprintf(stderr,
457ac17fc81SJan Schaumann "usage: split [-cd] [-l line_count] [-a suffix_length] [file [prefix]]\n"
458ac17fc81SJan Schaumann "       split [-cd] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
459ac17fc81SJan Schaumann "       split [-cd] -n chunk_count [-a suffix_length] [file [prefix]]\n"
460ac17fc81SJan Schaumann "       split [-cd] -p pattern [-a suffix_length] [file [prefix]]\n");
461d37fcb98SArchie Cobbs 	exit(EX_USAGE);
4629b50d902SRodney W. Grimes }
463