2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 static const char copyright[] =
35 "@(#) Copyright (c) 1987, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
43 #include <sys/param.h>
44 #include <sys/types.h>
63 #define DEFLINE 1000 /* Default num lines per file. */
65 static off_t bytecnt; /* Byte count to split on. */
66 static off_t chunks = 0; /* Chunks count to split into. */
67 static long numlines; /* Line count to split on. */
68 static int file_open; /* If a file open. */
69 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
70 static char bfr[MAXBSIZE]; /* I/O buffer. */
71 static char fname[MAXPATHLEN]; /* File name prefix. */
75 static long sufflen = 2; /* File name suffix length. */
77 static void newfile(void);
78 static void split1(void);
79 static void split2(void);
80 static void split3(void);
81 static void usage(void);
84 main(int argc, char **argv)
91 setlocale(LC_ALL, "");
94 while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1)
96 case '0': case '1': case '2': case '3': case '4':
97 case '5': case '6': case '7': case '8': case '9':
99 * Undocumented kludge: split was originally designed
100 * to take a number after a dash.
103 p = argv[optind - 1];
104 if (p[0] == '-' && p[1] == ch && !p[2])
105 numlines = strtol(++p, &ep, 10);
108 strtol(argv[optind] + 1, &ep, 10);
109 if (numlines <= 0 || *ep)
111 "%s: illegal line count", optarg);
114 case 'a': /* Suffix length */
115 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
117 "%s: illegal suffix length", optarg);
119 case 'b': /* Byte count. */
121 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
122 strchr("kKmMgG", *ep) == NULL || errno != 0)
124 "%s: illegal byte count", optarg);
125 if (*ep == 'k' || *ep == 'K')
127 else if (*ep == 'm' || *ep == 'M')
129 else if (*ep == 'g' || *ep == 'G')
130 scale = 1024 * 1024 * 1024;
133 if (bytecnti > OFF_MAX / scale)
134 errx(EX_USAGE, "%s: offset too large", optarg);
135 bytecnt = (off_t)(bytecnti * scale);
137 case 'd': /* Decimal suffix */
140 case 'l': /* Line count. */
143 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
145 "%s: illegal line count", optarg);
147 case 'n': /* Chunks. */
148 if (!isdigit((unsigned char)optarg[0]) ||
149 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
151 errx(EX_USAGE, "%s: illegal number of chunks",
156 case 'p': /* pattern matching. */
157 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
158 errx(EX_USAGE, "%s: illegal regexp", optarg);
167 if (*argv != NULL) { /* Input file. */
168 if (strcmp(*argv, "-") == 0)
170 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
171 err(EX_NOINPUT, "%s", *argv);
174 if (*argv != NULL) /* File name prefix. */
175 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
176 errx(EX_USAGE, "file name prefix is too long");
180 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
181 errx(EX_USAGE, "suffix is too long");
182 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
187 else if (bytecnt != 0 || chunks != 0)
190 if (bytecnt && chunks)
193 if (ifd == -1) /* Stdin by default. */
211 * Split the input by bytes.
224 switch ((len = read(ifd, bfr, MAXBSIZE))) {
228 err(EX_IOERR, "read");
232 if (!chunks || (nfiles < chunks)) {
237 if (bcnt + len >= bytecnt) {
238 dist = bytecnt - bcnt;
239 if (write(ofd, bfr, dist) != dist)
240 err(EX_IOERR, "write");
242 for (C = bfr + dist; len >= bytecnt;
243 len -= bytecnt, C += bytecnt) {
244 if (!chunks || (nfiles < chunks)) {
249 C, bytecnt) != bytecnt)
250 err(EX_IOERR, "write");
253 if (!chunks || (nfiles < chunks)) {
257 if (write(ofd, C, len) != len)
258 err(EX_IOERR, "write");
264 if (write(ofd, bfr, len) != len)
265 err(EX_IOERR, "write");
272 * Split the input by lines.
280 /* Stick a stream on top of input file descriptor */
281 if ((infp = fdopen(ifd, "r")) == NULL)
282 err(EX_NOINPUT, "fdopen");
284 /* Process input one line at a time */
285 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
286 const int len = strlen(bfr);
288 /* If line is too long to deal with, just write it out */
289 if (bfr[len - 1] != '\n')
292 /* Check if we need to start a new file */
297 pmatch.rm_eo = len - 1;
298 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
300 } else if (lcnt++ == numlines) {
306 /* Open output file if needed */
311 if (write(ofd, bfr, len) != len)
312 err(EX_IOERR, "write");
317 err(EX_IOERR, "read");
324 * Split the input into specified number of chunks
331 if (fstat(ifd, &sb) == -1) {
336 if (chunks > sb.st_size) {
337 errx(1, "can't split into more than %d files",
342 bytecnt = sb.st_size / chunks;
349 * Open a new output file.
354 long i, maxfiles, tfnum;
361 if (fname[0] == '\0') {
365 fpnt = fname + strlen(fname);
367 ofd = fileno(stdout);
378 pattlen = end - beg + 1;
380 /* maxfiles = pattlen^sufflen, but don't use libm. */
381 for (maxfiles = 1, i = 0; i < sufflen; i++)
382 if (LONG_MAX / pattlen < maxfiles)
383 errx(EX_USAGE, "suffix is too long (max %ld)", i);
387 if (fnum == maxfiles)
388 errx(EX_DATAERR, "too many files");
390 /* Generate suffix of sufflen letters */
394 fpnt[i] = tfnum % pattlen + beg;
397 fpnt[sufflen] = '\0';
400 if (!freopen(fname, "w", stdout))
401 err(EX_IOERR, "%s", fname);
408 (void)fprintf(stderr,
409 "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
410 " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
411 " split -n chunk_count [-a suffix_length] [file [prefix]]\n"
412 " split -p pattern [-a suffix_length] [file [prefix]]\n");