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