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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 static const char copyright[] =
39 "@(#) Copyright (c) 1987, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
47 #include <sys/param.h>
63 #define DEFLINE 1000 /* Default num lines per file. */
65 off_t bytecnt; /* Byte count to split on. */
66 long numlines; /* Line count to split on. */
67 int file_open; /* If a file open. */
68 int ifd = -1, ofd = -1; /* Input/output file descriptors. */
69 char bfr[MAXBSIZE]; /* I/O buffer. */
70 char fname[MAXPATHLEN]; /* File name prefix. */
73 long sufflen = 2; /* File name suffix length. */
78 static void usage(void);
81 main(int argc, char **argv)
88 while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1)
90 case '0': case '1': case '2': case '3': case '4':
91 case '5': case '6': case '7': case '8': case '9':
93 * Undocumented kludge: split was originally designed
94 * to take a number after a dash.
98 if (p[0] == '-' && p[1] == ch && !p[2])
99 numlines = strtol(++p, &ep, 10);
102 strtol(argv[optind] + 1, &ep, 10);
103 if (numlines <= 0 || *ep)
105 "%s: illegal line count", optarg);
108 case '-': /* Undocumented: historic stdin flag. */
113 case 'a': /* Suffix length */
114 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
116 "%s: illegal suffix length", optarg);
118 case 'b': /* Byte count. */
120 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
121 (*ep != '\0' && *ep != 'k' && *ep != 'm') ||
124 "%s: illegal byte count", optarg);
131 if (bytecnti > OFF_MAX / scale)
132 errx(EX_USAGE, "%s: offset too large", optarg);
133 bytecnt = (off_t)(bytecnti * scale);
135 case 'p' : /* pattern matching. */
136 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
137 errx(EX_USAGE, "%s: illegal regexp", optarg);
140 case 'l': /* Line count. */
143 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
145 "%s: illegal line count", optarg);
154 if (ifd == -1) { /* Input file. */
155 if (strcmp(*argv, "-") == 0)
157 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
158 err(EX_NOINPUT, "%s", *argv);
161 if (*argv != NULL) /* File name prefix. */
162 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
163 errx(EX_USAGE, "file name prefix is too long");
167 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
168 errx(EX_USAGE, "suffix is too long");
169 if (pflag && (numlines != 0 || bytecnt != 0))
174 else if (bytecnt != 0)
177 if (ifd == -1) /* Stdin by default. */
192 * Split the input by bytes.
202 switch ((len = read(ifd, bfr, MAXBSIZE))) {
206 err(EX_IOERR, "read");
211 if (bcnt + len >= bytecnt) {
212 dist = bytecnt - bcnt;
213 if (write(ofd, bfr, dist) != dist)
214 err(EX_IOERR, "write");
216 for (C = bfr + dist; len >= bytecnt;
217 len -= bytecnt, C += bytecnt) {
220 C, bytecnt) != bytecnt)
221 err(EX_IOERR, "write");
225 if (write(ofd, C, len) != len)
226 err(EX_IOERR, "write");
232 if (write(ofd, bfr, len) != len)
233 err(EX_IOERR, "write");
240 * Split the input by lines.
248 /* Stick a stream on top of input file descriptor */
249 if ((infp = fdopen(ifd, "r")) == NULL)
250 err(EX_NOINPUT, "fdopen");
252 /* Process input one line at a time */
253 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
254 const int len = strlen(bfr);
256 /* If line is too long to deal with, just write it out */
257 if (bfr[len - 1] != '\n')
260 /* Check if we need to start a new file */
265 pmatch.rm_eo = len - 1;
266 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
268 } else if (lcnt++ == numlines) {
274 /* Open output file if needed */
279 if (write(ofd, bfr, len) != len)
280 err(EX_IOERR, "write");
285 err(EX_IOERR, "read");
292 * Open a new output file.
297 long i, maxfiles, tfnum;
303 if (fname[0] == '\0') {
308 fpnt = fname + strlen(fname);
311 ofd = fileno(stdout);
314 /* maxfiles = 26^sufflen, but don't use libm. */
315 for (maxfiles = 1, i = 0; i < sufflen; i++)
316 if ((maxfiles *= 26) <= 0)
317 errx(EX_USAGE, "suffix is too long (max %ld)", i);
320 * Hack to increase max files; original code wandered through
323 if (fnum == maxfiles) {
324 if (!defname || fname[0] == 'z')
325 errx(EX_DATAERR, "too many files");
330 /* Generate suffix of sufflen letters */
334 fpnt[i] = tfnum % 26 + 'a';
337 fpnt[sufflen] = '\0';
340 if (!freopen(fname, "w", stdout))
341 err(EX_IOERR, "%s", fname);
348 (void)fprintf(stderr,
349 "usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n");
350 (void)fprintf(stderr,
351 " [file [prefix]]\n");