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>
62 #define DEFLINE 1000 /* Default num lines per file. */
64 static off_t bytecnt; /* Byte count to split on. */
65 static off_t chunks = 0; /* Chunks count to split into. */
66 static long numlines; /* Line count to split on. */
67 static int file_open; /* If a file open. */
68 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
69 static char bfr[MAXBSIZE]; /* I/O buffer. */
70 static char fname[MAXPATHLEN]; /* File name prefix. */
73 static long sufflen = 2; /* File name suffix length. */
75 static void newfile(void);
76 static void split1(void);
77 static void split2(void);
78 static void split3(void);
79 static void usage(void);
82 main(int argc, char **argv)
89 setlocale(LC_ALL, "");
91 while ((ch = getopt(argc, argv, "0123456789a:b:l:n:p:")) != -1)
93 case '0': case '1': case '2': case '3': case '4':
94 case '5': case '6': case '7': case '8': case '9':
96 * Undocumented kludge: split was originally designed
97 * to take a number after a dash.
100 p = argv[optind - 1];
101 if (p[0] == '-' && p[1] == ch && !p[2])
102 numlines = strtol(++p, &ep, 10);
105 strtol(argv[optind] + 1, &ep, 10);
106 if (numlines <= 0 || *ep)
108 "%s: illegal line count", optarg);
111 case 'a': /* Suffix length */
112 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
114 "%s: illegal suffix length", optarg);
116 case 'b': /* Byte count. */
118 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
119 strchr("kKmMgG", *ep) == NULL || errno != 0)
121 "%s: illegal byte count", optarg);
122 if (*ep == 'k' || *ep == 'K')
124 else if (*ep == 'm' || *ep == 'M')
126 else if (*ep == 'g' || *ep == 'G')
127 scale = 1024 * 1024 * 1024;
130 if (bytecnti > OFF_MAX / scale)
131 errx(EX_USAGE, "%s: offset too large", optarg);
132 bytecnt = (off_t)(bytecnti * scale);
134 case 'l': /* Line count. */
137 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
139 "%s: illegal line count", optarg);
141 case 'n': /* Chunks. */
142 if (!isdigit((unsigned char)optarg[0]) ||
143 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
145 errx(EX_USAGE, "%s: illegal number of chunks",
150 case 'p': /* pattern matching. */
151 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
152 errx(EX_USAGE, "%s: illegal regexp", optarg);
161 if (*argv != NULL) { /* Input file. */
162 if (strcmp(*argv, "-") == 0)
164 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
165 err(EX_NOINPUT, "%s", *argv);
168 if (*argv != NULL) /* File name prefix. */
169 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
170 errx(EX_USAGE, "file name prefix is too long");
174 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
175 errx(EX_USAGE, "suffix is too long");
176 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
181 else if (bytecnt != 0 || chunks != 0)
184 if (bytecnt && chunks)
187 if (ifd == -1) /* Stdin by default. */
205 * Split the input by bytes.
218 switch ((len = read(ifd, bfr, MAXBSIZE))) {
222 err(EX_IOERR, "read");
226 if (!chunks || (nfiles < chunks)) {
231 if (bcnt + len >= bytecnt) {
232 dist = bytecnt - bcnt;
233 if (write(ofd, bfr, dist) != dist)
234 err(EX_IOERR, "write");
236 for (C = bfr + dist; len >= bytecnt;
237 len -= bytecnt, C += bytecnt) {
238 if (!chunks || (nfiles < chunks)) {
243 C, bytecnt) != bytecnt)
244 err(EX_IOERR, "write");
247 if (!chunks || (nfiles < chunks)) {
251 if (write(ofd, C, len) != len)
252 err(EX_IOERR, "write");
258 if (write(ofd, bfr, len) != len)
259 err(EX_IOERR, "write");
266 * Split the input by lines.
274 /* Stick a stream on top of input file descriptor */
275 if ((infp = fdopen(ifd, "r")) == NULL)
276 err(EX_NOINPUT, "fdopen");
278 /* Process input one line at a time */
279 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
280 const int len = strlen(bfr);
282 /* If line is too long to deal with, just write it out */
283 if (bfr[len - 1] != '\n')
286 /* Check if we need to start a new file */
291 pmatch.rm_eo = len - 1;
292 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
294 } else if (lcnt++ == numlines) {
300 /* Open output file if needed */
305 if (write(ofd, bfr, len) != len)
306 err(EX_IOERR, "write");
311 err(EX_IOERR, "read");
318 * Split the input into specified number of chunks
325 if (fstat(ifd, &sb) == -1) {
330 if (chunks > sb.st_size) {
331 errx(1, "can't split into more than %d files",
336 bytecnt = sb.st_size / chunks;
343 * Open a new output file.
348 long i, maxfiles, tfnum;
354 if (fname[0] == '\0') {
359 fpnt = fname + strlen(fname);
362 ofd = fileno(stdout);
365 /* maxfiles = 26^sufflen, but don't use libm. */
366 for (maxfiles = 1, i = 0; i < sufflen; i++)
367 if ((maxfiles *= 26) <= 0)
368 errx(EX_USAGE, "suffix is too long (max %ld)", i);
370 if (fnum == maxfiles)
371 errx(EX_DATAERR, "too many files");
373 /* Generate suffix of sufflen letters */
377 fpnt[i] = tfnum % 26 + 'a';
380 fpnt[sufflen] = '\0';
383 if (!freopen(fname, "w", stdout))
384 err(EX_IOERR, "%s", fname);
391 (void)fprintf(stderr,
392 "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
393 " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
394 " split -n chunk_count [-a suffix_length] [file [prefix]]\n"
395 " split -p pattern [-a suffix_length] [file [prefix]]\n");