split.c revision 1.3 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1987 Regents of the University of California.
3 1.1 cgd * 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 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1987 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.2 mycroft /*static char sccsid[] = "from: @(#)split.c 4.8 (Berkeley) 6/1/90";*/
42 1.3 cgd static char rcsid[] = "$Id: split.c,v 1.3 1994/04/06 00:04:09 cgd Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/file.h>
47 1.1 cgd #include <stdio.h>
48 1.1 cgd #include <ctype.h>
49 1.1 cgd
50 1.1 cgd #define DEFLINE 1000 /* default num lines per file */
51 1.1 cgd #define ERR -1 /* general error */
52 1.1 cgd #define NO 0 /* no/false */
53 1.1 cgd #define OK 0 /* okay exit */
54 1.1 cgd #define YES 1 /* yes/true */
55 1.1 cgd
56 1.1 cgd static long bytecnt, /* byte count to split on */
57 1.1 cgd numlines; /* lines in each file */
58 1.1 cgd static int ifd = ERR, /* input file descriptor */
59 1.1 cgd ofd = ERR; /* output file descriptor */
60 1.1 cgd static short file_open; /* if a file open */
61 1.1 cgd static char bfr[MAXBSIZE], /* I/O buffer */
62 1.1 cgd fname[MAXPATHLEN]; /* file name */
63 1.1 cgd
64 1.1 cgd main(argc, argv)
65 1.1 cgd int argc;
66 1.1 cgd char **argv;
67 1.1 cgd {
68 1.1 cgd register int cnt;
69 1.1 cgd long atol();
70 1.1 cgd char *strcpy();
71 1.1 cgd
72 1.1 cgd for (cnt = 1; cnt < argc; ++cnt) {
73 1.1 cgd if (argv[cnt][0] == '-')
74 1.1 cgd switch(argv[cnt][1]) {
75 1.1 cgd case 0: /* stdin by request */
76 1.1 cgd if (ifd != ERR)
77 1.1 cgd usage();
78 1.1 cgd ifd = 0;
79 1.1 cgd break;
80 1.1 cgd case 'b': /* byte count split */
81 1.1 cgd if (numlines)
82 1.1 cgd usage();
83 1.3 cgd if (!argv[cnt][2]) {
84 1.3 cgd if (++cnt >= argc)
85 1.3 cgd usage();
86 1.3 cgd bytecnt = atol(argv[cnt]);
87 1.3 cgd } else
88 1.1 cgd bytecnt = atol(argv[cnt] + 2);
89 1.1 cgd if (bytecnt <= 0) {
90 1.1 cgd fputs("split: byte count must be greater than zero.\n", stderr);
91 1.1 cgd usage();
92 1.1 cgd }
93 1.1 cgd break;
94 1.1 cgd default:
95 1.1 cgd if (!isdigit(argv[cnt][1]) || bytecnt)
96 1.1 cgd usage();
97 1.1 cgd if ((numlines = atol(argv[cnt] + 1)) <= 0) {
98 1.1 cgd fputs("split: line count must be greater than zero.\n", stderr);
99 1.1 cgd usage();
100 1.1 cgd }
101 1.1 cgd break;
102 1.1 cgd }
103 1.1 cgd else if (ifd == ERR) { /* input file */
104 1.1 cgd if ((ifd = open(argv[cnt], O_RDONLY, 0)) < 0) {
105 1.1 cgd perror(argv[cnt]);
106 1.1 cgd exit(1);
107 1.1 cgd }
108 1.1 cgd }
109 1.1 cgd else if (!*fname) /* output file prefix */
110 1.1 cgd strcpy(fname, argv[cnt]);
111 1.1 cgd else
112 1.1 cgd usage();
113 1.1 cgd }
114 1.1 cgd if (ifd == ERR) /* stdin by default */
115 1.1 cgd ifd = 0;
116 1.1 cgd if (bytecnt)
117 1.1 cgd split1();
118 1.1 cgd if (!numlines)
119 1.1 cgd numlines = DEFLINE;
120 1.1 cgd split2();
121 1.1 cgd exit(0);
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd /*
125 1.1 cgd * split1 --
126 1.1 cgd * split by bytes
127 1.1 cgd */
128 1.1 cgd split1()
129 1.1 cgd {
130 1.1 cgd register long bcnt;
131 1.1 cgd register int dist, len;
132 1.1 cgd register char *C;
133 1.1 cgd
134 1.1 cgd for (bcnt = 0;;)
135 1.1 cgd switch(len = read(ifd, bfr, MAXBSIZE)) {
136 1.1 cgd case 0:
137 1.1 cgd exit(OK);
138 1.1 cgd case ERR:
139 1.1 cgd perror("read");
140 1.1 cgd exit(1);
141 1.1 cgd default:
142 1.1 cgd if (!file_open) {
143 1.1 cgd newfile();
144 1.1 cgd file_open = YES;
145 1.1 cgd }
146 1.1 cgd if (bcnt + len >= bytecnt) {
147 1.1 cgd dist = bytecnt - bcnt;
148 1.1 cgd if (write(ofd, bfr, dist) != dist)
149 1.1 cgd wrerror();
150 1.1 cgd len -= dist;
151 1.1 cgd for (C = bfr + dist; len >= bytecnt; len -= bytecnt, C += bytecnt) {
152 1.1 cgd newfile();
153 1.1 cgd if (write(ofd, C, (int)bytecnt) != bytecnt)
154 1.1 cgd wrerror();
155 1.1 cgd }
156 1.1 cgd if (len) {
157 1.1 cgd newfile();
158 1.1 cgd if (write(ofd, C, len) != len)
159 1.1 cgd wrerror();
160 1.1 cgd }
161 1.1 cgd else
162 1.1 cgd file_open = NO;
163 1.1 cgd bcnt = len;
164 1.1 cgd }
165 1.1 cgd else {
166 1.1 cgd bcnt += len;
167 1.1 cgd if (write(ofd, bfr, len) != len)
168 1.1 cgd wrerror();
169 1.1 cgd }
170 1.1 cgd }
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * split2 --
175 1.1 cgd * split by lines
176 1.1 cgd */
177 1.1 cgd split2()
178 1.1 cgd {
179 1.1 cgd register char *Ce, *Cs;
180 1.1 cgd register long lcnt;
181 1.1 cgd register int len, bcnt;
182 1.1 cgd
183 1.1 cgd for (lcnt = 0;;)
184 1.1 cgd switch(len = read(ifd, bfr, MAXBSIZE)) {
185 1.1 cgd case 0:
186 1.1 cgd exit(0);
187 1.1 cgd case ERR:
188 1.1 cgd perror("read");
189 1.1 cgd exit(1);
190 1.1 cgd default:
191 1.1 cgd if (!file_open) {
192 1.1 cgd newfile();
193 1.1 cgd file_open = YES;
194 1.1 cgd }
195 1.1 cgd for (Cs = Ce = bfr; len--; Ce++)
196 1.1 cgd if (*Ce == '\n' && ++lcnt == numlines) {
197 1.1 cgd bcnt = Ce - Cs + 1;
198 1.1 cgd if (write(ofd, Cs, bcnt) != bcnt)
199 1.1 cgd wrerror();
200 1.1 cgd lcnt = 0;
201 1.1 cgd Cs = Ce + 1;
202 1.1 cgd if (len)
203 1.1 cgd newfile();
204 1.1 cgd else
205 1.1 cgd file_open = NO;
206 1.1 cgd }
207 1.1 cgd if (Cs < Ce) {
208 1.1 cgd bcnt = Ce - Cs;
209 1.1 cgd if (write(ofd, Cs, bcnt) != bcnt)
210 1.1 cgd wrerror();
211 1.1 cgd }
212 1.1 cgd }
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd /*
216 1.1 cgd * newfile --
217 1.1 cgd * open a new file
218 1.1 cgd */
219 1.1 cgd newfile()
220 1.1 cgd {
221 1.1 cgd static long fnum;
222 1.1 cgd static short defname;
223 1.1 cgd static char *fpnt;
224 1.1 cgd
225 1.1 cgd if (ofd == ERR) {
226 1.1 cgd if (fname[0]) {
227 1.1 cgd fpnt = fname + strlen(fname);
228 1.1 cgd defname = NO;
229 1.1 cgd }
230 1.1 cgd else {
231 1.1 cgd fname[0] = 'x';
232 1.1 cgd fpnt = fname + 1;
233 1.1 cgd defname = YES;
234 1.1 cgd }
235 1.1 cgd ofd = fileno(stdout);
236 1.1 cgd }
237 1.1 cgd /*
238 1.1 cgd * hack to increase max files; original code just wandered through
239 1.1 cgd * magic characters. Maximum files is 3 * 26 * 26 == 2028
240 1.1 cgd */
241 1.1 cgd #define MAXFILES 676
242 1.1 cgd if (fnum == MAXFILES) {
243 1.1 cgd if (!defname || fname[0] == 'z') {
244 1.1 cgd fputs("split: too many files.\n", stderr);
245 1.1 cgd exit(1);
246 1.1 cgd }
247 1.1 cgd ++fname[0];
248 1.1 cgd fnum = 0;
249 1.1 cgd }
250 1.1 cgd fpnt[0] = fnum / 26 + 'a';
251 1.1 cgd fpnt[1] = fnum % 26 + 'a';
252 1.1 cgd ++fnum;
253 1.1 cgd if (!freopen(fname, "w", stdout)) {
254 1.1 cgd fprintf(stderr, "split: unable to write to %s.\n", fname);
255 1.1 cgd exit(ERR);
256 1.1 cgd }
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd /*
260 1.1 cgd * usage --
261 1.1 cgd * print usage message and die
262 1.1 cgd */
263 1.1 cgd usage()
264 1.1 cgd {
265 1.1 cgd fputs("usage: split [-] [-#] [-b byte_count] [file [prefix]]\n", stderr);
266 1.1 cgd exit(1);
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd /*
270 1.1 cgd * wrerror --
271 1.1 cgd * write error
272 1.1 cgd */
273 1.1 cgd wrerror()
274 1.1 cgd {
275 1.1 cgd perror("split: write");
276 1.1 cgd exit(1);
277 1.1 cgd }
278