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