Commit | Line | Data |
---|---|---|
86bcea21 KB |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
5e8b0e60 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
86bcea21 KB |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
20 | "@(#) Copyright (c) 1987 Regents of the University of California.\n\ | |
21 | All rights reserved.\n"; | |
22 | #endif /* not lint */ | |
23 | ||
24 | #ifndef lint | |
4bf5591e | 25 | static char sccsid[] = "@(#)split.c 4.7 (Berkeley) %G%"; |
86bcea21 | 26 | #endif /* not lint */ |
460f3462 KB |
27 | |
28 | #include <sys/param.h> | |
29 | #include <sys/file.h> | |
f37d3151 | 30 | #include <stdio.h> |
460f3462 KB |
31 | #include <ctype.h> |
32 | ||
33 | #define DEFLINE 1000 /* default num lines per file */ | |
34 | #define ERR -1 /* general error */ | |
460f3462 KB |
35 | #define NO 0 /* no/false */ |
36 | #define OK 0 /* okay exit */ | |
37 | #define YES 1 /* yes/true */ | |
f37d3151 | 38 | |
460f3462 KB |
39 | static long bytecnt, /* byte count to split on */ |
40 | numlines; /* lines in each file */ | |
41 | static int ifd = ERR, /* input file descriptor */ | |
42 | ofd = ERR; /* output file descriptor */ | |
43 | static short file_open; /* if a file open */ | |
44 | static char bfr[MAXBSIZE], /* I/O buffer */ | |
45 | fname[MAXPATHLEN]; /* file name */ | |
f37d3151 | 46 | |
86bcea21 | 47 | main(argc, argv) |
4bf5591e KB |
48 | int argc; |
49 | char **argv; | |
f37d3151 | 50 | { |
4bf5591e KB |
51 | register int cnt; |
52 | long atol(); | |
53 | char *strcpy(); | |
f37d3151 | 54 | |
86bcea21 | 55 | for (cnt = 1; cnt < argc; ++cnt) { |
460f3462 KB |
56 | if (argv[cnt][0] == '-') |
57 | switch(argv[cnt][1]) { | |
86bcea21 KB |
58 | case 0: /* stdin by request */ |
59 | if (ifd != ERR) | |
60 | usage(); | |
61 | ifd = 0; | |
62 | break; | |
63 | case 'b': /* byte count split */ | |
64 | if (numlines) | |
65 | usage(); | |
66 | if (!argv[cnt][2]) | |
67 | bytecnt = atol(argv[++cnt]); | |
68 | else | |
69 | bytecnt = atol(argv[cnt] + 2); | |
70 | if (bytecnt <= 0) { | |
71 | fputs("split: byte count must be greater than zero.\n", stderr); | |
72 | usage(); | |
73 | } | |
74 | break; | |
75 | default: | |
76 | if (!isdigit(argv[cnt][1]) || bytecnt) | |
77 | usage(); | |
78 | if ((numlines = atol(argv[cnt] + 1)) <= 0) { | |
79 | fputs("split: line count must be greater than zero.\n", stderr); | |
80 | usage(); | |
81 | } | |
82 | break; | |
460f3462 KB |
83 | } |
84 | else if (ifd == ERR) { /* input file */ | |
86bcea21 | 85 | if ((ifd = open(argv[cnt], O_RDONLY, 0)) < 0) { |
460f3462 | 86 | perror(argv[cnt]); |
4bf5591e | 87 | exit(1); |
f37d3151 | 88 | } |
f37d3151 | 89 | } |
460f3462 | 90 | else if (!*fname) /* output file prefix */ |
86bcea21 | 91 | strcpy(fname, argv[cnt]); |
460f3462 KB |
92 | else |
93 | usage(); | |
94 | } | |
95 | if (ifd == ERR) /* stdin by default */ | |
96 | ifd = 0; | |
97 | if (bytecnt) | |
98 | split1(); | |
99 | if (!numlines) | |
100 | numlines = DEFLINE; | |
101 | split2(); | |
4bf5591e | 102 | exit(0); |
460f3462 KB |
103 | } |
104 | ||
105 | /* | |
106 | * split1 -- | |
107 | * split by bytes | |
108 | */ | |
460f3462 KB |
109 | split1() |
110 | { | |
4bf5591e KB |
111 | register long bcnt; |
112 | register int dist, len; | |
113 | register char *C; | |
460f3462 KB |
114 | |
115 | for (bcnt = 0;;) | |
86bcea21 KB |
116 | switch(len = read(ifd, bfr, MAXBSIZE)) { |
117 | case 0: | |
118 | exit(OK); | |
119 | case ERR: | |
120 | perror("read"); | |
4bf5591e | 121 | exit(1); |
86bcea21 KB |
122 | default: |
123 | if (!file_open) { | |
124 | newfile(); | |
125 | file_open = YES; | |
126 | } | |
127 | if (bcnt + len >= bytecnt) { | |
128 | dist = bytecnt - bcnt; | |
4bf5591e KB |
129 | if (write(ofd, bfr, dist) != dist) |
130 | wrerror(); | |
86bcea21 KB |
131 | len -= dist; |
132 | for (C = bfr + dist; len >= bytecnt; len -= bytecnt, C += bytecnt) { | |
460f3462 | 133 | newfile(); |
4bf5591e KB |
134 | if (write(ofd, C, (int)bytecnt) != bytecnt) |
135 | wrerror(); | |
460f3462 | 136 | } |
86bcea21 KB |
137 | if (len) { |
138 | newfile(); | |
4bf5591e KB |
139 | if (write(ofd, C, len) != len) |
140 | wrerror(); | |
460f3462 | 141 | } |
86bcea21 KB |
142 | else |
143 | file_open = NO; | |
144 | bcnt = len; | |
145 | } | |
146 | else { | |
147 | bcnt += len; | |
4bf5591e KB |
148 | if (write(ofd, bfr, len) != len) |
149 | wrerror(); | |
86bcea21 | 150 | } |
460f3462 KB |
151 | } |
152 | } | |
153 | ||
154 | /* | |
155 | * split2 -- | |
156 | * split by lines | |
157 | */ | |
460f3462 KB |
158 | split2() |
159 | { | |
4bf5591e KB |
160 | register char *Ce, *Cs; |
161 | register long lcnt; | |
162 | register int len, bcnt; | |
460f3462 KB |
163 | |
164 | for (lcnt = 0;;) | |
86bcea21 KB |
165 | switch(len = read(ifd, bfr, MAXBSIZE)) { |
166 | case 0: | |
167 | exit(0); | |
168 | case ERR: | |
169 | perror("read"); | |
4bf5591e | 170 | exit(1); |
86bcea21 KB |
171 | default: |
172 | if (!file_open) { | |
173 | newfile(); | |
174 | file_open = YES; | |
175 | } | |
176 | for (Cs = Ce = bfr; len--; Ce++) | |
177 | if (*Ce == '\n' && ++lcnt == numlines) { | |
4bf5591e KB |
178 | bcnt = Ce - Cs + 1; |
179 | if (write(ofd, Cs, bcnt) != bcnt) | |
180 | wrerror(); | |
86bcea21 KB |
181 | lcnt = 0; |
182 | Cs = Ce + 1; | |
183 | if (len) | |
184 | newfile(); | |
185 | else | |
186 | file_open = NO; | |
460f3462 | 187 | } |
4bf5591e KB |
188 | if (Cs < Ce) { |
189 | bcnt = Ce - Cs; | |
190 | if (write(ofd, Cs, bcnt) != bcnt) | |
191 | wrerror(); | |
192 | } | |
f37d3151 | 193 | } |
460f3462 | 194 | } |
f37d3151 | 195 | |
460f3462 KB |
196 | /* |
197 | * newfile -- | |
198 | * open a new file | |
199 | */ | |
460f3462 KB |
200 | newfile() |
201 | { | |
4bf5591e KB |
202 | static long fnum; |
203 | static short defname; | |
204 | static char *fpnt; | |
460f3462 KB |
205 | |
206 | if (ofd == ERR) { | |
207 | if (fname[0]) { | |
208 | fpnt = fname + strlen(fname); | |
209 | defname = NO; | |
f37d3151 | 210 | } |
460f3462 KB |
211 | else { |
212 | fname[0] = 'x'; | |
213 | fpnt = fname + 1; | |
214 | defname = YES; | |
215 | } | |
216 | ofd = fileno(stdout); | |
217 | } | |
218 | /* | |
219 | * hack to increase max files; original code just wandered through | |
220 | * magic characters. Maximum files is 3 * 26 * 26 == 2028 | |
221 | */ | |
222 | #define MAXFILES 676 | |
223 | if (fnum == MAXFILES) { | |
224 | if (!defname || fname[0] == 'z') { | |
86bcea21 | 225 | fputs("split: too many files.\n", stderr); |
4bf5591e | 226 | exit(1); |
f37d3151 | 227 | } |
460f3462 KB |
228 | ++fname[0]; |
229 | fnum = 0; | |
230 | } | |
231 | fpnt[0] = fnum / 26 + 'a'; | |
232 | fpnt[1] = fnum % 26 + 'a'; | |
233 | ++fnum; | |
86bcea21 KB |
234 | if (!freopen(fname, "w", stdout)) { |
235 | fprintf(stderr, "split: unable to write to %s.\n", fname); | |
460f3462 KB |
236 | exit(ERR); |
237 | } | |
238 | } | |
239 | ||
240 | /* | |
241 | * usage -- | |
242 | * print usage message and die | |
243 | */ | |
460f3462 KB |
244 | usage() |
245 | { | |
86bcea21 | 246 | fputs("usage: split [-] [-#] [-b byte_count] [file [prefix]]\n", stderr); |
4bf5591e KB |
247 | exit(1); |
248 | } | |
249 | ||
250 | /* | |
251 | * wrerror -- | |
252 | * write error | |
253 | */ | |
254 | wrerror() | |
255 | { | |
256 | perror("split: write"); | |
257 | exit(1); | |
f37d3151 | 258 | } |