Deleted Added
full compact
split.c (131978) split.c (149345)
1/*
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/usr.bin/split/split.c 131978 2004-07-11 14:44:23Z tjr $");
35__FBSDID("$FreeBSD: head/usr.bin/split/split.c 149345 2005-08-21 06:35:02Z tjr $");
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1987, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
42
43#ifndef lint
44static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
45#endif
46
47#include <sys/param.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <inttypes.h>
54#include <limits.h>
55#include <locale.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61#include <regex.h>
62#include <sysexits.h>
63
64#define DEFLINE 1000 /* Default num lines per file. */
65
66off_t bytecnt; /* Byte count to split on. */
67long numlines; /* Line count to split on. */
68int file_open; /* If a file open. */
69int ifd = -1, ofd = -1; /* Input/output file descriptors. */
70char bfr[MAXBSIZE]; /* I/O buffer. */
71char fname[MAXPATHLEN]; /* File name prefix. */
72regex_t rgx;
73int pflag;
74long sufflen = 2; /* File name suffix length. */
75
76void newfile(void);
77void split1(void);
78void split2(void);
79static void usage(void);
80
81int
82main(int argc, char **argv)
83{
84 intmax_t bytecnti;
85 long scale;
86 int ch;
87 char *ep, *p;
88
89 setlocale(LC_ALL, "");
90
91 while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1)
92 switch (ch) {
93 case '0': case '1': case '2': case '3': case '4':
94 case '5': case '6': case '7': case '8': case '9':
95 /*
96 * Undocumented kludge: split was originally designed
97 * to take a number after a dash.
98 */
99 if (numlines == 0) {
100 p = argv[optind - 1];
101 if (p[0] == '-' && p[1] == ch && !p[2])
102 numlines = strtol(++p, &ep, 10);
103 else
104 numlines =
105 strtol(argv[optind] + 1, &ep, 10);
106 if (numlines <= 0 || *ep)
107 errx(EX_USAGE,
108 "%s: illegal line count", optarg);
109 }
110 break;
111 case '-': /* Undocumented: historic stdin flag. */
112 if (ifd != -1)
113 usage();
114 ifd = 0;
115 break;
116 case 'a': /* Suffix length */
117 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
118 errx(EX_USAGE,
119 "%s: illegal suffix length", optarg);
120 break;
121 case 'b': /* Byte count. */
122 errno = 0;
123 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
124 (*ep != '\0' && *ep != 'k' && *ep != 'm') ||
125 errno != 0)
126 errx(EX_USAGE,
127 "%s: illegal byte count", optarg);
128 if (*ep == 'k')
129 scale = 1024;
130 else if (*ep == 'm')
131 scale = 1024 * 1024;
132 else
133 scale = 1;
134 if (bytecnti > OFF_MAX / scale)
135 errx(EX_USAGE, "%s: offset too large", optarg);
136 bytecnt = (off_t)(bytecnti * scale);
137 break;
138 case 'p' : /* pattern matching. */
139 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
140 errx(EX_USAGE, "%s: illegal regexp", optarg);
141 pflag = 1;
142 break;
143 case 'l': /* Line count. */
144 if (numlines != 0)
145 usage();
146 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
147 errx(EX_USAGE,
148 "%s: illegal line count", optarg);
149 break;
150 default:
151 usage();
152 }
153 argv += optind;
154 argc -= optind;
155
156 if (*argv != NULL)
157 if (ifd == -1) { /* Input file. */
158 if (strcmp(*argv, "-") == 0)
159 ifd = STDIN_FILENO;
160 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
161 err(EX_NOINPUT, "%s", *argv);
162 ++argv;
163 }
164 if (*argv != NULL) /* File name prefix. */
165 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
166 errx(EX_USAGE, "file name prefix is too long");
167 if (*argv != NULL)
168 usage();
169
170 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
171 errx(EX_USAGE, "suffix is too long");
172 if (pflag && (numlines != 0 || bytecnt != 0))
173 usage();
174
175 if (numlines == 0)
176 numlines = DEFLINE;
177 else if (bytecnt != 0)
178 usage();
179
180 if (ifd == -1) /* Stdin by default. */
181 ifd = 0;
182
183 if (bytecnt) {
184 split1();
185 exit (0);
186 }
187 split2();
188 if (pflag)
189 regfree(&rgx);
190 exit(0);
191}
192
193/*
194 * split1 --
195 * Split the input by bytes.
196 */
197void
198split1(void)
199{
200 off_t bcnt;
201 char *C;
202 ssize_t dist, len;
203
204 for (bcnt = 0;;)
205 switch ((len = read(ifd, bfr, MAXBSIZE))) {
206 case 0:
207 exit(0);
208 case -1:
209 err(EX_IOERR, "read");
210 /* NOTREACHED */
211 default:
212 if (!file_open)
213 newfile();
214 if (bcnt + len >= bytecnt) {
215 dist = bytecnt - bcnt;
216 if (write(ofd, bfr, dist) != dist)
217 err(EX_IOERR, "write");
218 len -= dist;
219 for (C = bfr + dist; len >= bytecnt;
220 len -= bytecnt, C += bytecnt) {
221 newfile();
222 if (write(ofd,
223 C, bytecnt) != bytecnt)
224 err(EX_IOERR, "write");
225 }
226 if (len != 0) {
227 newfile();
228 if (write(ofd, C, len) != len)
229 err(EX_IOERR, "write");
230 } else
231 file_open = 0;
232 bcnt = len;
233 } else {
234 bcnt += len;
235 if (write(ofd, bfr, len) != len)
236 err(EX_IOERR, "write");
237 }
238 }
239}
240
241/*
242 * split2 --
243 * Split the input by lines.
244 */
245void
246split2(void)
247{
248 long lcnt = 0;
249 FILE *infp;
250
251 /* Stick a stream on top of input file descriptor */
252 if ((infp = fdopen(ifd, "r")) == NULL)
253 err(EX_NOINPUT, "fdopen");
254
255 /* Process input one line at a time */
256 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
257 const int len = strlen(bfr);
258
259 /* If line is too long to deal with, just write it out */
260 if (bfr[len - 1] != '\n')
261 goto writeit;
262
263 /* Check if we need to start a new file */
264 if (pflag) {
265 regmatch_t pmatch;
266
267 pmatch.rm_so = 0;
268 pmatch.rm_eo = len - 1;
269 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
270 newfile();
271 } else if (lcnt++ == numlines) {
272 newfile();
273 lcnt = 1;
274 }
275
276writeit:
277 /* Open output file if needed */
278 if (!file_open)
279 newfile();
280
281 /* Write out line */
282 if (write(ofd, bfr, len) != len)
283 err(EX_IOERR, "write");
284 }
285
286 /* EOF or error? */
287 if (ferror(infp))
288 err(EX_IOERR, "read");
289 else
290 exit(0);
291}
292
293/*
294 * newfile --
295 * Open a new output file.
296 */
297void
298newfile(void)
299{
300 long i, maxfiles, tfnum;
301 static long fnum;
302 static int defname;
303 static char *fpnt;
304
305 if (ofd == -1) {
306 if (fname[0] == '\0') {
307 fname[0] = 'x';
308 fpnt = fname + 1;
309 defname = 1;
310 } else {
311 fpnt = fname + strlen(fname);
312 defname = 0;
313 }
314 ofd = fileno(stdout);
315 }
316
317 /* maxfiles = 26^sufflen, but don't use libm. */
318 for (maxfiles = 1, i = 0; i < sufflen; i++)
319 if ((maxfiles *= 26) <= 0)
320 errx(EX_USAGE, "suffix is too long (max %ld)", i);
321
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1987, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
42
43#ifndef lint
44static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94";
45#endif
46
47#include <sys/param.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <inttypes.h>
54#include <limits.h>
55#include <locale.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61#include <regex.h>
62#include <sysexits.h>
63
64#define DEFLINE 1000 /* Default num lines per file. */
65
66off_t bytecnt; /* Byte count to split on. */
67long numlines; /* Line count to split on. */
68int file_open; /* If a file open. */
69int ifd = -1, ofd = -1; /* Input/output file descriptors. */
70char bfr[MAXBSIZE]; /* I/O buffer. */
71char fname[MAXPATHLEN]; /* File name prefix. */
72regex_t rgx;
73int pflag;
74long sufflen = 2; /* File name suffix length. */
75
76void newfile(void);
77void split1(void);
78void split2(void);
79static void usage(void);
80
81int
82main(int argc, char **argv)
83{
84 intmax_t bytecnti;
85 long scale;
86 int ch;
87 char *ep, *p;
88
89 setlocale(LC_ALL, "");
90
91 while ((ch = getopt(argc, argv, "-0123456789a:b:l:p:")) != -1)
92 switch (ch) {
93 case '0': case '1': case '2': case '3': case '4':
94 case '5': case '6': case '7': case '8': case '9':
95 /*
96 * Undocumented kludge: split was originally designed
97 * to take a number after a dash.
98 */
99 if (numlines == 0) {
100 p = argv[optind - 1];
101 if (p[0] == '-' && p[1] == ch && !p[2])
102 numlines = strtol(++p, &ep, 10);
103 else
104 numlines =
105 strtol(argv[optind] + 1, &ep, 10);
106 if (numlines <= 0 || *ep)
107 errx(EX_USAGE,
108 "%s: illegal line count", optarg);
109 }
110 break;
111 case '-': /* Undocumented: historic stdin flag. */
112 if (ifd != -1)
113 usage();
114 ifd = 0;
115 break;
116 case 'a': /* Suffix length */
117 if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
118 errx(EX_USAGE,
119 "%s: illegal suffix length", optarg);
120 break;
121 case 'b': /* Byte count. */
122 errno = 0;
123 if ((bytecnti = strtoimax(optarg, &ep, 10)) <= 0 ||
124 (*ep != '\0' && *ep != 'k' && *ep != 'm') ||
125 errno != 0)
126 errx(EX_USAGE,
127 "%s: illegal byte count", optarg);
128 if (*ep == 'k')
129 scale = 1024;
130 else if (*ep == 'm')
131 scale = 1024 * 1024;
132 else
133 scale = 1;
134 if (bytecnti > OFF_MAX / scale)
135 errx(EX_USAGE, "%s: offset too large", optarg);
136 bytecnt = (off_t)(bytecnti * scale);
137 break;
138 case 'p' : /* pattern matching. */
139 if (regcomp(&rgx, optarg, REG_EXTENDED|REG_NOSUB) != 0)
140 errx(EX_USAGE, "%s: illegal regexp", optarg);
141 pflag = 1;
142 break;
143 case 'l': /* Line count. */
144 if (numlines != 0)
145 usage();
146 if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *ep)
147 errx(EX_USAGE,
148 "%s: illegal line count", optarg);
149 break;
150 default:
151 usage();
152 }
153 argv += optind;
154 argc -= optind;
155
156 if (*argv != NULL)
157 if (ifd == -1) { /* Input file. */
158 if (strcmp(*argv, "-") == 0)
159 ifd = STDIN_FILENO;
160 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
161 err(EX_NOINPUT, "%s", *argv);
162 ++argv;
163 }
164 if (*argv != NULL) /* File name prefix. */
165 if (strlcpy(fname, *argv++, sizeof(fname)) >= sizeof(fname))
166 errx(EX_USAGE, "file name prefix is too long");
167 if (*argv != NULL)
168 usage();
169
170 if (strlen(fname) + (unsigned long)sufflen >= sizeof(fname))
171 errx(EX_USAGE, "suffix is too long");
172 if (pflag && (numlines != 0 || bytecnt != 0))
173 usage();
174
175 if (numlines == 0)
176 numlines = DEFLINE;
177 else if (bytecnt != 0)
178 usage();
179
180 if (ifd == -1) /* Stdin by default. */
181 ifd = 0;
182
183 if (bytecnt) {
184 split1();
185 exit (0);
186 }
187 split2();
188 if (pflag)
189 regfree(&rgx);
190 exit(0);
191}
192
193/*
194 * split1 --
195 * Split the input by bytes.
196 */
197void
198split1(void)
199{
200 off_t bcnt;
201 char *C;
202 ssize_t dist, len;
203
204 for (bcnt = 0;;)
205 switch ((len = read(ifd, bfr, MAXBSIZE))) {
206 case 0:
207 exit(0);
208 case -1:
209 err(EX_IOERR, "read");
210 /* NOTREACHED */
211 default:
212 if (!file_open)
213 newfile();
214 if (bcnt + len >= bytecnt) {
215 dist = bytecnt - bcnt;
216 if (write(ofd, bfr, dist) != dist)
217 err(EX_IOERR, "write");
218 len -= dist;
219 for (C = bfr + dist; len >= bytecnt;
220 len -= bytecnt, C += bytecnt) {
221 newfile();
222 if (write(ofd,
223 C, bytecnt) != bytecnt)
224 err(EX_IOERR, "write");
225 }
226 if (len != 0) {
227 newfile();
228 if (write(ofd, C, len) != len)
229 err(EX_IOERR, "write");
230 } else
231 file_open = 0;
232 bcnt = len;
233 } else {
234 bcnt += len;
235 if (write(ofd, bfr, len) != len)
236 err(EX_IOERR, "write");
237 }
238 }
239}
240
241/*
242 * split2 --
243 * Split the input by lines.
244 */
245void
246split2(void)
247{
248 long lcnt = 0;
249 FILE *infp;
250
251 /* Stick a stream on top of input file descriptor */
252 if ((infp = fdopen(ifd, "r")) == NULL)
253 err(EX_NOINPUT, "fdopen");
254
255 /* Process input one line at a time */
256 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
257 const int len = strlen(bfr);
258
259 /* If line is too long to deal with, just write it out */
260 if (bfr[len - 1] != '\n')
261 goto writeit;
262
263 /* Check if we need to start a new file */
264 if (pflag) {
265 regmatch_t pmatch;
266
267 pmatch.rm_so = 0;
268 pmatch.rm_eo = len - 1;
269 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
270 newfile();
271 } else if (lcnt++ == numlines) {
272 newfile();
273 lcnt = 1;
274 }
275
276writeit:
277 /* Open output file if needed */
278 if (!file_open)
279 newfile();
280
281 /* Write out line */
282 if (write(ofd, bfr, len) != len)
283 err(EX_IOERR, "write");
284 }
285
286 /* EOF or error? */
287 if (ferror(infp))
288 err(EX_IOERR, "read");
289 else
290 exit(0);
291}
292
293/*
294 * newfile --
295 * Open a new output file.
296 */
297void
298newfile(void)
299{
300 long i, maxfiles, tfnum;
301 static long fnum;
302 static int defname;
303 static char *fpnt;
304
305 if (ofd == -1) {
306 if (fname[0] == '\0') {
307 fname[0] = 'x';
308 fpnt = fname + 1;
309 defname = 1;
310 } else {
311 fpnt = fname + strlen(fname);
312 defname = 0;
313 }
314 ofd = fileno(stdout);
315 }
316
317 /* maxfiles = 26^sufflen, but don't use libm. */
318 for (maxfiles = 1, i = 0; i < sufflen; i++)
319 if ((maxfiles *= 26) <= 0)
320 errx(EX_USAGE, "suffix is too long (max %ld)", i);
321
322 /*
323 * Hack to increase max files; original code wandered through
324 * magic characters.
325 */
326 if (fnum == maxfiles) {
327 if (!defname || fname[0] == 'z')
328 errx(EX_DATAERR, "too many files");
329 ++fname[0];
330 fnum = 0;
331 }
322 if (fnum == maxfiles)
323 errx(EX_DATAERR, "too many files");
332
333 /* Generate suffix of sufflen letters */
334 tfnum = fnum;
335 i = sufflen - 1;
336 do {
337 fpnt[i] = tfnum % 26 + 'a';
338 tfnum /= 26;
339 } while (i-- > 0);
340 fpnt[sufflen] = '\0';
341
342 ++fnum;
343 if (!freopen(fname, "w", stdout))
344 err(EX_IOERR, "%s", fname);
345 file_open = 1;
346}
347
348static void
349usage(void)
350{
351 (void)fprintf(stderr,
352"usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n");
353 (void)fprintf(stderr,
354" [file [prefix]]\n");
355 exit(EX_USAGE);
356}
324
325 /* Generate suffix of sufflen letters */
326 tfnum = fnum;
327 i = sufflen - 1;
328 do {
329 fpnt[i] = tfnum % 26 + 'a';
330 tfnum /= 26;
331 } while (i-- > 0);
332 fpnt[sufflen] = '\0';
333
334 ++fnum;
335 if (!freopen(fname, "w", stdout))
336 err(EX_IOERR, "%s", fname);
337 file_open = 1;
338}
339
340static void
341usage(void)
342{
343 (void)fprintf(stderr,
344"usage: split [-a sufflen] [-b byte_count] [-l line_count] [-p pattern]\n");
345 (void)fprintf(stderr,
346" [file [prefix]]\n");
347 exit(EX_USAGE);
348}