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