Home | History | Annotate | Line # | Download | only in split
split.c revision 1.14
      1  1.14     bjh21 /*	$NetBSD: split.c,v 1.14 2003/06/26 22:49:53 bjh21 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.6     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.6     lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
     39   1.6     lukem 	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.5       jtc static char sccsid[] = "@(#)split.c	8.3 (Berkeley) 4/25/94";
     45   1.4       jtc #endif
     46  1.14     bjh21 __RCSID("$NetBSD: split.c,v 1.14 2003/06/26 22:49:53 bjh21 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.12     bjh21 #include <errno.h>
     54   1.4       jtc #include <fcntl.h>
     55   1.1       cgd #include <stdio.h>
     56   1.4       jtc #include <stdlib.h>
     57   1.4       jtc #include <string.h>
     58   1.4       jtc #include <unistd.h>
     59   1.4       jtc 
     60   1.9  christos #define DEFLINE	1000		/* Default num lines per file. */
     61   1.4       jtc 
     62   1.9  christos static int file_open;		/* If a file open. */
     63   1.9  christos static int ifd = -1, ofd = -1;	/* Input/output file descriptors. */
     64  1.12     bjh21 static char *fname;		/* File name prefix. */
     65  1.14     bjh21 static size_t sfxlen = 2;		/* suffix length. */
     66   1.9  christos 
     67   1.9  christos int  main(int, char **);
     68   1.9  christos static void newfile(void);
     69  1.14     bjh21 static void split1(off_t);
     70  1.14     bjh21 static void split2(off_t);
     71   1.9  christos static void usage(void) __attribute__((__noreturn__));
     72  1.14     bjh21 static size_t bigwrite(int, void const *, size_t);
     73   1.1       cgd 
     74   1.4       jtc int
     75   1.9  christos main(int argc, char *argv[])
     76   1.1       cgd {
     77   1.4       jtc 	int ch;
     78   1.4       jtc 	char *ep, *p;
     79  1.14     bjh21 	off_t bytecnt = 0;	/* Byte count to split on. */
     80  1.14     bjh21 	off_t numlines = 0;	/* Line count to split on. */
     81  1.12     bjh21 	size_t namelen;
     82  1.12     bjh21 	long name_max;
     83   1.4       jtc 
     84  1.10     bjh21 	while ((ch = getopt(argc, argv, "-0123456789b:l:a:")) != -1)
     85   1.4       jtc 		switch (ch) {
     86   1.4       jtc 		case '0': case '1': case '2': case '3': case '4':
     87   1.4       jtc 		case '5': case '6': case '7': case '8': case '9':
     88   1.4       jtc 			/*
     89   1.4       jtc 			 * Undocumented kludge: split was originally designed
     90   1.4       jtc 			 * to take a number after a dash.
     91   1.4       jtc 			 */
     92   1.4       jtc 			if (numlines == 0) {
     93   1.4       jtc 				p = argv[optind - 1];
     94   1.4       jtc 				if (p[0] == '-' && p[1] == ch && !p[2])
     95  1.14     bjh21 					p++;
     96   1.4       jtc 				else
     97  1.14     bjh21 					p = argv[optind] + 1;
     98  1.14     bjh21 				numlines = strtoull(p, &ep, 10);
     99  1.14     bjh21 				if (numlines == 0 || *ep != '\0')
    100  1.14     bjh21 					errx(1, "%s: illegal line count.", p);
    101   1.1       cgd 			}
    102   1.4       jtc 			break;
    103  1.11     bjh21 		case '-':		/* stdin flag. */
    104   1.4       jtc 			if (ifd != -1)
    105   1.4       jtc 				usage();
    106   1.4       jtc 			ifd = 0;
    107   1.4       jtc 			break;
    108   1.4       jtc 		case 'b':		/* Byte count. */
    109  1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    110  1.14     bjh21 			    (bytecnt = strtoull(optarg, &ep, 10)) == 0 ||
    111   1.6     lukem 			    (*ep != '\0' && *ep != 'k' && *ep != 'm'))
    112   1.4       jtc 				errx(1, "%s: illegal byte count.", optarg);
    113   1.4       jtc 			if (*ep == 'k')
    114   1.4       jtc 				bytecnt *= 1024;
    115   1.4       jtc 			else if (*ep == 'm')
    116   1.9  christos 				bytecnt *= 1024 * 1024;
    117   1.4       jtc 			break;
    118   1.4       jtc 		case 'l':		/* Line count. */
    119   1.4       jtc 			if (numlines != 0)
    120   1.4       jtc 				usage();
    121  1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    122  1.14     bjh21 			    (numlines = strtoull(optarg, &ep, 10)) == 0 ||
    123  1.14     bjh21 			    *ep != '\0')
    124   1.4       jtc 				errx(1, "%s: illegal line count.", optarg);
    125   1.4       jtc 			break;
    126  1.13     bjh21 		case 'a':		/* Suffix length. */
    127  1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    128  1.14     bjh21 			    (sfxlen = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
    129  1.14     bjh21 			    *ep != '\0')
    130  1.10     bjh21 				errx(1, "%s: illegal suffix length.", optarg);
    131  1.10     bjh21 			break;
    132   1.4       jtc 		default:
    133   1.4       jtc 			usage();
    134   1.4       jtc 		}
    135   1.4       jtc 	argv += optind;
    136   1.4       jtc 	argc -= optind;
    137   1.4       jtc 
    138   1.4       jtc 	if (*argv != NULL)
    139   1.4       jtc 		if (ifd == -1) {		/* Input file. */
    140  1.11     bjh21 			if (strcmp(*argv, "-") == 0)
    141  1.11     bjh21 				ifd = STDIN_FILENO;
    142  1.11     bjh21 			else if ((ifd = open(*argv, O_RDONLY, 0)) < 0)
    143   1.4       jtc 				err(1, "%s", *argv);
    144   1.4       jtc 			++argv;
    145   1.1       cgd 		}
    146  1.10     bjh21 
    147  1.12     bjh21 	errno = 0;
    148  1.12     bjh21 	if ((name_max = pathconf(".", _PC_NAME_MAX)) == -1 &&
    149  1.12     bjh21 	    errno != 0)
    150  1.12     bjh21 		err(EXIT_FAILURE, "pathconf");
    151  1.10     bjh21 	if (*argv != NULL) {
    152  1.12     bjh21 		namelen = strlen(*argv) + sfxlen;
    153  1.12     bjh21 		if (name_max != -1 && namelen > name_max)
    154  1.10     bjh21 			errx(EXIT_FAILURE, "Output file name too long");
    155  1.12     bjh21 		if ((fname = malloc(namelen + 1)) == NULL)
    156  1.12     bjh21 			err(EXIT_FAILURE, NULL);
    157  1.10     bjh21 		(void)strcpy(fname, *argv++);		/* File name prefix. */
    158  1.10     bjh21 	} else {
    159  1.12     bjh21 		if (name_max != -1 && 1 + sfxlen > name_max)
    160  1.10     bjh21 			errx(EXIT_FAILURE, "Output file name too long");
    161  1.12     bjh21 		if ((fname = malloc(sfxlen + 2)) == NULL)
    162  1.12     bjh21 			err(EXIT_FAILURE, NULL);
    163  1.12     bjh21 		fname[0] = '\0';
    164  1.10     bjh21 	}
    165  1.10     bjh21 
    166   1.4       jtc 	if (*argv != NULL)
    167   1.4       jtc 		usage();
    168   1.4       jtc 
    169   1.4       jtc 	if (numlines == 0)
    170   1.4       jtc 		numlines = DEFLINE;
    171   1.4       jtc 	else if (bytecnt)
    172   1.4       jtc 		usage();
    173   1.4       jtc 
    174   1.4       jtc 	if (ifd == -1)				/* Stdin by default. */
    175   1.1       cgd 		ifd = 0;
    176   1.4       jtc 
    177  1.13     bjh21 	if (bytecnt)
    178   1.9  christos 		split1(bytecnt);
    179  1.13     bjh21 	else
    180   1.9  christos 		split2(numlines);
    181  1.13     bjh21 
    182   1.9  christos 	return 0;
    183   1.1       cgd }
    184   1.1       cgd 
    185   1.1       cgd /*
    186   1.1       cgd  * split1 --
    187   1.4       jtc  *	Split the input by bytes.
    188   1.1       cgd  */
    189   1.9  christos static void
    190  1.14     bjh21 split1(off_t bytecnt)
    191   1.1       cgd {
    192  1.14     bjh21 	off_t bcnt;
    193  1.14     bjh21 	ssize_t dist, len;
    194   1.4       jtc 	char *C;
    195   1.9  christos 	char bfr[MAXBSIZE];
    196   1.1       cgd 
    197   1.1       cgd 	for (bcnt = 0;;)
    198   1.4       jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    199   1.1       cgd 		case 0:
    200   1.4       jtc 			exit(0);
    201  1.13     bjh21 			/* NOTREACHED */
    202   1.4       jtc 		case -1:
    203   1.4       jtc 			err(1, "read");
    204   1.4       jtc 			/* NOTREACHED */
    205   1.1       cgd 		default:
    206   1.1       cgd 			if (!file_open) {
    207   1.1       cgd 				newfile();
    208   1.4       jtc 				file_open = 1;
    209   1.1       cgd 			}
    210   1.1       cgd 			if (bcnt + len >= bytecnt) {
    211  1.14     bjh21 				/* LINTED: bytecnt - bcnt <= len */
    212   1.1       cgd 				dist = bytecnt - bcnt;
    213   1.9  christos 				if (bigwrite(ofd, bfr, dist) != dist)
    214   1.4       jtc 					err(1, "write");
    215   1.1       cgd 				len -= dist;
    216   1.4       jtc 				for (C = bfr + dist; len >= bytecnt;
    217  1.14     bjh21 				    /* LINTED: bytecnt <= len */
    218   1.4       jtc 				    len -= bytecnt, C += bytecnt) {
    219   1.1       cgd 					newfile();
    220  1.14     bjh21 					/* LINTED: as above */
    221   1.9  christos 					if (bigwrite(ofd,
    222  1.14     bjh21 					    C, bytecnt) != bytecnt)
    223   1.4       jtc 						err(1, "write");
    224   1.1       cgd 				}
    225   1.1       cgd 				if (len) {
    226   1.1       cgd 					newfile();
    227  1.14     bjh21 					/* LINTED: len >= 0 */
    228   1.9  christos 					if (bigwrite(ofd, C, len) != len)
    229   1.4       jtc 						err(1, "write");
    230   1.4       jtc 				} else
    231   1.4       jtc 					file_open = 0;
    232   1.1       cgd 				bcnt = len;
    233   1.4       jtc 			} else {
    234   1.1       cgd 				bcnt += len;
    235  1.14     bjh21 				/* LINTED: len >= 0 */
    236   1.9  christos 				if (bigwrite(ofd, bfr, len) != len)
    237   1.4       jtc 					err(1, "write");
    238   1.1       cgd 			}
    239   1.1       cgd 		}
    240   1.1       cgd }
    241   1.1       cgd 
    242   1.1       cgd /*
    243   1.1       cgd  * split2 --
    244   1.4       jtc  *	Split the input by lines.
    245   1.1       cgd  */
    246   1.9  christos static void
    247  1.14     bjh21 split2(off_t numlines)
    248   1.1       cgd {
    249  1.14     bjh21 	off_t lcnt;
    250  1.14     bjh21 	size_t bcnt;
    251   1.9  christos 	ssize_t len;
    252   1.4       jtc 	char *Ce, *Cs;
    253   1.9  christos 	char bfr[MAXBSIZE];
    254   1.1       cgd 
    255   1.1       cgd 	for (lcnt = 0;;)
    256   1.4       jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    257   1.1       cgd 		case 0:
    258   1.1       cgd 			exit(0);
    259  1.13     bjh21 			/* NOTREACHED */
    260   1.4       jtc 		case -1:
    261   1.4       jtc 			err(1, "read");
    262   1.4       jtc 			/* NOTREACHED */
    263   1.1       cgd 		default:
    264   1.1       cgd 			if (!file_open) {
    265   1.1       cgd 				newfile();
    266   1.4       jtc 				file_open = 1;
    267   1.1       cgd 			}
    268   1.1       cgd 			for (Cs = Ce = bfr; len--; Ce++)
    269   1.1       cgd 				if (*Ce == '\n' && ++lcnt == numlines) {
    270   1.1       cgd 					bcnt = Ce - Cs + 1;
    271   1.9  christos 					if (bigwrite(ofd, Cs, bcnt) != bcnt)
    272   1.4       jtc 						err(1, "write");
    273   1.1       cgd 					lcnt = 0;
    274   1.1       cgd 					Cs = Ce + 1;
    275   1.1       cgd 					if (len)
    276   1.1       cgd 						newfile();
    277   1.1       cgd 					else
    278   1.4       jtc 						file_open = 0;
    279   1.1       cgd 				}
    280   1.1       cgd 			if (Cs < Ce) {
    281   1.1       cgd 				bcnt = Ce - Cs;
    282   1.9  christos 				if (bigwrite(ofd, Cs, bcnt) != bcnt)
    283   1.4       jtc 					err(1, "write");
    284   1.1       cgd 			}
    285   1.1       cgd 		}
    286   1.1       cgd }
    287   1.1       cgd 
    288   1.1       cgd /*
    289   1.1       cgd  * newfile --
    290   1.4       jtc  *	Open a new output file.
    291   1.1       cgd  */
    292   1.9  christos static void
    293   1.9  christos newfile(void)
    294   1.1       cgd {
    295   1.9  christos 	static int fnum;
    296   1.4       jtc 	static int defname;
    297   1.1       cgd 	static char *fpnt;
    298  1.10     bjh21 	int quot, i;
    299   1.1       cgd 
    300   1.4       jtc 	if (ofd == -1) {
    301   1.4       jtc 		if (fname[0] == '\0') {
    302   1.1       cgd 			fname[0] = 'x';
    303   1.1       cgd 			fpnt = fname + 1;
    304   1.4       jtc 			defname = 1;
    305   1.4       jtc 		} else {
    306   1.4       jtc 			fpnt = fname + strlen(fname);
    307   1.4       jtc 			defname = 0;
    308   1.1       cgd 		}
    309   1.1       cgd 		ofd = fileno(stdout);
    310   1.1       cgd 	}
    311   1.1       cgd 	/*
    312   1.4       jtc 	 * Hack to increase max files; original code wandered through
    313   1.1       cgd 	 * magic characters.  Maximum files is 3 * 26 * 26 == 2028
    314   1.1       cgd 	 */
    315  1.10     bjh21 	fpnt[sfxlen] = '\0';
    316  1.10     bjh21 	quot = fnum;
    317  1.10     bjh21 	for (i = sfxlen - 1; i >= 0; i--) {
    318  1.10     bjh21 		fpnt[i] = quot % 26 + 'a';
    319  1.10     bjh21 		quot = quot / 26;
    320  1.10     bjh21 	}
    321  1.10     bjh21 	if (quot > 0) {
    322   1.4       jtc 		if (!defname || fname[0] == 'z')
    323   1.4       jtc 			errx(1, "too many files.");
    324   1.1       cgd 		++fname[0];
    325   1.1       cgd 		fnum = 0;
    326   1.1       cgd 	}
    327   1.1       cgd 	++fnum;
    328   1.4       jtc 	if (!freopen(fname, "w", stdout))
    329   1.4       jtc 		err(1, "%s", fname);
    330   1.1       cgd }
    331   1.1       cgd 
    332  1.14     bjh21 static size_t
    333  1.14     bjh21 bigwrite(int fd, const void *buf, size_t len)
    334   1.9  christos {
    335   1.9  christos 	const char *ptr = buf;
    336  1.14     bjh21 	size_t sofar = 0;
    337  1.14     bjh21 	ssize_t w;
    338   1.9  christos 
    339   1.9  christos 	while (len != 0) {
    340  1.14     bjh21 		if  ((w = write(fd, ptr, len)) == -1)
    341   1.9  christos 			return sofar;
    342   1.9  christos 		len -= w;
    343   1.9  christos 		ptr += w;
    344   1.9  christos 		sofar += w;
    345   1.9  christos 	}
    346   1.9  christos 	return sofar;
    347   1.9  christos }
    348   1.9  christos 
    349   1.9  christos 
    350   1.9  christos static void
    351   1.9  christos usage(void)
    352   1.1       cgd {
    353   1.4       jtc 	(void)fprintf(stderr,
    354  1.10     bjh21 "Usage: %s [-b byte_count] [-l line_count] [-a suffix_length] "
    355  1.10     bjh21 "[file [prefix]]\n", getprogname());
    356   1.1       cgd 	exit(1);
    357   1.1       cgd }
    358