split.c revision 1590
1303231Sdim/*
2303231Sdim * Copyright (c) 1987, 1993, 1994
3303231Sdim *	The Regents of the University of California.  All rights reserved.
4303231Sdim *
5303231Sdim * Redistribution and use in source and binary forms, with or without
6303231Sdim * modification, are permitted provided that the following conditions
7303231Sdim * are met:
8303231Sdim * 1. Redistributions of source code must retain the above copyright
9303231Sdim *    notice, this list of conditions and the following disclaimer.
10303231Sdim * 2. Redistributions in binary form must reproduce the above copyright
11303231Sdim *    notice, this list of conditions and the following disclaimer in the
12303231Sdim *    documentation and/or other materials provided with the distribution.
13303231Sdim * 3. All advertising materials mentioning features or use of this software
14303231Sdim *    must display the following acknowledgement:
15303231Sdim *	This product includes software developed by the University of
16303231Sdim *	California, Berkeley and its contributors.
17303231Sdim * 4. Neither the name of the University nor the names of its contributors
18303231Sdim *    may be used to endorse or promote products derived from this software
19303231Sdim *    without specific prior written permission.
20303231Sdim *
21303231Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22303231Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23303231Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24303231Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25303231Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26303231Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27303231Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28303231Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29303231Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30303231Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31303231Sdim * SUCH DAMAGE.
32303231Sdim */
33303231Sdim
34303231Sdim#ifndef lint
35303231Sdimstatic char copyright[] =
36303231Sdim"@(#) Copyright (c) 1987, 1993, 1994\n\
37303231Sdim	The Regents of the University of California.  All rights reserved.\n";
38303231Sdim#endif /* not lint */
39303231Sdim
40303231Sdim#ifndef lint
41303231Sdimstatic char sccsid[] = "@(#)split.c	8.2 (Berkeley) 4/16/94";
42303231Sdim#endif /* not lint */
43303231Sdim
44303231Sdim#include <sys/param.h>
45303231Sdim
46303231Sdim#include <ctype.h>
47303231Sdim#include <err.h>
48303231Sdim#include <fcntl.h>
49303231Sdim#include <stdio.h>
50303231Sdim#include <stdlib.h>
51303231Sdim#include <string.h>
52303231Sdim#include <unistd.h>
53303231Sdim
54303231Sdim#define DEFLINE	1000			/* Default num lines per file. */
55303231Sdim
56303231Sdimlong	 bytecnt;			/* Byte count to split on. */
57303231Sdimlong	 numlines;			/* Line count to split on. */
58303231Sdimint	 file_open;			/* If a file open. */
59303231Sdimint	 ifd = -1, ofd = -1;		/* Input/output file descriptors. */
60303231Sdimchar	 bfr[MAXBSIZE];			/* I/O buffer. */
61303231Sdimchar	 fname[MAXPATHLEN];		/* File name prefix. */
62303231Sdim
63303231Sdimvoid newfile __P((void));
64303231Sdimvoid split1 __P((void));
65303231Sdimvoid split2 __P((void));
66303231Sdimvoid usage __P((void));
67303231Sdim
68303231Sdimint
69303231Sdimmain(argc, argv)
70303231Sdim	int argc;
71303231Sdim	char *argv[];
72303231Sdim{
73303231Sdim	int ch;
74303231Sdim	char *ep, *p;
75303231Sdim
76303231Sdim	while ((ch = getopt(argc, argv, "-0123456789b:l:")) != EOF)
77303231Sdim		switch (ch) {
78303231Sdim		case '0': case '1': case '2': case '3': case '4':
79303231Sdim		case '5': case '6': case '7': case '8': case '9':
80303231Sdim			/*
81303231Sdim			 * Undocumented kludge: split was originally designed
82303231Sdim			 * to take a number after a dash.
83303231Sdim			 */
84303231Sdim			if (numlines == 0) {
85303231Sdim				p = argv[optind - 1];
86303231Sdim				if (p[0] == '-' && p[1] == ch && !p[2])
87303231Sdim					numlines = strtol(++p, &ep, 10);
88303231Sdim				else
89303231Sdim					numlines =
90303231Sdim					    strtol(argv[optind] + 1, &ep, 10);
91303231Sdim				if (numlines <= 0 || *ep)
92303231Sdim					errx(1,
93303231Sdim					    "%s: illegal line count.", optarg);
94303231Sdim			}
95303231Sdim			break;
96303231Sdim		case '-':		/* Undocumented: historic stdin flag. */
97303231Sdim			if (ifd != -1)
98303231Sdim				usage();
99303231Sdim			ifd = 0;
100303231Sdim			break;
101303231Sdim		case 'b':		/* Byte count. */
102303231Sdim			if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
103303231Sdim			    *ep != '\0' && *ep != 'k' && *ep != 'm')
104303231Sdim				errx(1, "%s: illegal byte count.", optarg);
105303231Sdim			if (*ep == 'k')
106303231Sdim				bytecnt *= 1024;
107303231Sdim			else if (*ep == 'm')
108303231Sdim				bytecnt *= 1048576;
109303231Sdim			break;
110303231Sdim		case 'l':		/* Line count. */
111303231Sdim			if (numlines != 0)
112303231Sdim				usage();
113303231Sdim			if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *p)
114303231Sdim				errx(1, "%s: illegal line count.", optarg);
115303231Sdim			break;
116303231Sdim		default:
117303231Sdim			usage();
118303231Sdim		}
119303231Sdim	argv += optind;
120303231Sdim	argc -= optind;
121303231Sdim
122303231Sdim	if (*argv != NULL)
123303231Sdim		if (ifd == -1) {		/* Input file. */
124303231Sdim			if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
125303231Sdim				err(1, "%s", *argv);
126303231Sdim			++argv;
127303231Sdim		}
128303231Sdim	if (*argv != NULL)			/* File name prefix. */
129303231Sdim		(void)strcpy(fname, *argv++);
130303231Sdim	if (*argv != NULL)
131303231Sdim		usage();
132303231Sdim
133303231Sdim	if (numlines == 0)
134303231Sdim		numlines = DEFLINE;
135303231Sdim	else if (bytecnt)
136303231Sdim		usage();
137303231Sdim
138303231Sdim	if (ifd == -1)				/* Stdin by default. */
139303231Sdim		ifd = 0;
140303231Sdim
141303231Sdim	if (bytecnt) {
142303231Sdim		split1();
143303231Sdim		exit (0);
144303231Sdim	}
145303231Sdim	split2();
146303231Sdim	exit(0);
147303231Sdim}
148303231Sdim
149303231Sdim/*
150303231Sdim * split1 --
151303231Sdim *	Split the input by bytes.
152303231Sdim */
153void
154split1()
155{
156	long bcnt;
157	int dist, len;
158	char *C;
159
160	for (bcnt = 0;;)
161		switch (len = read(ifd, bfr, MAXBSIZE)) {
162		case 0:
163			exit(0);
164		case -1:
165			err(1, "read");
166			/* NOTREACHED */
167		default:
168			if (!file_open) {
169				newfile();
170				file_open = 1;
171			}
172			if (bcnt + len >= bytecnt) {
173				dist = bytecnt - bcnt;
174				if (write(ofd, bfr, dist) != dist)
175					err(1, "write");
176				len -= dist;
177				for (C = bfr + dist; len >= bytecnt;
178				    len -= bytecnt, C += bytecnt) {
179					newfile();
180					if (write(ofd,
181					    C, (int)bytecnt) != bytecnt)
182						err(1, "write");
183				}
184				if (len) {
185					newfile();
186					if (write(ofd, C, len) != len)
187						err(1, "write");
188				} else
189					file_open = 0;
190				bcnt = len;
191			} else {
192				bcnt += len;
193				if (write(ofd, bfr, len) != len)
194					err(1, "write");
195			}
196		}
197}
198
199/*
200 * split2 --
201 *	Split the input by lines.
202 */
203void
204split2()
205{
206	long lcnt;
207	int len, bcnt;
208	char *Ce, *Cs;
209
210	for (lcnt = 0;;)
211		switch (len = read(ifd, bfr, MAXBSIZE)) {
212		case 0:
213			exit(0);
214		case -1:
215			err(1, "read");
216			/* NOTREACHED */
217		default:
218			if (!file_open) {
219				newfile();
220				file_open = 1;
221			}
222			for (Cs = Ce = bfr; len--; Ce++)
223				if (*Ce == '\n' && ++lcnt == numlines) {
224					bcnt = Ce - Cs + 1;
225					if (write(ofd, Cs, bcnt) != bcnt)
226						err(1, "write");
227					lcnt = 0;
228					Cs = Ce + 1;
229					if (len)
230						newfile();
231					else
232						file_open = 0;
233				}
234			if (Cs < Ce) {
235				bcnt = Ce - Cs;
236				if (write(ofd, Cs, bcnt) != bcnt)
237					err(1, "write");
238			}
239		}
240}
241
242/*
243 * newfile --
244 *	Open a new output file.
245 */
246void
247newfile()
248{
249	static long fnum;
250	static int defname;
251	static char *fpnt;
252
253	if (ofd == -1) {
254		if (fname[0] == '\0') {
255			fname[0] = 'x';
256			fpnt = fname + 1;
257			defname = 1;
258		} else {
259			fpnt = fname + strlen(fname);
260			defname = 0;
261		}
262		ofd = fileno(stdout);
263	}
264	/*
265	 * Hack to increase max files; original code wandered through
266	 * magic characters.  Maximum files is 3 * 26 * 26 == 2028
267	 */
268#define MAXFILES	676
269	if (fnum == MAXFILES) {
270		if (!defname || fname[0] == 'z')
271			errx(1, "too many files.");
272		++fname[0];
273		fnum = 0;
274	}
275	fpnt[0] = fnum / 26 + 'a';
276	fpnt[1] = fnum % 26 + 'a';
277	++fnum;
278	if (!freopen(fname, "w", stdout))
279		err(1, "%s", fname);
280}
281
282void
283usage()
284{
285	(void)fprintf(stderr,
286"usage: split [-b byte_count] [-l line_count] [file [prefix]]\n");
287	exit(1);
288}
289