split.c revision 1.15 1 /* $NetBSD: split.c,v 1.15 2003/06/26 23:06:52 bjh21 Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)split.c 8.3 (Berkeley) 4/25/94";
45 #endif
46 __RCSID("$NetBSD: split.c,v 1.15 2003/06/26 23:06:52 bjh21 Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #define DEFLINE 1000 /* Default num lines per file. */
61
62 static int file_open; /* If a file open. */
63 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
64 static char *fname; /* File name prefix. */
65 static size_t sfxlen = 2; /* suffix length. */
66
67 int main(int, char **);
68 static void newfile(void);
69 static void split1(off_t);
70 static void split2(off_t);
71 static void usage(void) __attribute__((__noreturn__));
72 static size_t bigwrite(int, void const *, size_t);
73
74 int
75 main(int argc, char *argv[])
76 {
77 int ch;
78 char *ep, *p;
79 off_t bytecnt = 0; /* Byte count to split on. */
80 off_t numlines = 0; /* Line count to split on. */
81 size_t namelen;
82 long name_max;
83
84 while ((ch = getopt(argc, argv, "0123456789b:l:a:")) != -1)
85 switch (ch) {
86 case '0': case '1': case '2': case '3': case '4':
87 case '5': case '6': case '7': case '8': case '9':
88 /*
89 * Undocumented kludge: split was originally designed
90 * to take a number after a dash.
91 */
92 if (numlines == 0) {
93 p = argv[optind - 1];
94 if (p[0] == '-' && p[1] == ch && !p[2])
95 p++;
96 else
97 p = argv[optind] + 1;
98 numlines = strtoull(p, &ep, 10);
99 if (numlines == 0 || *ep != '\0')
100 errx(1, "%s: illegal line count.", p);
101 }
102 break;
103 case 'b': /* Byte count. */
104 if (!isdigit((unsigned char)optarg[0]) ||
105 (bytecnt = strtoull(optarg, &ep, 10)) == 0 ||
106 (*ep != '\0' && *ep != 'k' && *ep != 'm'))
107 errx(1, "%s: illegal byte count.", optarg);
108 if (*ep == 'k')
109 bytecnt *= 1024;
110 else if (*ep == 'm')
111 bytecnt *= 1024 * 1024;
112 break;
113 case 'l': /* Line count. */
114 if (numlines != 0)
115 usage();
116 if (!isdigit((unsigned char)optarg[0]) ||
117 (numlines = strtoull(optarg, &ep, 10)) == 0 ||
118 *ep != '\0')
119 errx(1, "%s: illegal line count.", optarg);
120 break;
121 case 'a': /* Suffix length. */
122 if (!isdigit((unsigned char)optarg[0]) ||
123 (sfxlen = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
124 *ep != '\0')
125 errx(1, "%s: illegal suffix length.", optarg);
126 break;
127 default:
128 usage();
129 }
130 argv += optind;
131 argc -= optind;
132
133 if (*argv != NULL) {
134 if (strcmp(*argv, "-") == 0)
135 ifd = STDIN_FILENO;
136 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
137 err(1, "%s", *argv);
138 ++argv;
139 }
140
141 errno = 0;
142 if ((name_max = pathconf(".", _PC_NAME_MAX)) == -1 &&
143 errno != 0)
144 err(EXIT_FAILURE, "pathconf");
145 if (*argv != NULL) {
146 namelen = strlen(*argv) + sfxlen;
147 if (name_max != -1 && namelen > name_max)
148 errx(EXIT_FAILURE, "Output file name too long");
149 if ((fname = malloc(namelen + 1)) == NULL)
150 err(EXIT_FAILURE, NULL);
151 (void)strcpy(fname, *argv++); /* File name prefix. */
152 } else {
153 if (name_max != -1 && 1 + sfxlen > name_max)
154 errx(EXIT_FAILURE, "Output file name too long");
155 if ((fname = malloc(sfxlen + 2)) == NULL)
156 err(EXIT_FAILURE, NULL);
157 fname[0] = '\0';
158 }
159
160 if (*argv != NULL)
161 usage();
162
163 if (numlines == 0)
164 numlines = DEFLINE;
165 else if (bytecnt)
166 usage();
167
168 if (ifd == -1) /* Stdin by default. */
169 ifd = 0;
170
171 if (bytecnt)
172 split1(bytecnt);
173 else
174 split2(numlines);
175
176 return 0;
177 }
178
179 /*
180 * split1 --
181 * Split the input by bytes.
182 */
183 static void
184 split1(off_t bytecnt)
185 {
186 off_t bcnt;
187 ssize_t dist, len;
188 char *C;
189 char bfr[MAXBSIZE];
190
191 for (bcnt = 0;;)
192 switch (len = read(ifd, bfr, MAXBSIZE)) {
193 case 0:
194 exit(0);
195 /* NOTREACHED */
196 case -1:
197 err(1, "read");
198 /* NOTREACHED */
199 default:
200 if (!file_open) {
201 newfile();
202 file_open = 1;
203 }
204 if (bcnt + len >= bytecnt) {
205 /* LINTED: bytecnt - bcnt <= len */
206 dist = bytecnt - bcnt;
207 if (bigwrite(ofd, bfr, dist) != dist)
208 err(1, "write");
209 len -= dist;
210 for (C = bfr + dist; len >= bytecnt;
211 /* LINTED: bytecnt <= len */
212 len -= bytecnt, C += bytecnt) {
213 newfile();
214 /* LINTED: as above */
215 if (bigwrite(ofd,
216 C, bytecnt) != bytecnt)
217 err(1, "write");
218 }
219 if (len) {
220 newfile();
221 /* LINTED: len >= 0 */
222 if (bigwrite(ofd, C, len) != len)
223 err(1, "write");
224 } else
225 file_open = 0;
226 bcnt = len;
227 } else {
228 bcnt += len;
229 /* LINTED: len >= 0 */
230 if (bigwrite(ofd, bfr, len) != len)
231 err(1, "write");
232 }
233 }
234 }
235
236 /*
237 * split2 --
238 * Split the input by lines.
239 */
240 static void
241 split2(off_t numlines)
242 {
243 off_t lcnt;
244 size_t bcnt;
245 ssize_t len;
246 char *Ce, *Cs;
247 char bfr[MAXBSIZE];
248
249 for (lcnt = 0;;)
250 switch (len = read(ifd, bfr, MAXBSIZE)) {
251 case 0:
252 exit(0);
253 /* NOTREACHED */
254 case -1:
255 err(1, "read");
256 /* NOTREACHED */
257 default:
258 if (!file_open) {
259 newfile();
260 file_open = 1;
261 }
262 for (Cs = Ce = bfr; len--; Ce++)
263 if (*Ce == '\n' && ++lcnt == numlines) {
264 bcnt = Ce - Cs + 1;
265 if (bigwrite(ofd, Cs, bcnt) != bcnt)
266 err(1, "write");
267 lcnt = 0;
268 Cs = Ce + 1;
269 if (len)
270 newfile();
271 else
272 file_open = 0;
273 }
274 if (Cs < Ce) {
275 bcnt = Ce - Cs;
276 if (bigwrite(ofd, Cs, bcnt) != bcnt)
277 err(1, "write");
278 }
279 }
280 }
281
282 /*
283 * newfile --
284 * Open a new output file.
285 */
286 static void
287 newfile(void)
288 {
289 static int fnum;
290 static int defname;
291 static char *fpnt;
292 int quot, i;
293
294 if (ofd == -1) {
295 if (fname[0] == '\0') {
296 fname[0] = 'x';
297 fpnt = fname + 1;
298 defname = 1;
299 } else {
300 fpnt = fname + strlen(fname);
301 defname = 0;
302 }
303 ofd = fileno(stdout);
304 }
305 /*
306 * Hack to increase max files; original code wandered through
307 * magic characters. Maximum files is 3 * 26 * 26 == 2028
308 */
309 fpnt[sfxlen] = '\0';
310 quot = fnum;
311 for (i = sfxlen - 1; i >= 0; i--) {
312 fpnt[i] = quot % 26 + 'a';
313 quot = quot / 26;
314 }
315 if (quot > 0) {
316 if (!defname || fname[0] == 'z')
317 errx(1, "too many files.");
318 ++fname[0];
319 fnum = 0;
320 }
321 ++fnum;
322 if (!freopen(fname, "w", stdout))
323 err(1, "%s", fname);
324 }
325
326 static size_t
327 bigwrite(int fd, const void *buf, size_t len)
328 {
329 const char *ptr = buf;
330 size_t sofar = 0;
331 ssize_t w;
332
333 while (len != 0) {
334 if ((w = write(fd, ptr, len)) == -1)
335 return sofar;
336 len -= w;
337 ptr += w;
338 sofar += w;
339 }
340 return sofar;
341 }
342
343
344 static void
345 usage(void)
346 {
347 (void)fprintf(stderr,
348 "Usage: %s [-b byte_count] [-l line_count] [-a suffix_length] "
349 "[file [prefix]]\n", getprogname());
350 exit(1);
351 }
352