split.c revision 1.2 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.2 mycroft static char rcsid[] = "$Id: split.c,v 1.2 1993/08/01 18:08:21 mycroft 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.1 cgd if (!argv[cnt][2])
84 1.1 cgd bytecnt = atol(argv[++cnt]);
85 1.1 cgd else
86 1.1 cgd bytecnt = atol(argv[cnt] + 2);
87 1.1 cgd if (bytecnt <= 0) {
88 1.1 cgd fputs("split: byte count must be greater than zero.\n", stderr);
89 1.1 cgd usage();
90 1.1 cgd }
91 1.1 cgd break;
92 1.1 cgd default:
93 1.1 cgd if (!isdigit(argv[cnt][1]) || bytecnt)
94 1.1 cgd usage();
95 1.1 cgd if ((numlines = atol(argv[cnt] + 1)) <= 0) {
96 1.1 cgd fputs("split: line count must be greater than zero.\n", stderr);
97 1.1 cgd usage();
98 1.1 cgd }
99 1.1 cgd break;
100 1.1 cgd }
101 1.1 cgd else if (ifd == ERR) { /* input file */
102 1.1 cgd if ((ifd = open(argv[cnt], O_RDONLY, 0)) < 0) {
103 1.1 cgd perror(argv[cnt]);
104 1.1 cgd exit(1);
105 1.1 cgd }
106 1.1 cgd }
107 1.1 cgd else if (!*fname) /* output file prefix */
108 1.1 cgd strcpy(fname, argv[cnt]);
109 1.1 cgd else
110 1.1 cgd usage();
111 1.1 cgd }
112 1.1 cgd if (ifd == ERR) /* stdin by default */
113 1.1 cgd ifd = 0;
114 1.1 cgd if (bytecnt)
115 1.1 cgd split1();
116 1.1 cgd if (!numlines)
117 1.1 cgd numlines = DEFLINE;
118 1.1 cgd split2();
119 1.1 cgd exit(0);
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd /*
123 1.1 cgd * split1 --
124 1.1 cgd * split by bytes
125 1.1 cgd */
126 1.1 cgd split1()
127 1.1 cgd {
128 1.1 cgd register long bcnt;
129 1.1 cgd register int dist, len;
130 1.1 cgd register char *C;
131 1.1 cgd
132 1.1 cgd for (bcnt = 0;;)
133 1.1 cgd switch(len = read(ifd, bfr, MAXBSIZE)) {
134 1.1 cgd case 0:
135 1.1 cgd exit(OK);
136 1.1 cgd case ERR:
137 1.1 cgd perror("read");
138 1.1 cgd exit(1);
139 1.1 cgd default:
140 1.1 cgd if (!file_open) {
141 1.1 cgd newfile();
142 1.1 cgd file_open = YES;
143 1.1 cgd }
144 1.1 cgd if (bcnt + len >= bytecnt) {
145 1.1 cgd dist = bytecnt - bcnt;
146 1.1 cgd if (write(ofd, bfr, dist) != dist)
147 1.1 cgd wrerror();
148 1.1 cgd len -= dist;
149 1.1 cgd for (C = bfr + dist; len >= bytecnt; len -= bytecnt, C += bytecnt) {
150 1.1 cgd newfile();
151 1.1 cgd if (write(ofd, C, (int)bytecnt) != bytecnt)
152 1.1 cgd wrerror();
153 1.1 cgd }
154 1.1 cgd if (len) {
155 1.1 cgd newfile();
156 1.1 cgd if (write(ofd, C, len) != len)
157 1.1 cgd wrerror();
158 1.1 cgd }
159 1.1 cgd else
160 1.1 cgd file_open = NO;
161 1.1 cgd bcnt = len;
162 1.1 cgd }
163 1.1 cgd else {
164 1.1 cgd bcnt += len;
165 1.1 cgd if (write(ofd, bfr, len) != len)
166 1.1 cgd wrerror();
167 1.1 cgd }
168 1.1 cgd }
169 1.1 cgd }
170 1.1 cgd
171 1.1 cgd /*
172 1.1 cgd * split2 --
173 1.1 cgd * split by lines
174 1.1 cgd */
175 1.1 cgd split2()
176 1.1 cgd {
177 1.1 cgd register char *Ce, *Cs;
178 1.1 cgd register long lcnt;
179 1.1 cgd register int len, bcnt;
180 1.1 cgd
181 1.1 cgd for (lcnt = 0;;)
182 1.1 cgd switch(len = read(ifd, bfr, MAXBSIZE)) {
183 1.1 cgd case 0:
184 1.1 cgd exit(0);
185 1.1 cgd case ERR:
186 1.1 cgd perror("read");
187 1.1 cgd exit(1);
188 1.1 cgd default:
189 1.1 cgd if (!file_open) {
190 1.1 cgd newfile();
191 1.1 cgd file_open = YES;
192 1.1 cgd }
193 1.1 cgd for (Cs = Ce = bfr; len--; Ce++)
194 1.1 cgd if (*Ce == '\n' && ++lcnt == numlines) {
195 1.1 cgd bcnt = Ce - Cs + 1;
196 1.1 cgd if (write(ofd, Cs, bcnt) != bcnt)
197 1.1 cgd wrerror();
198 1.1 cgd lcnt = 0;
199 1.1 cgd Cs = Ce + 1;
200 1.1 cgd if (len)
201 1.1 cgd newfile();
202 1.1 cgd else
203 1.1 cgd file_open = NO;
204 1.1 cgd }
205 1.1 cgd if (Cs < Ce) {
206 1.1 cgd bcnt = Ce - Cs;
207 1.1 cgd if (write(ofd, Cs, bcnt) != bcnt)
208 1.1 cgd wrerror();
209 1.1 cgd }
210 1.1 cgd }
211 1.1 cgd }
212 1.1 cgd
213 1.1 cgd /*
214 1.1 cgd * newfile --
215 1.1 cgd * open a new file
216 1.1 cgd */
217 1.1 cgd newfile()
218 1.1 cgd {
219 1.1 cgd static long fnum;
220 1.1 cgd static short defname;
221 1.1 cgd static char *fpnt;
222 1.1 cgd
223 1.1 cgd if (ofd == ERR) {
224 1.1 cgd if (fname[0]) {
225 1.1 cgd fpnt = fname + strlen(fname);
226 1.1 cgd defname = NO;
227 1.1 cgd }
228 1.1 cgd else {
229 1.1 cgd fname[0] = 'x';
230 1.1 cgd fpnt = fname + 1;
231 1.1 cgd defname = YES;
232 1.1 cgd }
233 1.1 cgd ofd = fileno(stdout);
234 1.1 cgd }
235 1.1 cgd /*
236 1.1 cgd * hack to increase max files; original code just wandered through
237 1.1 cgd * magic characters. Maximum files is 3 * 26 * 26 == 2028
238 1.1 cgd */
239 1.1 cgd #define MAXFILES 676
240 1.1 cgd if (fnum == MAXFILES) {
241 1.1 cgd if (!defname || fname[0] == 'z') {
242 1.1 cgd fputs("split: too many files.\n", stderr);
243 1.1 cgd exit(1);
244 1.1 cgd }
245 1.1 cgd ++fname[0];
246 1.1 cgd fnum = 0;
247 1.1 cgd }
248 1.1 cgd fpnt[0] = fnum / 26 + 'a';
249 1.1 cgd fpnt[1] = fnum % 26 + 'a';
250 1.1 cgd ++fnum;
251 1.1 cgd if (!freopen(fname, "w", stdout)) {
252 1.1 cgd fprintf(stderr, "split: unable to write to %s.\n", fname);
253 1.1 cgd exit(ERR);
254 1.1 cgd }
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd /*
258 1.1 cgd * usage --
259 1.1 cgd * print usage message and die
260 1.1 cgd */
261 1.1 cgd usage()
262 1.1 cgd {
263 1.1 cgd fputs("usage: split [-] [-#] [-b byte_count] [file [prefix]]\n", stderr);
264 1.1 cgd exit(1);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * wrerror --
269 1.1 cgd * write error
270 1.1 cgd */
271 1.1 cgd wrerror()
272 1.1 cgd {
273 1.1 cgd perror("split: write");
274 1.1 cgd exit(1);
275 1.1 cgd }
276