2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 static const char copyright[] =
37 "@(#) Copyright (c) 1987, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
45 #include <sys/param.h>
46 #include <sys/types.h>
66 #define DEFLINE 1000 /* Default num lines per file. */
68 static off_t bytecnt; /* Byte count to split on. */
69 static off_t chunks = 0; /* Chunks count to split into. */
70 static long numlines; /* Line count to split on. */
71 static int file_open; /* If a file open. */
72 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
73 static char bfr[MAXBSIZE]; /* I/O buffer. */
74 static char fname[MAXPATHLEN]; /* File name prefix. */
78 static long sufflen = 2; /* File name suffix length. */
80 static void newfile(void);
81 static void split1(void);
82 static void split2(void);
83 static void split3(void);
84 static void usage(void);
87 main(int argc, char **argv)
93 setlocale(LC_ALL, "");
96 while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1)
98 case '0': case '1': case '2': case '3': case '4':
99 case '5': case '6': case '7': case '8': case '9':
101 * Undocumented kludge: split was originally designed
102 * to take a number after a dash.
105 p = argv[optind - 1];
106 if (p[0] == '-' && p[1] == ch && !p[2])
107 numlines = strtol(++p, &ep, 10);
110 strtol(argv[optind] + 1, &ep, 10);
111 if (numlines <= 0 || *ep)
113 "%s: illegal line count", optarg);
116 case 'a': /* Suffix length */
117 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
119 "%s: illegal suffix length", optarg);
121 case 'b': /* Byte count. */
123 error = expand_number(optarg, &bytecnt);
125 errx(EX_USAGE, "%s: offset too large", optarg);
127 case 'd': /* Decimal suffix */
130 case 'l': /* Line count. */
133 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
135 "%s: illegal line count", optarg);
137 case 'n': /* Chunks. */
138 if (!isdigit((unsigned char)optarg[0]) ||
139 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
141 errx(EX_USAGE, "%s: illegal number of chunks",
146 case 'p': /* pattern matching. */
147 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
148 errx(EX_USAGE, "%s: illegal regexp", optarg);
157 if (*argv != NULL) { /* Input file. */
158 if (strcmp(*argv, "-") == 0)
160 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
161 err(EX_NOINPUT, "%s", *argv);
164 if (*argv != NULL) /* File name prefix. */
165 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
166 errx(EX_USAGE, "file name prefix is too long");
170 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
171 errx(EX_USAGE, "suffix is too long");
172 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
177 else if (bytecnt != 0 || chunks != 0)
180 if (bytecnt && chunks)
183 if (ifd == -1) /* Stdin by default. */
201 * Split the input by bytes.
214 switch ((len = read(ifd, bfr, MAXBSIZE))) {
218 err(EX_IOERR, "read");
222 if (!chunks || (nfiles < chunks)) {
227 if (bcnt + len >= bytecnt) {
228 dist = bytecnt - bcnt;
229 if (write(ofd, bfr, dist) != dist)
230 err(EX_IOERR, "write");
232 for (C = bfr + dist; len >= bytecnt;
233 len -= bytecnt, C += bytecnt) {
234 if (!chunks || (nfiles < chunks)) {
239 C, bytecnt) != bytecnt)
240 err(EX_IOERR, "write");
243 if (!chunks || (nfiles < chunks)) {
247 if (write(ofd, C, len) != len)
248 err(EX_IOERR, "write");
254 if (write(ofd, bfr, len) != len)
255 err(EX_IOERR, "write");
262 * Split the input by lines.
270 /* Stick a stream on top of input file descriptor */
271 if ((infp = fdopen(ifd, "r")) == NULL)
272 err(EX_NOINPUT, "fdopen");
274 /* Process input one line at a time */
275 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
276 const int len = strlen(bfr);
278 /* If line is too long to deal with, just write it out */
279 if (bfr[len - 1] != '\n')
282 /* Check if we need to start a new file */
287 pmatch.rm_eo = len - 1;
288 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
290 } else if (lcnt++ == numlines) {
296 /* Open output file if needed */
301 if (write(ofd, bfr, len) != len)
302 err(EX_IOERR, "write");
307 err(EX_IOERR, "read");
314 * Split the input into specified number of chunks
321 if (fstat(ifd, &sb) == -1) {
326 if (chunks > sb.st_size) {
327 errx(1, "can't split into more than %d files",
332 bytecnt = sb.st_size / chunks;
339 * Open a new output file.
344 long i, maxfiles, tfnum;
351 if (fname[0] == '\0') {
355 fpnt = fname + strlen(fname);
357 ofd = fileno(stdout);
368 pattlen = end - beg + 1;
370 /* maxfiles = pattlen^sufflen, but don't use libm. */
371 for (maxfiles = 1, i = 0; i < sufflen; i++)
372 if (LONG_MAX / pattlen < maxfiles)
373 errx(EX_USAGE, "suffix is too long (max %ld)", i);
377 if (fnum == maxfiles)
378 errx(EX_DATAERR, "too many files");
380 /* Generate suffix of sufflen letters */
384 fpnt[i] = tfnum % pattlen + beg;
387 fpnt[sufflen] = '\0';
390 if (!freopen(fname, "w", stdout))
391 err(EX_IOERR, "%s", fname);
398 (void)fprintf(stderr,
399 "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
400 " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
401 " split -n chunk_count [-a suffix_length] [file [prefix]]\n"
402 " split -p pattern [-a suffix_length] [file [prefix]]\n");