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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
46 #include <sys/param.h>
47 #include <sys/types.h>
59 #define DEFLINE 1000 /* Default num lines per file. */
61 long bytecnt; /* Byte count to split on. */
62 long numlines; /* Line count to split on. */
63 int file_open; /* If a file open. */
64 int ifd = -1, ofd = -1; /* Input/output file descriptors. */
65 char bfr[MAXBSIZE]; /* I/O buffer. */
66 char fname[MAXPATHLEN]; /* File name prefix. */
70 void newfile __P((void));
71 void split1 __P((void));
72 void split2 __P((void));
73 static void usage __P((void));
83 while ((ch = getopt(argc, argv, "-0123456789b:l:p:")) != -1)
85 case '0': case '1': case '2': case '3': case '4':
86 case '5': case '6': case '7': case '8': case '9':
88 * Undocumented kludge: split was originally designed
89 * to take a number after a dash.
93 if (p[0] == '-' && p[1] == ch && !p[2])
94 numlines = strtol(++p, &ep, 10);
97 strtol(argv[optind] + 1, &ep, 10);
98 if (numlines <= 0 || *ep)
100 "%s: illegal line count", optarg);
103 case '-': /* Undocumented: historic stdin flag. */
108 case 'b': /* Byte count. */
109 if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
110 (*ep != '\0' && *ep != 'k' && *ep != 'm'))
112 "%s: illegal byte count", optarg);
118 case 'p' : /* pattern matching. */
119 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
120 errx(EX_USAGE, "%s: illegal regexp", optarg);
123 case 'l': /* Line count. */
126 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
128 "%s: illegal line count", optarg);
137 if (ifd == -1) { /* Input file. */
138 if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
139 err(EX_NOINPUT, "%s", *argv);
142 if (*argv != NULL) /* File name prefix. */
143 (void)strcpy(fname, *argv++);
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.
180 switch ((len = read(ifd, bfr, MAXBSIZE))) {
184 err(EX_IOERR, "read");
189 if (bcnt + len >= bytecnt) {
190 dist = bytecnt - bcnt;
191 if (write(ofd, bfr, dist) != dist)
192 err(EX_IOERR, "write");
194 for (C = bfr + dist; len >= bytecnt;
195 len -= bytecnt, C += bytecnt) {
198 C, (int)bytecnt) != bytecnt)
199 err(EX_IOERR, "write");
203 if (write(ofd, C, len) != len)
204 err(EX_IOERR, "write");
210 if (write(ofd, bfr, len) != len)
211 err(EX_IOERR, "write");
218 * Split the input by lines.
226 /* Stick a stream on top of input file descriptor */
227 if ((infp = fdopen(ifd, "r")) == NULL)
228 err(EX_NOINPUT, "fdopen");
230 /* Process input one line at a time */
231 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
232 const int len = strlen(bfr);
234 /* If line is too long to deal with, just write it out */
235 if (bfr[len - 1] != '\n')
238 /* Check if we need to start a new file */
243 pmatch.rm_eo = len - 1;
244 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
246 } else if (lcnt++ == numlines) {
252 /* Open output file if needed */
257 if (write(ofd, bfr, len) != len)
258 err(EX_IOERR, "write");
263 err(EX_IOERR, "read");
270 * Open a new output file.
280 if (fname[0] == '\0') {
285 fpnt = fname + strlen(fname);
288 ofd = fileno(stdout);
291 * Hack to increase max files; original code wandered through
292 * magic characters. Maximum files is 3 * 26 * 26 == 2028
295 if (fnum == MAXFILES) {
296 if (!defname || fname[0] == 'z')
297 errx(EX_DATAERR, "too many files");
301 fpnt[0] = fnum / 26 + 'a';
302 fpnt[1] = fnum % 26 + 'a';
304 if (!freopen(fname, "w", stdout))
305 err(EX_IOERR, "%s", fname);
312 (void)fprintf(stderr,
313 "usage: split [-b byte_count] [-l line_count] [-p pattern] [file [prefix]]\n");