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