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. 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
29 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)split.c 8.2 (Berkeley) 4/16/94
31 * $FreeBSD: src/usr.bin/split/split.c,v 1.6.2.2 2002/07/25 12:46:36 tjr Exp $
34 #include <sys/param.h>
50 #define DEFLINE 1000 /* Default num lines per file. */
52 static int64_t bytecnt; /* Byte count to split on. */
53 static long numlines; /* Line count to split on. */
54 static int file_open; /* If a file open. */
55 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
56 static char *bfr; /* I/O buffer. */
57 static char fname[MAXPATHLEN]; /* File name prefix. */
61 static long sufflen = 2; /* File name suffix length. */
63 static void newfile(void);
64 static void split1(void);
65 static void split2(void);
66 static void usage(void);
69 main(int argc, char **argv)
75 while ((ch = getopt(argc, argv, "0123456789a:b:dl:p:")) != -1)
77 case '0': case '1': case '2': case '3': case '4':
78 case '5': case '6': case '7': case '8': case '9':
80 * Undocumented kludge: split was originally designed
81 * to take a number after a dash.
85 if (p[0] == '-' && p[1] == ch && !p[2])
86 numlines = strtol(++p, &ep, 10);
89 strtol(argv[optind] + 1, &ep, 10);
90 if (numlines <= 0 || *ep)
92 "%s: illegal line count", optarg);
95 case 'a': /* Suffix length */
96 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
98 "%s: illegal suffix length", optarg);
100 case 'b': /* Byte count. */
101 error = dehumanize_number(optarg, &bytecnt);
103 errx(EX_USAGE, "%s: illegal byte count",
106 case 'd': /* Decimal suffix */
109 case 'p' : /* pattern matching. */
110 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
111 errx(EX_USAGE, "%s: illegal regexp", optarg);
114 case 'l': /* Line count. */
117 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
119 "%s: illegal line count", optarg);
128 if (strcmp(*argv, "-") == 0)
130 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
131 err(EX_NOINPUT, "%s", *argv);
135 if (*argv != NULL) /* File name prefix. */
136 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
137 errx(EX_USAGE, "file name prefix is too long");
141 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
142 errx(EX_USAGE, "suffix is too long");
143 if (pflag && (numlines != 0 || bytecnt != 0))
148 else if (bytecnt != 0)
151 if (ifd == -1) /* Stdin by default. */
166 * Split the input by bytes.
175 if((bfr = (char *)malloc(bytecnt)) == NULL)
176 err(EX_OSERR, "malloc");
179 switch ((len = read(ifd, bfr, bytecnt))) {
185 err(EX_IOERR, "read");
190 if (bcnt + len >= bytecnt) {
191 dist = bytecnt - bcnt;
192 if (write(ofd, bfr, dist) != dist)
193 err(EX_IOERR, "write");
195 for (C = bfr + dist; len >= bytecnt;
196 len -= bytecnt, C += bytecnt) {
199 C, (int)bytecnt) != bytecnt) {
201 err(EX_IOERR, "write");
206 if (write(ofd, C, len) != len) {
208 err(EX_IOERR, "write");
215 if (write(ofd, bfr, len) != len) {
217 err(EX_IOERR, "write");
226 * Split the input by lines.
235 /* Stick a stream on top of input file descriptor */
236 if ((infp = fdopen(ifd, "r")) == NULL)
237 err(EX_NOINPUT, "fdopen");
239 if((bfr = (char *)malloc(MAXBSIZE)) == NULL)
240 err(EX_OSERR, "malloc");
242 /* Process input one line at a time */
243 while (fgets(bfr, MAXBSIZE, infp) != NULL) {
244 const int len = strlen(bfr);
246 /* Consider starting a new file only when at beginning of a line */
248 /* Check if we need to start a new file */
253 pmatch.rm_eo = len - 1;
254 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
256 } else if (lcnt++ == numlines) {
262 if (bfr[len - 1] != '\n')
267 /* Open output file if needed */
272 if (write(ofd, bfr, len) != len) {
274 err(EX_IOERR, "write");
282 err(EX_IOERR, "read");
289 * Open a new output file.
294 long i, maxfiles, tfnum;
301 if (fname[0] == '\0') {
305 fpnt = fname + strlen(fname);
307 ofd = fileno(stdout);
318 pattlen = end - beg + 1;
320 /* maxfiles = pattlen^sufflen, but don't use libm. */
321 for (maxfiles = 1, i = 0; i < sufflen; i++)
322 if (LONG_MAX / pattlen < maxfiles)
323 errx(EX_USAGE, "suffix is too long (max %ld)", i);
327 if (fnum == maxfiles)
328 errx(EX_DATAERR, "too many files");
330 /* Generate suffix of sufflen letters */
334 fpnt[i] = tfnum % pattlen + beg;
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");