Home | History | Annotate | Line # | Download | only in split
split.c revision 1.4
      1  1.4  jtc /*	$NetBSD: split.c,v 1.4 1994/12/21 08:20:36 jtc Exp $	*/
      2  1.4  jtc 
      3  1.1  cgd /*
      4  1.4  jtc  * Copyright (c) 1987, 1993, 1994
      5  1.4  jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1  cgd  * modification, are permitted provided that the following conditions
      9  1.1  cgd  * are met:
     10  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1  cgd  *    must display the following acknowledgement:
     17  1.1  cgd  *	This product includes software developed by the University of
     18  1.1  cgd  *	California, Berkeley and its contributors.
     19  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  cgd  *    may be used to endorse or promote products derived from this software
     21  1.1  cgd  *    without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  cgd  * SUCH DAMAGE.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #ifndef lint
     37  1.4  jtc static char copyright[] =
     38  1.4  jtc "@(#) Copyright (c) 1987, 1993, 1994\n\
     39  1.4  jtc 	The Regents of the University of California.  All rights reserved.\n";
     40  1.1  cgd #endif /* not lint */
     41  1.1  cgd 
     42  1.1  cgd #ifndef lint
     43  1.4  jtc #if 0
     44  1.4  jtc static char sccsid[] = "@(#)split.c	8.2 (Berkeley) 4/16/94";
     45  1.4  jtc #endif
     46  1.4  jtc static char rcsid[] = "$NetBSD: split.c,v 1.4 1994/12/21 08:20:36 jtc Exp $";
     47  1.1  cgd #endif /* not lint */
     48  1.1  cgd 
     49  1.1  cgd #include <sys/param.h>
     50  1.4  jtc 
     51  1.4  jtc #include <ctype.h>
     52  1.4  jtc #include <err.h>
     53  1.4  jtc #include <fcntl.h>
     54  1.1  cgd #include <stdio.h>
     55  1.4  jtc #include <stdlib.h>
     56  1.4  jtc #include <string.h>
     57  1.4  jtc #include <unistd.h>
     58  1.4  jtc 
     59  1.4  jtc #define DEFLINE	1000			/* Default num lines per file. */
     60  1.4  jtc 
     61  1.4  jtc long	 bytecnt;			/* Byte count to split on. */
     62  1.4  jtc long	 numlines;			/* Line count to split on. */
     63  1.4  jtc int	 file_open;			/* If a file open. */
     64  1.4  jtc int	 ifd = -1, ofd = -1;		/* Input/output file descriptors. */
     65  1.4  jtc char	 bfr[MAXBSIZE];			/* I/O buffer. */
     66  1.4  jtc char	 fname[MAXPATHLEN];		/* File name prefix. */
     67  1.4  jtc 
     68  1.4  jtc void newfile __P((void));
     69  1.4  jtc void split1 __P((void));
     70  1.4  jtc void split2 __P((void));
     71  1.4  jtc void usage __P((void));
     72  1.1  cgd 
     73  1.4  jtc int
     74  1.1  cgd main(argc, argv)
     75  1.1  cgd 	int argc;
     76  1.4  jtc 	char *argv[];
     77  1.1  cgd {
     78  1.4  jtc 	int ch;
     79  1.4  jtc 	char *ep, *p;
     80  1.4  jtc 
     81  1.4  jtc 	while ((ch = getopt(argc, argv, "-0123456789b:l:")) != EOF)
     82  1.4  jtc 		switch (ch) {
     83  1.4  jtc 		case '0': case '1': case '2': case '3': case '4':
     84  1.4  jtc 		case '5': case '6': case '7': case '8': case '9':
     85  1.4  jtc 			/*
     86  1.4  jtc 			 * Undocumented kludge: split was originally designed
     87  1.4  jtc 			 * to take a number after a dash.
     88  1.4  jtc 			 */
     89  1.4  jtc 			if (numlines == 0) {
     90  1.4  jtc 				p = argv[optind - 1];
     91  1.4  jtc 				if (p[0] == '-' && p[1] == ch && !p[2])
     92  1.4  jtc 					numlines = strtol(++p, &ep, 10);
     93  1.4  jtc 				else
     94  1.4  jtc 					numlines =
     95  1.4  jtc 					    strtol(argv[optind] + 1, &ep, 10);
     96  1.4  jtc 				if (numlines <= 0 || *ep)
     97  1.4  jtc 					errx(1,
     98  1.4  jtc 					    "%s: illegal line count.", optarg);
     99  1.1  cgd 			}
    100  1.4  jtc 			break;
    101  1.4  jtc 		case '-':		/* Undocumented: historic stdin flag. */
    102  1.4  jtc 			if (ifd != -1)
    103  1.4  jtc 				usage();
    104  1.4  jtc 			ifd = 0;
    105  1.4  jtc 			break;
    106  1.4  jtc 		case 'b':		/* Byte count. */
    107  1.4  jtc 			if ((bytecnt = strtol(optarg, &ep, 10)) <= 0 ||
    108  1.4  jtc 			    *ep != '\0' && *ep != 'k' && *ep != 'm')
    109  1.4  jtc 				errx(1, "%s: illegal byte count.", optarg);
    110  1.4  jtc 			if (*ep == 'k')
    111  1.4  jtc 				bytecnt *= 1024;
    112  1.4  jtc 			else if (*ep == 'm')
    113  1.4  jtc 				bytecnt *= 1048576;
    114  1.4  jtc 			break;
    115  1.4  jtc 		case 'l':		/* Line count. */
    116  1.4  jtc 			if (numlines != 0)
    117  1.4  jtc 				usage();
    118  1.4  jtc 			if ((numlines = strtol(optarg, &ep, 10)) <= 0 || *p)
    119  1.4  jtc 				errx(1, "%s: illegal line count.", optarg);
    120  1.4  jtc 			break;
    121  1.4  jtc 		default:
    122  1.4  jtc 			usage();
    123  1.4  jtc 		}
    124  1.4  jtc 	argv += optind;
    125  1.4  jtc 	argc -= optind;
    126  1.4  jtc 
    127  1.4  jtc 	if (*argv != NULL)
    128  1.4  jtc 		if (ifd == -1) {		/* Input file. */
    129  1.4  jtc 			if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
    130  1.4  jtc 				err(1, "%s", *argv);
    131  1.4  jtc 			++argv;
    132  1.1  cgd 		}
    133  1.4  jtc 	if (*argv != NULL)			/* File name prefix. */
    134  1.4  jtc 		(void)strcpy(fname, *argv++);
    135  1.4  jtc 	if (*argv != NULL)
    136  1.4  jtc 		usage();
    137  1.4  jtc 
    138  1.4  jtc 	if (numlines == 0)
    139  1.4  jtc 		numlines = DEFLINE;
    140  1.4  jtc 	else if (bytecnt)
    141  1.4  jtc 		usage();
    142  1.4  jtc 
    143  1.4  jtc 	if (ifd == -1)				/* Stdin by default. */
    144  1.1  cgd 		ifd = 0;
    145  1.4  jtc 
    146  1.4  jtc 	if (bytecnt) {
    147  1.1  cgd 		split1();
    148  1.4  jtc 		exit (0);
    149  1.4  jtc 	}
    150  1.1  cgd 	split2();
    151  1.1  cgd 	exit(0);
    152  1.1  cgd }
    153  1.1  cgd 
    154  1.1  cgd /*
    155  1.1  cgd  * split1 --
    156  1.4  jtc  *	Split the input by bytes.
    157  1.1  cgd  */
    158  1.4  jtc void
    159  1.1  cgd split1()
    160  1.1  cgd {
    161  1.4  jtc 	long bcnt;
    162  1.4  jtc 	int dist, len;
    163  1.4  jtc 	char *C;
    164  1.1  cgd 
    165  1.1  cgd 	for (bcnt = 0;;)
    166  1.4  jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    167  1.1  cgd 		case 0:
    168  1.4  jtc 			exit(0);
    169  1.4  jtc 		case -1:
    170  1.4  jtc 			err(1, "read");
    171  1.4  jtc 			/* NOTREACHED */
    172  1.1  cgd 		default:
    173  1.1  cgd 			if (!file_open) {
    174  1.1  cgd 				newfile();
    175  1.4  jtc 				file_open = 1;
    176  1.1  cgd 			}
    177  1.1  cgd 			if (bcnt + len >= bytecnt) {
    178  1.1  cgd 				dist = bytecnt - bcnt;
    179  1.1  cgd 				if (write(ofd, bfr, dist) != dist)
    180  1.4  jtc 					err(1, "write");
    181  1.1  cgd 				len -= dist;
    182  1.4  jtc 				for (C = bfr + dist; len >= bytecnt;
    183  1.4  jtc 				    len -= bytecnt, C += bytecnt) {
    184  1.1  cgd 					newfile();
    185  1.4  jtc 					if (write(ofd,
    186  1.4  jtc 					    C, (int)bytecnt) != bytecnt)
    187  1.4  jtc 						err(1, "write");
    188  1.1  cgd 				}
    189  1.1  cgd 				if (len) {
    190  1.1  cgd 					newfile();
    191  1.1  cgd 					if (write(ofd, C, len) != len)
    192  1.4  jtc 						err(1, "write");
    193  1.4  jtc 				} else
    194  1.4  jtc 					file_open = 0;
    195  1.1  cgd 				bcnt = len;
    196  1.4  jtc 			} else {
    197  1.1  cgd 				bcnt += len;
    198  1.1  cgd 				if (write(ofd, bfr, len) != len)
    199  1.4  jtc 					err(1, "write");
    200  1.1  cgd 			}
    201  1.1  cgd 		}
    202  1.1  cgd }
    203  1.1  cgd 
    204  1.1  cgd /*
    205  1.1  cgd  * split2 --
    206  1.4  jtc  *	Split the input by lines.
    207  1.1  cgd  */
    208  1.4  jtc void
    209  1.1  cgd split2()
    210  1.1  cgd {
    211  1.4  jtc 	long lcnt;
    212  1.4  jtc 	int len, bcnt;
    213  1.4  jtc 	char *Ce, *Cs;
    214  1.1  cgd 
    215  1.1  cgd 	for (lcnt = 0;;)
    216  1.4  jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    217  1.1  cgd 		case 0:
    218  1.1  cgd 			exit(0);
    219  1.4  jtc 		case -1:
    220  1.4  jtc 			err(1, "read");
    221  1.4  jtc 			/* NOTREACHED */
    222  1.1  cgd 		default:
    223  1.1  cgd 			if (!file_open) {
    224  1.1  cgd 				newfile();
    225  1.4  jtc 				file_open = 1;
    226  1.1  cgd 			}
    227  1.1  cgd 			for (Cs = Ce = bfr; len--; Ce++)
    228  1.1  cgd 				if (*Ce == '\n' && ++lcnt == numlines) {
    229  1.1  cgd 					bcnt = Ce - Cs + 1;
    230  1.1  cgd 					if (write(ofd, Cs, bcnt) != bcnt)
    231  1.4  jtc 						err(1, "write");
    232  1.1  cgd 					lcnt = 0;
    233  1.1  cgd 					Cs = Ce + 1;
    234  1.1  cgd 					if (len)
    235  1.1  cgd 						newfile();
    236  1.1  cgd 					else
    237  1.4  jtc 						file_open = 0;
    238  1.1  cgd 				}
    239  1.1  cgd 			if (Cs < Ce) {
    240  1.1  cgd 				bcnt = Ce - Cs;
    241  1.1  cgd 				if (write(ofd, Cs, bcnt) != bcnt)
    242  1.4  jtc 					err(1, "write");
    243  1.1  cgd 			}
    244  1.1  cgd 		}
    245  1.1  cgd }
    246  1.1  cgd 
    247  1.1  cgd /*
    248  1.1  cgd  * newfile --
    249  1.4  jtc  *	Open a new output file.
    250  1.1  cgd  */
    251  1.4  jtc void
    252  1.1  cgd newfile()
    253  1.1  cgd {
    254  1.1  cgd 	static long fnum;
    255  1.4  jtc 	static int defname;
    256  1.1  cgd 	static char *fpnt;
    257  1.1  cgd 
    258  1.4  jtc 	if (ofd == -1) {
    259  1.4  jtc 		if (fname[0] == '\0') {
    260  1.1  cgd 			fname[0] = 'x';
    261  1.1  cgd 			fpnt = fname + 1;
    262  1.4  jtc 			defname = 1;
    263  1.4  jtc 		} else {
    264  1.4  jtc 			fpnt = fname + strlen(fname);
    265  1.4  jtc 			defname = 0;
    266  1.1  cgd 		}
    267  1.1  cgd 		ofd = fileno(stdout);
    268  1.1  cgd 	}
    269  1.1  cgd 	/*
    270  1.4  jtc 	 * Hack to increase max files; original code wandered through
    271  1.1  cgd 	 * magic characters.  Maximum files is 3 * 26 * 26 == 2028
    272  1.1  cgd 	 */
    273  1.1  cgd #define MAXFILES	676
    274  1.1  cgd 	if (fnum == MAXFILES) {
    275  1.4  jtc 		if (!defname || fname[0] == 'z')
    276  1.4  jtc 			errx(1, "too many files.");
    277  1.1  cgd 		++fname[0];
    278  1.1  cgd 		fnum = 0;
    279  1.1  cgd 	}
    280  1.1  cgd 	fpnt[0] = fnum / 26 + 'a';
    281  1.1  cgd 	fpnt[1] = fnum % 26 + 'a';
    282  1.1  cgd 	++fnum;
    283  1.4  jtc 	if (!freopen(fname, "w", stdout))
    284  1.4  jtc 		err(1, "%s", fname);
    285  1.1  cgd }
    286  1.1  cgd 
    287  1.4  jtc void
    288  1.1  cgd usage()
    289  1.1  cgd {
    290  1.4  jtc 	(void)fprintf(stderr,
    291  1.4  jtc "usage: split [-b byte_count] [-l line_count] [file [prefix]]\n");
    292  1.1  cgd 	exit(1);
    293  1.1  cgd }
    294