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>
64 #define DEFLINE 1000 /* Default num lines per file. */
66 static off_t bytecnt; /* Byte count to split on. */
67 static off_t chunks = 0; /* Chunks count to split into. */
68 static long numlines; /* Line count to split on. */
69 static int file_open; /* If a file open. */
70 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
71 static char bfr[MAXBSIZE]; /* I/O buffer. */
72 static char fname[MAXPATHLEN]; /* File name prefix. */
76 static long sufflen = 2; /* File name suffix length. */
78 static void newfile(void);
79 static void split1(void);
80 static void split2(void);
81 static void split3(void);
82 static void usage(void);
85 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 error = expand_number(optarg, &bytecnt);
123 errx(EX_USAGE, "%s: offset too large", optarg);
125 case 'd': /* Decimal suffix */
128 case 'l': /* Line count. */
131 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
133 "%s: illegal line count", optarg);
135 case 'n': /* Chunks. */
136 if (!isdigit((unsigned char)optarg[0]) ||
137 (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
139 errx(EX_USAGE, "%s: illegal number of chunks",
144 case 'p': /* pattern matching. */
145 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
146 errx(EX_USAGE, "%s: illegal regexp", optarg);
155 if (*argv != NULL) { /* Input file. */
156 if (strcmp(*argv, "-") == 0)
158 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
159 err(EX_NOINPUT, "%s", *argv);
162 if (*argv != NULL) /* File name prefix. */
163 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
164 errx(EX_USAGE, "file name prefix is too long");
168 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
169 errx(EX_USAGE, "suffix is too long");
170 if (pflag && (numlines != 0 || bytecnt != 0 || chunks != 0))
175 else if (bytecnt != 0 || chunks != 0)
178 if (bytecnt && chunks)
181 if (ifd == -1) /* Stdin by default. */
199 * Split the input by bytes.
212 switch ((len = read(ifd, bfr, MAXBSIZE))) {
216 err(EX_IOERR, "read");
220 if (!chunks || (nfiles < chunks)) {
225 if (bcnt + len >= bytecnt) {
226 dist = bytecnt - bcnt;
227 if (write(ofd, bfr, dist) != dist)
228 err(EX_IOERR, "write");
230 for (C = bfr + dist; len >= bytecnt;
231 len -= bytecnt, C += bytecnt) {
232 if (!chunks || (nfiles < chunks)) {
237 C, bytecnt) != bytecnt)
238 err(EX_IOERR, "write");
241 if (!chunks || (nfiles < chunks)) {
245 if (write(ofd, C, len) != len)
246 err(EX_IOERR, "write");
252 if (write(ofd, bfr, len) != len)
253 err(EX_IOERR, "write");
260 * Split the input by lines.
268 /* Stick a stream on top of input file descriptor */
269 if ((infp = fdopen(ifd, "r")) == NULL)
270 err(EX_NOINPUT, "fdopen");
272 /* Process input one line at a time */
273 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
274 const int len = strlen(bfr);
276 /* If line is too long to deal with, just write it out */
277 if (bfr[len - 1] != '\n')
280 /* Check if we need to start a new file */
285 pmatch.rm_eo = len - 1;
286 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
288 } else if (lcnt++ == numlines) {
294 /* Open output file if needed */
299 if (write(ofd, bfr, len) != len)
300 err(EX_IOERR, "write");
305 err(EX_IOERR, "read");
312 * Split the input into specified number of chunks
319 if (fstat(ifd, &sb) == -1) {
324 if (chunks > sb.st_size) {
325 errx(1, "can't split into more than %d files",
330 bytecnt = sb.st_size / chunks;
337 * Open a new output file.
342 long i, maxfiles, tfnum;
349 if (fname[0] == '\0') {
353 fpnt = fname + strlen(fname);
355 ofd = fileno(stdout);
366 pattlen = end - beg + 1;
368 /* maxfiles = pattlen^sufflen, but don't use libm. */
369 for (maxfiles = 1, i = 0; i < sufflen; i++)
370 if (LONG_MAX / pattlen < maxfiles)
371 errx(EX_USAGE, "suffix is too long (max %ld)", i);
375 if (fnum == maxfiles)
376 errx(EX_DATAERR, "too many files");
378 /* Generate suffix of sufflen letters */
382 fpnt[i] = tfnum % pattlen + beg;
385 fpnt[sufflen] = '\0';
388 if (!freopen(fname, "w", stdout))
389 err(EX_IOERR, "%s", fname);
396 (void)fprintf(stderr,
397 "usage: split [-l line_count] [-a suffix_length] [file [prefix]]\n"
398 " split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n"
399 " split -n chunk_count [-a suffix_length] [file [prefix]]\n"
400 " split -p pattern [-a suffix_length] [file [prefix]]\n");