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