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
33 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)split.c 8.2 (Berkeley) 4/16/94
35 * $FreeBSD: src/usr.bin/split/split.c,v 1.6.2.2 2002/07/25 12:46:36 tjr Exp $
36 * $DragonFly: src/usr.bin/split/split.c,v 1.4 2003/10/25 14:14:52 hmp Exp $
39 #include <sys/param.h>
53 #define DEFLINE 1000 /* Default num lines per file. */
55 off_t bytecnt; /* Byte count to split on. */
56 long numlines; /* Line count to split on. */
57 int file_open; /* If a file open. */
58 int ifd = -1, ofd = -1; /* Input/output file descriptors. */
59 char *bfr; /* I/O buffer. */
60 char fname[MAXPATHLEN]; /* File name prefix. */
63 long sufflen = 2; /* File name suffix length. */
68 static void usage(void);
71 main(int argc, char **argv)
78 while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1)
80 case '0': case '1': case '2': case '3': case '4':
81 case '5': case '6': case '7': case '8': case '9':
83 * Undocumented kludge: split was originally designed
84 * to take a number after a dash.
88 if (p[0] == '-' && p[1] == ch && !p[2])
89 numlines = strtol(++p, &ep, 10);
92 strtol(argv[optind] + 1, &ep, 10);
93 if (numlines <= 0 || *ep)
95 "%s: illegal line count", optarg);
98 case '-': /* Undocumented: historic stdin flag. */
103 case 'a': /* Suffix length */
104 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
106 "%s: illegal suffix length", optarg);
108 case 'b': /* Byte count. */
110 if ((bytecnti = strtoll(optarg, &ep, 10)) <= 0 ||
111 (*ep != '\0' && *ep != 'k' && *ep != 'm') ||
114 "%s: illegal byte count", optarg);
121 bytecnt = (off_t)(bytecnti * scale);
123 case 'p' : /* pattern matching. */
124 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
125 errx(EX_USAGE, "%s: illegal regexp", optarg);
128 case 'l': /* Line count. */
131 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
133 "%s: illegal line count", optarg);
142 if (ifd == -1) { /* Input file. */
143 if (strcmp(*argv, "-") == 0)
145 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
146 err(EX_NOINPUT, "%s", *argv);
149 if (*argv != NULL) /* File name prefix. */
150 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
151 errx(EX_USAGE, "file name prefix is too long");
155 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
156 errx(EX_USAGE, "suffix is too long");
157 if (pflag && (numlines != 0 || bytecnt != 0))
162 else if (bytecnt != 0)
165 if (ifd == -1) /* Stdin by default. */
180 * Split the input by bytes.
189 if((bfr = (char *)malloc(bytecnt)) == NULL)
190 err(EX_OSERR, "malloc");
193 switch ((len = read(ifd, bfr, bytecnt))) {
199 err(EX_IOERR, "read");
204 if (bcnt + len >= bytecnt) {
205 dist = bytecnt - bcnt;
206 if (write(ofd, bfr, dist) != dist)
207 err(EX_IOERR, "write");
209 for (C = bfr + dist; len >= bytecnt;
210 len -= bytecnt, C += bytecnt) {
213 C, (int)bytecnt) != bytecnt) {
215 err(EX_IOERR, "write");
220 if (write(ofd, C, len) != len) {
222 err(EX_IOERR, "write");
229 if (write(ofd, bfr, len) != len) {
231 err(EX_IOERR, "write");
240 * Split the input by lines.
249 /* Stick a stream on top of input file descriptor */
250 if ((infp = fdopen(ifd, "r")) == NULL)
251 err(EX_NOINPUT, "fdopen");
253 if((bfr = (char *)malloc(MAXBSIZE)) == NULL)
254 err(EX_OSERR, "malloc");
256 /* Process input one line at a time */
257 while (fgets(bfr, MAXBSIZE, infp) != NULL) {
258 const int len = strlen(bfr);
260 /* Consider starting a new file only when at beginning of a line */
262 /* Check if we need to start a new file */
267 pmatch.rm_eo = len - 1;
268 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
270 } else if (lcnt++ == numlines) {
276 if (bfr[len - 1] != '\n')
281 /* Open output file if needed */
286 if (write(ofd, bfr, len) != len) {
288 err(EX_IOERR, "write");
296 err(EX_IOERR, "read");
303 * Open a new output file.
308 long i, maxfiles, tfnum;
314 if (fname[0] == '\0') {
319 fpnt = fname + strlen(fname);
322 ofd = fileno(stdout);
325 /* maxfiles = 26^sufflen, but don't use libm. */
326 for (maxfiles = 1, i = 0; i < sufflen; i++)
327 if ((maxfiles *= 26) <= 0)
328 errx(EX_USAGE, "suffix is too long (max %ld)", i);
331 * Hack to increase max files; original code wandered through
334 if (fnum == maxfiles) {
335 if (!defname || fname[0] == 'z')
336 errx(EX_DATAERR, "too many files");
341 /* Generate suffix of sufflen letters */
345 fpnt[i] = tfnum % 26 + 'a';
348 fpnt[sufflen] = '\0';
351 if (!freopen(fname, "w", stdout))
352 err(EX_IOERR, "%s", fname);
359 (void)fprintf(stderr,
360 "usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n");
361 (void)fprintf(stderr,
362 " [file [prefix]]\n");