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>
48 #include <sys/types.h>
67 #define DEFLINE 1000 /* Default num lines per file. */
69 static off_t bytecnt; /* Byte count to split on. */
70 static off_t chunks = 0; /* Chunks count to split into. */
71 static long numlines; /* Line count to split on. */
72 static int file_open; /* If a file open. */
73 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
74 static char bfr[MAXBSIZE]; /* I/O buffer. */
75 static char fname[MAXPATHLEN]; /* File name prefix. */
79 static long sufflen = 2; /* File name suffix length. */
81 static void newfile(void);
82 static void split1(void);
83 static void split2(void);
84 static void split3(void);
85 static void usage(void);
88 main(int argc, char **argv)
95 setlocale(LC_ALL, "");
98 while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1)
100 case '0': case '1': case '2': case '3': case '4':
101 case '5': case '6': case '7': case '8': case '9':
103 * Undocumented kludge: split was originally designed
104 * to take a number after a dash.
107 p = argv[optind - 1];
108 if (p[0] == '-' && p[1] == ch && !p[2])
109 numlines = strtol(++p, &ep, 10);
112 strtol(argv[optind] + 1, &ep, 10);
113 if (numlines <= 0 || *ep)
115 "%s: illegal line count", optarg);
118 case 'a': /* Suffix length */
119 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
121 "%s: illegal suffix length", optarg);
123 case 'b': /* Byte count. */
125 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
126 strchr("kKmMgG", *ep) == NULL || errno != 0)
128 "%s: illegal byte count", optarg);
129 if (*ep == 'k' || *ep == 'K')
131 else if (*ep == 'm' || *ep == 'M')
133 else if (*ep == 'g' || *ep == 'G')
134 scale = 1024 * 1024 * 1024;
137 if (bytecnti > OFF_MAX / scale)
138 errx(EX_USAGE, "%s: offset too large", optarg);
139 bytecnt = (off_t)(bytecnti * scale);
141 case 'd': /* Decimal suffix */
144 case 'l': /* Line count. */
147 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
149 "%s: illegal line count", optarg);
151 case 'n': /* Chunks. */
152 if (!isdigit((unsigned char)optarg[0]) ||
153 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
155 errx(EX_USAGE, "%s: illegal number of chunks",
160 case 'p': /* pattern matching. */
161 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
162 errx(EX_USAGE, "%s: illegal regexp", optarg);
171 if (*argv != NULL) { /* Input file. */
172 if (strcmp(*argv, "-") == 0)
174 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
175 err(EX_NOINPUT, "%s", *argv);
178 if (*argv != NULL) /* File name prefix. */
179 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
180 errx(EX_USAGE, "file name prefix is too long");
184 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
185 errx(EX_USAGE, "suffix is too long");
186 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
191 else if (bytecnt != 0 || chunks != 0)
194 if (bytecnt && chunks)
197 if (ifd == -1) /* Stdin by default. */
215 * Split the input by bytes.
228 switch ((len = read(ifd, bfr, MAXBSIZE))) {
232 err(EX_IOERR, "read");
236 if (!chunks || (nfiles < chunks)) {
241 if (bcnt + len >= bytecnt) {
242 dist = bytecnt - bcnt;
243 if (write(ofd, bfr, dist) != dist)
244 err(EX_IOERR, "write");
246 for (C = bfr + dist; len >= bytecnt;
247 len -= bytecnt, C += bytecnt) {
248 if (!chunks || (nfiles < chunks)) {
253 C, bytecnt) != bytecnt)
254 err(EX_IOERR, "write");
257 if (!chunks || (nfiles < chunks)) {
261 if (write(ofd, C, len) != len)
262 err(EX_IOERR, "write");
268 if (write(ofd, bfr, len) != len)
269 err(EX_IOERR, "write");
276 * Split the input by lines.
284 /* Stick a stream on top of input file descriptor */
285 if ((infp = fdopen(ifd, "r")) == NULL)
286 err(EX_NOINPUT, "fdopen");
288 /* Process input one line at a time */
289 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
290 const int len = strlen(bfr);
292 /* If line is too long to deal with, just write it out */
293 if (bfr[len - 1] != '\n')
296 /* Check if we need to start a new file */
301 pmatch.rm_eo = len - 1;
302 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
304 } else if (lcnt++ == numlines) {
310 /* Open output file if needed */
315 if (write(ofd, bfr, len) != len)
316 err(EX_IOERR, "write");
321 err(EX_IOERR, "read");
328 * Split the input into specified number of chunks
335 if (fstat(ifd, &sb) == -1) {
340 if (chunks > sb.st_size) {
341 errx(1, "can't split into more than %d files",
346 bytecnt = sb.st_size / chunks;
353 * Open a new output file.
358 long i, maxfiles, tfnum;
365 if (fname[0] == '\0') {
369 fpnt = fname + strlen(fname);
371 ofd = fileno(stdout);
382 pattlen = end - beg + 1;
384 /* maxfiles = pattlen^sufflen, but don't use libm. */
385 for (maxfiles = 1, i = 0; i < sufflen; i++)
386 if (LONG_MAX / pattlen < maxfiles)
387 errx(EX_USAGE, "suffix is too long (max %ld)", i);
391 if (fnum == maxfiles)
392 errx(EX_DATAERR, "too many files");
394 /* Generate suffix of sufflen letters */
398 fpnt[i] = tfnum % pattlen + beg;
401 fpnt[sufflen] = '\0';
404 if (!freopen(fname, "w", stdout))
405 err(EX_IOERR, "%s", fname);
412 (void)fprintf(stderr,
413 "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
414 " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
415 " split -n chunk_count [-a suffix_length] [file [prefix]]\n"
416 " split -p pattern [-a suffix_length] [file [prefix]]\n");