Home | History | Annotate | Line # | Download | only in split
split.c revision 1.26.24.1
      1  1.26.24.1  pgoyette /*	$NetBSD: split.c,v 1.26.24.1 2017/03/20 06:58:06 pgoyette 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.20       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32        1.6     lukem #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34       1.24     lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
     35       1.24     lukem  The Regents of the University of California.  All rights reserved.");
     36        1.1       cgd #endif /* not lint */
     37        1.1       cgd 
     38        1.1       cgd #ifndef lint
     39        1.4       jtc #if 0
     40        1.5       jtc static char sccsid[] = "@(#)split.c	8.3 (Berkeley) 4/25/94";
     41        1.4       jtc #endif
     42  1.26.24.1  pgoyette __RCSID("$NetBSD: split.c,v 1.26.24.1 2017/03/20 06:58:06 pgoyette Exp $");
     43        1.1       cgd #endif /* not lint */
     44        1.1       cgd 
     45        1.1       cgd #include <sys/param.h>
     46  1.26.24.1  pgoyette #include <sys/stat.h>
     47        1.4       jtc 
     48        1.4       jtc #include <ctype.h>
     49        1.4       jtc #include <err.h>
     50       1.12     bjh21 #include <errno.h>
     51        1.4       jtc #include <fcntl.h>
     52        1.1       cgd #include <stdio.h>
     53        1.4       jtc #include <stdlib.h>
     54        1.4       jtc #include <string.h>
     55        1.4       jtc #include <unistd.h>
     56        1.4       jtc 
     57        1.9  christos #define DEFLINE	1000		/* Default num lines per file. */
     58        1.4       jtc 
     59        1.9  christos static int file_open;		/* If a file open. */
     60       1.16     bjh21 static int ifd = STDIN_FILENO, ofd = -1; /* Input/output file descriptors. */
     61       1.12     bjh21 static char *fname;		/* File name prefix. */
     62       1.14     bjh21 static size_t sfxlen = 2;		/* suffix length. */
     63        1.9  christos 
     64        1.9  christos static void newfile(void);
     65       1.26     joerg static void split1(off_t, int) __dead;
     66       1.26     joerg static void split2(off_t) __dead;
     67       1.26     joerg static void split3(off_t) __dead;
     68       1.23     perry static void usage(void) __dead;
     69       1.14     bjh21 static size_t bigwrite(int, void const *, size_t);
     70        1.1       cgd 
     71        1.4       jtc int
     72        1.9  christos main(int argc, char *argv[])
     73        1.1       cgd {
     74        1.4       jtc 	int ch;
     75        1.4       jtc 	char *ep, *p;
     76       1.18     bjh21 	char const *base;
     77       1.14     bjh21 	off_t bytecnt = 0;	/* Byte count to split on. */
     78       1.14     bjh21 	off_t numlines = 0;	/* Line count to split on. */
     79       1.22  jschauma 	off_t chunks = 0;	/* Number of chunks to split into. */
     80        1.4       jtc 
     81       1.22  jschauma 	while ((ch = getopt(argc, argv, "0123456789b:l:a:n:")) != -1)
     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.14     bjh21 					p++;
     93        1.4       jtc 				else
     94       1.14     bjh21 					p = argv[optind] + 1;
     95       1.14     bjh21 				numlines = strtoull(p, &ep, 10);
     96       1.14     bjh21 				if (numlines == 0 || *ep != '\0')
     97       1.14     bjh21 					errx(1, "%s: illegal line count.", p);
     98        1.1       cgd 			}
     99        1.4       jtc 			break;
    100        1.4       jtc 		case 'b':		/* Byte count. */
    101       1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    102       1.14     bjh21 			    (bytecnt = strtoull(optarg, &ep, 10)) == 0 ||
    103        1.6     lukem 			    (*ep != '\0' && *ep != 'k' && *ep != 'm'))
    104        1.4       jtc 				errx(1, "%s: illegal byte count.", optarg);
    105        1.4       jtc 			if (*ep == 'k')
    106        1.4       jtc 				bytecnt *= 1024;
    107        1.4       jtc 			else if (*ep == 'm')
    108        1.9  christos 				bytecnt *= 1024 * 1024;
    109        1.4       jtc 			break;
    110        1.4       jtc 		case 'l':		/* Line count. */
    111        1.4       jtc 			if (numlines != 0)
    112        1.4       jtc 				usage();
    113       1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    114       1.14     bjh21 			    (numlines = strtoull(optarg, &ep, 10)) == 0 ||
    115       1.14     bjh21 			    *ep != '\0')
    116        1.4       jtc 				errx(1, "%s: illegal line count.", optarg);
    117        1.4       jtc 			break;
    118       1.13     bjh21 		case 'a':		/* Suffix length. */
    119       1.14     bjh21 			if (!isdigit((unsigned char)optarg[0]) ||
    120       1.14     bjh21 			    (sfxlen = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
    121       1.14     bjh21 			    *ep != '\0')
    122       1.10     bjh21 				errx(1, "%s: illegal suffix length.", optarg);
    123       1.10     bjh21 			break;
    124       1.22  jschauma 		case 'n':		/* Chunks. */
    125       1.22  jschauma 			if (!isdigit((unsigned char)optarg[0]) ||
    126       1.22  jschauma 			    (chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
    127       1.22  jschauma 			    *ep != '\0')
    128       1.22  jschauma 				errx(1, "%s: illegal number of chunks.", optarg);
    129       1.22  jschauma 			break;
    130        1.4       jtc 		default:
    131        1.4       jtc 			usage();
    132        1.4       jtc 		}
    133        1.4       jtc 	argv += optind;
    134        1.4       jtc 	argc -= optind;
    135        1.4       jtc 
    136       1.15     bjh21 	if (*argv != NULL) {
    137       1.16     bjh21 		if (strcmp(*argv, "-") != 0 &&
    138       1.16     bjh21 		    (ifd = open(*argv, O_RDONLY, 0)) < 0)
    139       1.15     bjh21 			err(1, "%s", *argv);
    140       1.15     bjh21 		++argv;
    141       1.15     bjh21 	}
    142       1.10     bjh21 
    143       1.18     bjh21 
    144       1.18     bjh21 	base = (*argv != NULL) ? *argv++ : "x";
    145       1.19     bjh21 	if ((fname = malloc(strlen(base) + sfxlen + 1)) == NULL)
    146       1.18     bjh21 		err(EXIT_FAILURE, NULL);
    147       1.18     bjh21 	(void)strcpy(fname, base);		/* File name prefix. */
    148       1.10     bjh21 
    149        1.4       jtc 	if (*argv != NULL)
    150        1.4       jtc 		usage();
    151        1.4       jtc 
    152        1.4       jtc 	if (numlines == 0)
    153        1.4       jtc 		numlines = DEFLINE;
    154       1.22  jschauma 	else if (bytecnt || chunks)
    155       1.22  jschauma 		usage();
    156       1.22  jschauma 
    157       1.22  jschauma 	if (bytecnt && chunks)
    158        1.4       jtc 		usage();
    159        1.4       jtc 
    160       1.13     bjh21 	if (bytecnt)
    161       1.22  jschauma 		split1(bytecnt, 0);
    162       1.22  jschauma 	else if (chunks)
    163       1.22  jschauma 		split3(chunks);
    164       1.22  jschauma 	else
    165        1.9  christos 		split2(numlines);
    166       1.13     bjh21 
    167        1.9  christos 	return 0;
    168        1.1       cgd }
    169        1.1       cgd 
    170        1.1       cgd /*
    171        1.1       cgd  * split1 --
    172        1.4       jtc  *	Split the input by bytes.
    173        1.1       cgd  */
    174        1.9  christos static void
    175       1.22  jschauma split1(off_t bytecnt, int maxcnt)
    176        1.1       cgd {
    177       1.14     bjh21 	off_t bcnt;
    178       1.14     bjh21 	ssize_t dist, len;
    179        1.4       jtc 	char *C;
    180        1.9  christos 	char bfr[MAXBSIZE];
    181       1.22  jschauma 	int nfiles;
    182       1.22  jschauma 
    183       1.22  jschauma 	nfiles = 0;
    184        1.1       cgd 
    185        1.1       cgd 	for (bcnt = 0;;)
    186        1.4       jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    187        1.1       cgd 		case 0:
    188        1.4       jtc 			exit(0);
    189       1.13     bjh21 			/* NOTREACHED */
    190        1.4       jtc 		case -1:
    191        1.4       jtc 			err(1, "read");
    192        1.4       jtc 			/* NOTREACHED */
    193        1.1       cgd 		default:
    194        1.1       cgd 			if (!file_open) {
    195       1.22  jschauma 				if (!maxcnt || (nfiles < maxcnt)) {
    196       1.22  jschauma 					newfile();
    197       1.22  jschauma 					nfiles++;
    198       1.22  jschauma 					file_open = 1;
    199       1.22  jschauma 				}
    200        1.1       cgd 			}
    201        1.1       cgd 			if (bcnt + len >= bytecnt) {
    202       1.14     bjh21 				/* LINTED: bytecnt - bcnt <= len */
    203        1.1       cgd 				dist = bytecnt - bcnt;
    204       1.25     lukem 				if (bigwrite(ofd, bfr, dist) != (size_t)dist)
    205        1.4       jtc 					err(1, "write");
    206        1.1       cgd 				len -= dist;
    207        1.4       jtc 				for (C = bfr + dist; len >= bytecnt;
    208       1.14     bjh21 				    /* LINTED: bytecnt <= len */
    209        1.4       jtc 				    len -= bytecnt, C += bytecnt) {
    210       1.22  jschauma 					if (!maxcnt || (nfiles < maxcnt)) {
    211       1.22  jschauma 						newfile();
    212       1.22  jschauma 						nfiles++;
    213       1.22  jschauma 					}
    214       1.14     bjh21 					/* LINTED: as above */
    215        1.9  christos 					if (bigwrite(ofd,
    216       1.25     lukem 					    C, bytecnt) != (size_t)bytecnt)
    217        1.4       jtc 						err(1, "write");
    218        1.1       cgd 				}
    219        1.1       cgd 				if (len) {
    220       1.22  jschauma 					if (!maxcnt || (nfiles < maxcnt)) {
    221       1.22  jschauma 						newfile();
    222       1.22  jschauma 						nfiles++;
    223       1.22  jschauma 					}
    224       1.14     bjh21 					/* LINTED: len >= 0 */
    225       1.25     lukem 					if (bigwrite(ofd, C, len) != (size_t)len)
    226        1.4       jtc 						err(1, "write");
    227        1.4       jtc 				} else
    228        1.4       jtc 					file_open = 0;
    229        1.1       cgd 				bcnt = len;
    230        1.4       jtc 			} else {
    231        1.1       cgd 				bcnt += len;
    232       1.14     bjh21 				/* LINTED: len >= 0 */
    233       1.25     lukem 				if (bigwrite(ofd, bfr, len) != (size_t)len)
    234        1.4       jtc 					err(1, "write");
    235        1.1       cgd 			}
    236        1.1       cgd 		}
    237        1.1       cgd }
    238        1.1       cgd 
    239        1.1       cgd /*
    240        1.1       cgd  * split2 --
    241        1.4       jtc  *	Split the input by lines.
    242        1.1       cgd  */
    243        1.9  christos static void
    244       1.14     bjh21 split2(off_t numlines)
    245        1.1       cgd {
    246       1.14     bjh21 	off_t lcnt;
    247       1.14     bjh21 	size_t bcnt;
    248        1.9  christos 	ssize_t len;
    249        1.4       jtc 	char *Ce, *Cs;
    250        1.9  christos 	char bfr[MAXBSIZE];
    251        1.1       cgd 
    252        1.1       cgd 	for (lcnt = 0;;)
    253        1.4       jtc 		switch (len = read(ifd, bfr, MAXBSIZE)) {
    254        1.1       cgd 		case 0:
    255        1.1       cgd 			exit(0);
    256       1.13     bjh21 			/* NOTREACHED */
    257        1.4       jtc 		case -1:
    258        1.4       jtc 			err(1, "read");
    259        1.4       jtc 			/* NOTREACHED */
    260        1.1       cgd 		default:
    261        1.1       cgd 			if (!file_open) {
    262        1.1       cgd 				newfile();
    263        1.4       jtc 				file_open = 1;
    264        1.1       cgd 			}
    265        1.1       cgd 			for (Cs = Ce = bfr; len--; Ce++)
    266        1.1       cgd 				if (*Ce == '\n' && ++lcnt == numlines) {
    267        1.1       cgd 					bcnt = Ce - Cs + 1;
    268       1.25     lukem 					if (bigwrite(ofd, Cs, bcnt) != (size_t)bcnt)
    269        1.4       jtc 						err(1, "write");
    270        1.1       cgd 					lcnt = 0;
    271        1.1       cgd 					Cs = Ce + 1;
    272        1.1       cgd 					if (len)
    273        1.1       cgd 						newfile();
    274        1.1       cgd 					else
    275        1.4       jtc 						file_open = 0;
    276        1.1       cgd 				}
    277        1.1       cgd 			if (Cs < Ce) {
    278        1.1       cgd 				bcnt = Ce - Cs;
    279       1.25     lukem 				if (bigwrite(ofd, Cs, bcnt) != (size_t)bcnt)
    280        1.4       jtc 					err(1, "write");
    281        1.1       cgd 			}
    282        1.1       cgd 		}
    283        1.1       cgd }
    284        1.1       cgd 
    285        1.1       cgd /*
    286       1.22  jschauma  * split3 --
    287       1.22  jschauma  *	Split the input into specified number of chunks
    288       1.22  jschauma  */
    289       1.22  jschauma static void
    290       1.22  jschauma split3(off_t chunks)
    291       1.22  jschauma {
    292       1.22  jschauma 	struct stat sb;
    293       1.22  jschauma 
    294       1.22  jschauma 	if (fstat(ifd, &sb) == -1) {
    295       1.22  jschauma 		err(1, "stat");
    296       1.22  jschauma 		/* NOTREACHED */
    297       1.22  jschauma 	}
    298       1.22  jschauma 
    299       1.22  jschauma 	if (chunks > sb.st_size) {
    300       1.22  jschauma 		errx(1, "can't split into more than %d files",
    301       1.22  jschauma 				(int)sb.st_size);
    302       1.22  jschauma 		/* NOTREACHED */
    303       1.22  jschauma 	}
    304       1.22  jschauma 
    305       1.22  jschauma 	split1(sb.st_size/chunks, chunks);
    306       1.22  jschauma }
    307       1.22  jschauma 
    308       1.22  jschauma /*
    309        1.1       cgd  * newfile --
    310        1.4       jtc  *	Open a new output file.
    311        1.1       cgd  */
    312        1.9  christos static void
    313        1.9  christos newfile(void)
    314        1.1       cgd {
    315        1.9  christos 	static int fnum;
    316        1.1       cgd 	static char *fpnt;
    317       1.10     bjh21 	int quot, i;
    318        1.1       cgd 
    319        1.4       jtc 	if (ofd == -1) {
    320       1.18     bjh21 		fpnt = fname + strlen(fname);
    321       1.18     bjh21 		fpnt[sfxlen] = '\0';
    322       1.18     bjh21 	} else if (close(ofd) != 0)
    323       1.18     bjh21 		err(1, "%s", fname);
    324       1.18     bjh21 
    325       1.10     bjh21 	quot = fnum;
    326       1.10     bjh21 	for (i = sfxlen - 1; i >= 0; i--) {
    327       1.10     bjh21 		fpnt[i] = quot % 26 + 'a';
    328       1.10     bjh21 		quot = quot / 26;
    329       1.10     bjh21 	}
    330       1.18     bjh21 	if (quot > 0)
    331       1.18     bjh21 		errx(1, "too many files.");
    332        1.1       cgd 	++fnum;
    333       1.17     bjh21 	if ((ofd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE)) < 0)
    334        1.4       jtc 		err(1, "%s", fname);
    335        1.1       cgd }
    336        1.1       cgd 
    337       1.14     bjh21 static size_t
    338       1.14     bjh21 bigwrite(int fd, const void *buf, size_t len)
    339        1.9  christos {
    340        1.9  christos 	const char *ptr = buf;
    341       1.14     bjh21 	size_t sofar = 0;
    342       1.14     bjh21 	ssize_t w;
    343        1.9  christos 
    344        1.9  christos 	while (len != 0) {
    345       1.14     bjh21 		if  ((w = write(fd, ptr, len)) == -1)
    346        1.9  christos 			return sofar;
    347        1.9  christos 		len -= w;
    348        1.9  christos 		ptr += w;
    349        1.9  christos 		sofar += w;
    350        1.9  christos 	}
    351        1.9  christos 	return sofar;
    352        1.9  christos }
    353        1.9  christos 
    354        1.9  christos 
    355        1.9  christos static void
    356        1.9  christos usage(void)
    357        1.1       cgd {
    358        1.4       jtc 	(void)fprintf(stderr,
    359       1.22  jschauma "usage: %s [-b byte_count] [-l line_count] [-n chunk_count] [-a suffix_length] "
    360       1.10     bjh21 "[file [prefix]]\n", getprogname());
    361        1.1       cgd 	exit(1);
    362        1.1       cgd }
    363