split.c revision 1.12 1 /* $NetBSD: split.c,v 1.12 2003/06/24 00:09:26 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.12 2003/06/24 00:09:26 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 int sfxlen = 2; /* suffix length. */
66
67 int main(int, char **);
68 static void newfile(void);
69 static void split1(unsigned long long);
70 static void split2(unsigned long long);
71 static void usage(void) __attribute__((__noreturn__));
72 static unsigned long long bigwrite __P((int, const void *, unsigned long long));
73
74 int
75 main(int argc, char *argv[])
76 {
77 int ch;
78 char *ep, *p;
79 unsigned long long bytecnt = 0; /* Byte count to split on. */
80 unsigned long long 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 numlines = strtol(++p, &ep, 10);
96 else
97 numlines =
98 strtol(argv[optind] + 1, &ep, 10);
99 if (numlines <= 0 || *ep)
100 errx(1,
101 "%s: illegal line count.", optarg);
102 }
103 break;
104 case '-': /* stdin flag. */
105 if (ifd != -1)
106 usage();
107 ifd = 0;
108 break;
109 case 'b': /* Byte count. */
110 if ((bytecnt = strtoull(optarg, &ep, 10)) <= 0 ||
111 (*ep != '\0' && *ep != 'k' && *ep != 'm'))
112 errx(1, "%s: illegal byte count.", optarg);
113 if (*ep == 'k')
114 bytecnt *= 1024;
115 else if (*ep == 'm')
116 bytecnt *= 1024 * 1024;
117 break;
118 case 'l': /* Line count. */
119 if (numlines != 0)
120 usage();
121 if ((numlines = strtoull(optarg, &ep, 10)) <= 0 || *ep)
122 errx(1, "%s: illegal line count.", optarg);
123 break;
124 case 'a': /* Suffix length. */
125 if ((sfxlen = strtol(optarg, &ep, 10)) <= 0 || *ep)
126 errx(1, "%s: illegal suffix length.", optarg);
127 break;
128 default:
129 usage();
130 }
131 argv += optind;
132 argc -= optind;
133
134 if (*argv != NULL)
135 if (ifd == -1) { /* Input file. */
136 if (strcmp(*argv, "-") == 0)
137 ifd = STDIN_FILENO;
138 else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
139 err(1, "%s", *argv);
140 ++argv;
141 }
142
143 errno = 0;
144 if ((name_max = pathconf(".", _PC_NAME_MAX)) == -1 &&
145 errno != 0)
146 err(EXIT_FAILURE, "pathconf");
147 if (*argv != NULL) {
148 namelen = strlen(*argv) + sfxlen;
149 if (name_max != -1 && namelen > name_max)
150 errx(EXIT_FAILURE, "Output file name too long");
151 if ((fname = malloc(namelen + 1)) == NULL)
152 err(EXIT_FAILURE, NULL);
153 (void)strcpy(fname, *argv++); /* File name prefix. */
154 } else {
155 if (name_max != -1 && 1 + sfxlen > name_max)
156 errx(EXIT_FAILURE, "Output file name too long");
157 if ((fname = malloc(sfxlen + 2)) == NULL)
158 err(EXIT_FAILURE, NULL);
159 fname[0] = '\0';
160 }
161
162 if (*argv != NULL)
163 usage();
164
165 if (numlines == 0)
166 numlines = DEFLINE;
167 else if (bytecnt)
168 usage();
169
170 if (ifd == -1) /* Stdin by default. */
171 ifd = 0;
172
173 if (bytecnt) {
174 split1(bytecnt);
175 } else {
176 split2(numlines);
177 }
178 return 0;
179 }
180
181 /*
182 * split1 --
183 * Split the input by bytes.
184 */
185 static void
186 split1(unsigned long long bytecnt)
187 {
188 unsigned long long bcnt, dist;
189 ssize_t len;
190 char *C;
191 char bfr[MAXBSIZE];
192
193 for (bcnt = 0;;)
194 switch (len = read(ifd, bfr, MAXBSIZE)) {
195 case 0:
196 exit(0);
197 case -1:
198 err(1, "read");
199 /* NOTREACHED */
200 default:
201 if (!file_open) {
202 newfile();
203 file_open = 1;
204 }
205 if (bcnt + len >= bytecnt) {
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 len -= bytecnt, C += bytecnt) {
212 newfile();
213 if (bigwrite(ofd,
214 C, (int)bytecnt) != bytecnt)
215 err(1, "write");
216 }
217 if (len) {
218 newfile();
219 if (bigwrite(ofd, C, len) != len)
220 err(1, "write");
221 } else
222 file_open = 0;
223 bcnt = len;
224 } else {
225 bcnt += len;
226 if (bigwrite(ofd, bfr, len) != len)
227 err(1, "write");
228 }
229 }
230 }
231
232 /*
233 * split2 --
234 * Split the input by lines.
235 */
236 static void
237 split2(unsigned long long numlines)
238 {
239 unsigned long long lcnt, bcnt;
240 ssize_t len;
241 char *Ce, *Cs;
242 char bfr[MAXBSIZE];
243
244 for (lcnt = 0;;)
245 switch (len = read(ifd, bfr, MAXBSIZE)) {
246 case 0:
247 exit(0);
248 case -1:
249 err(1, "read");
250 /* NOTREACHED */
251 default:
252 if (!file_open) {
253 newfile();
254 file_open = 1;
255 }
256 for (Cs = Ce = bfr; len--; Ce++)
257 if (*Ce == '\n' && ++lcnt == numlines) {
258 bcnt = Ce - Cs + 1;
259 if (bigwrite(ofd, Cs, bcnt) != bcnt)
260 err(1, "write");
261 lcnt = 0;
262 Cs = Ce + 1;
263 if (len)
264 newfile();
265 else
266 file_open = 0;
267 }
268 if (Cs < Ce) {
269 bcnt = Ce - Cs;
270 if (bigwrite(ofd, Cs, bcnt) != bcnt)
271 err(1, "write");
272 }
273 }
274 }
275
276 /*
277 * newfile --
278 * Open a new output file.
279 */
280 static void
281 newfile(void)
282 {
283 static int fnum;
284 static int defname;
285 static char *fpnt;
286 int quot, i;
287
288 if (ofd == -1) {
289 if (fname[0] == '\0') {
290 fname[0] = 'x';
291 fpnt = fname + 1;
292 defname = 1;
293 } else {
294 fpnt = fname + strlen(fname);
295 defname = 0;
296 }
297 ofd = fileno(stdout);
298 }
299 /*
300 * Hack to increase max files; original code wandered through
301 * magic characters. Maximum files is 3 * 26 * 26 == 2028
302 */
303 fpnt[sfxlen] = '\0';
304 quot = fnum;
305 for (i = sfxlen - 1; i >= 0; i--) {
306 fpnt[i] = quot % 26 + 'a';
307 quot = quot / 26;
308 }
309 if (quot > 0) {
310 if (!defname || fname[0] == 'z')
311 errx(1, "too many files.");
312 ++fname[0];
313 fnum = 0;
314 }
315 ++fnum;
316 if (!freopen(fname, "w", stdout))
317 err(1, "%s", fname);
318 }
319
320 static unsigned long long
321 bigwrite(int fd, const void *buf, unsigned long long len)
322 {
323 const char *ptr = buf;
324 unsigned long long sofar = 0;
325
326 while (len != 0) {
327 ssize_t w, nw = (len > INT_MAX) ? INT_MAX : (ssize_t)len;
328 if ((w = write(fd, ptr, nw)) == -1)
329 return sofar;
330 len -= w;
331 ptr += w;
332 sofar += w;
333 }
334 return sofar;
335 }
336
337
338 static void
339 usage(void)
340 {
341 (void)fprintf(stderr,
342 "Usage: %s [-b byte_count] [-l line_count] [-a suffix_length] "
343 "[file [prefix]]\n", getprogname());
344 exit(1);
345 }
346