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
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>
48 #define DEFLINE 1000 /* Default num lines per file. */
50 off_t bytecnt; /* Byte count to split on. */
51 long numlines; /* Line count to split on. */
52 int file_open; /* If a file open. */
53 int ifd = -1, ofd = -1; /* Input/output file descriptors. */
54 char *bfr; /* I/O buffer. */
55 char fname[MAXPATHLEN]; /* File name prefix. */
58 long sufflen = 2; /* File name suffix length. */
63 static void usage(void);
66 main(int argc, char **argv)
73 while ((ch = getopt(argc, argv, "0123456789a:b:l:p:")) != -1)
75 case '0': case '1': case '2': case '3': case '4':
76 case '5': case '6': case '7': case '8': case '9':
78 * Undocumented kludge: split was originally designed
79 * to take a number after a dash.
83 if (p[0] == '-' && p[1] == ch && !p[2])
84 numlines = strtol(++p, &ep, 10);
87 strtol(argv[optind] + 1, &ep, 10);
88 if (numlines <= 0 || *ep)
90 "%s: illegal line count", optarg);
93 case 'a': /* Suffix length */
94 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
96 "%s: illegal suffix length", optarg);
98 case 'b': /* Byte count. */
100 if ((bytecnti = strtoll(optarg, &ep, 10)) <= 0 ||
101 (*ep != '\0' && *ep != 'k' && *ep != 'm') ||
104 "%s: illegal byte count", optarg);
111 bytecnt = (off_t)(bytecnti * scale);
113 case 'p' : /* pattern matching. */
114 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
115 errx(EX_USAGE, "%s: illegal regexp", optarg);
118 case 'l': /* Line count. */
121 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
123 "%s: illegal line count", optarg);
132 if (strcmp(*argv, "-") == 0)
134 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
135 err(EX_NOINPUT, "%s", *argv);
139 if (*argv != NULL) /* File name prefix. */
140 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
141 errx(EX_USAGE, "file name prefix is too long");
145 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
146 errx(EX_USAGE, "suffix is too long");
147 if (pflag && (numlines != 0 || bytecnt != 0))
152 else if (bytecnt != 0)
155 if (ifd == -1) /* Stdin by default. */
170 * Split the input by bytes.
179 if((bfr = (char *)malloc(bytecnt)) == NULL)
180 err(EX_OSERR, "malloc");
183 switch ((len = read(ifd, bfr, bytecnt))) {
189 err(EX_IOERR, "read");
194 if (bcnt + len >= bytecnt) {
195 dist = bytecnt - bcnt;
196 if (write(ofd, bfr, dist) != dist)
197 err(EX_IOERR, "write");
199 for (C = bfr + dist; len >= bytecnt;
200 len -= bytecnt, C += bytecnt) {
203 C, (int)bytecnt) != bytecnt) {
205 err(EX_IOERR, "write");
210 if (write(ofd, C, len) != len) {
212 err(EX_IOERR, "write");
219 if (write(ofd, bfr, len) != len) {
221 err(EX_IOERR, "write");
230 * Split the input by lines.
239 /* Stick a stream on top of input file descriptor */
240 if ((infp = fdopen(ifd, "r")) == NULL)
241 err(EX_NOINPUT, "fdopen");
243 if((bfr = (char *)malloc(MAXBSIZE)) == NULL)
244 err(EX_OSERR, "malloc");
246 /* Process input one line at a time */
247 while (fgets(bfr, MAXBSIZE, infp) != NULL) {
248 const int len = strlen(bfr);
250 /* Consider starting a new file only when at beginning of a line */
252 /* Check if we need to start a new file */
257 pmatch.rm_eo = len - 1;
258 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
260 } else if (lcnt++ == numlines) {
266 if (bfr[len - 1] != '\n')
271 /* Open output file if needed */
276 if (write(ofd, bfr, len) != len) {
278 err(EX_IOERR, "write");
286 err(EX_IOERR, "read");
293 * Open a new output file.
298 long i, maxfiles, tfnum;
303 if (fname[0] == '\0') {
307 fpnt = fname + strlen(fname);
309 ofd = fileno(stdout);
312 /* maxfiles = 26^sufflen, but don't use libm. */
313 for (maxfiles = 1, i = 0; i < sufflen; i++)
314 if ((maxfiles *= 26) <= 0)
315 errx(EX_USAGE, "suffix is too long (max %ld)", i);
317 if (fnum == maxfiles)
318 errx(EX_DATAERR, "too many files");
320 /* Generate suffix of sufflen letters */
324 fpnt[i] = tfnum % 26 + 'a';
327 fpnt[sufflen] = '\0';
330 if (!freopen(fname, "w", stdout))
331 err(EX_IOERR, "%s", fname);
338 (void)fprintf(stderr,
339 "usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n");
340 (void)fprintf(stderr,
341 " [file [prefix]]\n");