split.c revision 1.10 1 /* $NetBSD: split.c,v 1.10 2003/06/10 16:32:54 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.10 2003/06/10 16:32:54 bjh21 Exp $");
47 #endif /* not lint */
48
49 #include <sys/param.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <fcntl.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #define DEFLINE 1000 /* Default num lines per file. */
60
61 static int file_open; /* If a file open. */
62 static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
63 static char fname[MAXPATHLEN]; /* File name prefix. */
64 static int sfxlen = 2; /* suffix length. */
65
66 int main(int, char **);
67 static void newfile(void);
68 static void split1(unsigned long long);
69 static void split2(unsigned long long);
70 static void usage(void) __attribute__((__noreturn__));
71 static unsigned long long bigwrite __P((int, const void *, unsigned long long));
72
73 int
74 main(int argc, char *argv[])
75 {
76 int ch;
77 char *ep, *p;
78 unsigned long long bytecnt = 0; /* Byte count to split on. */
79 unsigned long long numlines = 0;/* Line count to split on. */
80
81 while ((ch = getopt(argc, argv, "-0123456789b:l:a:")) != -1)
82 switch (ch) {
83 case '0': case '1': case '2': case '3': case '4':
84 case '5': case '6': case '7': case '8': case '9':
85 /*
86 * Undocumented kludge: split was originally designed
87 * to take a number after a dash.
88 */
89 if (numlines == 0) {
90 p = argv[optind - 1];
91 if (p[0] == '-' && p[1] == ch && !p[2])
92 numlines = strtol(++p, &ep, 10);
93 else
94 numlines =
95 strtol(argv[optind] + 1, &ep, 10);
96 if (numlines <= 0 || *ep)
97 errx(1,
98 "%s: illegal line count.", optarg);
99 }
100 break;
101 case '-': /* Undocumented: historic stdin flag. */
102 if (ifd != -1)
103 usage();
104 ifd = 0;
105 break;
106 case 'b': /* Byte count. */
107 if ((bytecnt = strtoull(optarg, &ep, 10)) <= 0 ||
108 (*ep != '\0' && *ep != 'k' && *ep != 'm'))
109 errx(1, "%s: illegal byte count.", optarg);
110 if (*ep == 'k')
111 bytecnt *= 1024;
112 else if (*ep == 'm')
113 bytecnt *= 1024 * 1024;
114 break;
115 case 'l': /* Line count. */
116 if (numlines != 0)
117 usage();
118 if ((numlines = strtoull(optarg, &ep, 10)) <= 0 || *ep)
119 errx(1, "%s: illegal line count.", optarg);
120 break;
121 case 'a': /* Suffix length. */
122 if ((sfxlen = strtol(optarg, &ep, 10)) <= 0 || *ep)
123 errx(1, "%s: illegal suffix length.", optarg);
124 break;
125 default:
126 usage();
127 }
128 argv += optind;
129 argc -= optind;
130
131 if (*argv != NULL)
132 if (ifd == -1) { /* Input file. */
133 if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
134 err(1, "%s", *argv);
135 ++argv;
136 }
137
138 if (*argv != NULL) {
139 if (strlen(*argv) + sfxlen > NAME_MAX)
140 errx(EXIT_FAILURE, "Output file name too long");
141 (void)strcpy(fname, *argv++); /* File name prefix. */
142 } else {
143 if (1 + sfxlen > NAME_MAX)
144 errx(EXIT_FAILURE, "Output file name too long");
145 }
146
147 if (*argv != NULL)
148 usage();
149
150 if (numlines == 0)
151 numlines = DEFLINE;
152 else if (bytecnt)
153 usage();
154
155 if (ifd == -1) /* Stdin by default. */
156 ifd = 0;
157
158 if (bytecnt) {
159 split1(bytecnt);
160 } else {
161 split2(numlines);
162 }
163 return 0;
164 }
165
166 /*
167 * split1 --
168 * Split the input by bytes.
169 */
170 static void
171 split1(unsigned long long bytecnt)
172 {
173 unsigned long long bcnt, dist;
174 ssize_t len;
175 char *C;
176 char bfr[MAXBSIZE];
177
178 for (bcnt = 0;;)
179 switch (len = read(ifd, bfr, MAXBSIZE)) {
180 case 0:
181 exit(0);
182 case -1:
183 err(1, "read");
184 /* NOTREACHED */
185 default:
186 if (!file_open) {
187 newfile();
188 file_open = 1;
189 }
190 if (bcnt + len >= bytecnt) {
191 dist = bytecnt - bcnt;
192 if (bigwrite(ofd, bfr, dist) != dist)
193 err(1, "write");
194 len -= dist;
195 for (C = bfr + dist; len >= bytecnt;
196 len -= bytecnt, C += bytecnt) {
197 newfile();
198 if (bigwrite(ofd,
199 C, (int)bytecnt) != bytecnt)
200 err(1, "write");
201 }
202 if (len) {
203 newfile();
204 if (bigwrite(ofd, C, len) != len)
205 err(1, "write");
206 } else
207 file_open = 0;
208 bcnt = len;
209 } else {
210 bcnt += len;
211 if (bigwrite(ofd, bfr, len) != len)
212 err(1, "write");
213 }
214 }
215 }
216
217 /*
218 * split2 --
219 * Split the input by lines.
220 */
221 static void
222 split2(unsigned long long numlines)
223 {
224 unsigned long long lcnt, bcnt;
225 ssize_t len;
226 char *Ce, *Cs;
227 char bfr[MAXBSIZE];
228
229 for (lcnt = 0;;)
230 switch (len = read(ifd, bfr, MAXBSIZE)) {
231 case 0:
232 exit(0);
233 case -1:
234 err(1, "read");
235 /* NOTREACHED */
236 default:
237 if (!file_open) {
238 newfile();
239 file_open = 1;
240 }
241 for (Cs = Ce = bfr; len--; Ce++)
242 if (*Ce == '\n' && ++lcnt == numlines) {
243 bcnt = Ce - Cs + 1;
244 if (bigwrite(ofd, Cs, bcnt) != bcnt)
245 err(1, "write");
246 lcnt = 0;
247 Cs = Ce + 1;
248 if (len)
249 newfile();
250 else
251 file_open = 0;
252 }
253 if (Cs < Ce) {
254 bcnt = Ce - Cs;
255 if (bigwrite(ofd, Cs, bcnt) != bcnt)
256 err(1, "write");
257 }
258 }
259 }
260
261 /*
262 * newfile --
263 * Open a new output file.
264 */
265 static void
266 newfile(void)
267 {
268 static int fnum;
269 static int defname;
270 static char *fpnt;
271 int quot, i;
272
273 if (ofd == -1) {
274 if (fname[0] == '\0') {
275 fname[0] = 'x';
276 fpnt = fname + 1;
277 defname = 1;
278 } else {
279 fpnt = fname + strlen(fname);
280 defname = 0;
281 }
282 ofd = fileno(stdout);
283 }
284 /*
285 * Hack to increase max files; original code wandered through
286 * magic characters. Maximum files is 3 * 26 * 26 == 2028
287 */
288 fpnt[sfxlen] = '\0';
289 quot = fnum;
290 for (i = sfxlen - 1; i >= 0; i--) {
291 fpnt[i] = quot % 26 + 'a';
292 quot = quot / 26;
293 }
294 if (quot > 0) {
295 if (!defname || fname[0] == 'z')
296 errx(1, "too many files.");
297 ++fname[0];
298 fnum = 0;
299 }
300 ++fnum;
301 if (!freopen(fname, "w", stdout))
302 err(1, "%s", fname);
303 }
304
305 static unsigned long long
306 bigwrite(int fd, const void *buf, unsigned long long len)
307 {
308 const char *ptr = buf;
309 unsigned long long sofar = 0;
310
311 while (len != 0) {
312 ssize_t w, nw = (len > INT_MAX) ? INT_MAX : (ssize_t)len;
313 if ((w = write(fd, ptr, nw)) == -1)
314 return sofar;
315 len -= w;
316 ptr += w;
317 sofar += w;
318 }
319 return sofar;
320 }
321
322
323 static void
324 usage(void)
325 {
326 (void)fprintf(stderr,
327 "Usage: %s [-b byte_count] [-l line_count] [-a suffix_length] "
328 "[file [prefix]]\n", getprogname());
329 exit(1);
330 }
331