/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
int
main(int argc, char *argv[])
{
int ch;
char *ep, *p;
char const *base;
off_t bytecnt = 0; /* Byte count to split on. */
off_t numlines = 0; /* Line count to split on. */
off_t chunks = 0; /* Number of chunks to split into. */
while ((ch = getopt(argc, argv, "0123456789a:b:l:n:")) != -1)
switch (ch) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/*
* Undocumented kludge: split was originally designed
* to take a number after a dash.
*/
if (numlines == 0) {
p = argv[optind - 1];
if (p[0] == '-' && p[1] == ch && !p[2])
p++;
else
p = argv[optind] + 1;
numlines = strtoull(p, &ep, 10);
if (numlines == 0 || *ep != '\0')
errx(EXIT_FAILURE, "%s: illegal line count.", p);
}
break;
case 'a': /* Suffix length. */
if (!isdigit((unsigned char)optarg[0]) ||
(sfxlen = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
*ep != '\0')
errx(EXIT_FAILURE, "%s: illegal suffix length.", optarg);
autosfx = 0;
break;
case 'b': /* Byte count. */
if (!isdigit((unsigned char)optarg[0]) ||
(bytecnt = strtoull(optarg, &ep, 10)) == 0 ||
(*ep != '\0' && *ep != 'k' && *ep != 'm'))
errx(EXIT_FAILURE, "%s: illegal byte count.", optarg);
if (*ep == 'k')
bytecnt *= 1024;
else if (*ep == 'm')
bytecnt *= 1024 * 1024;
break;
case 'l': /* Line count. */
if (numlines != 0)
usage();
if (!isdigit((unsigned char)optarg[0]) ||
(numlines = strtoull(optarg, &ep, 10)) == 0 ||
*ep != '\0')
errx(EXIT_FAILURE, "%s: illegal line count.", optarg);
break;
case 'n': /* Chunks. */
if (!isdigit((unsigned char)optarg[0]) ||
(chunks = (size_t)strtoul(optarg, &ep, 10)) == 0 ||
*ep != '\0')
errx(EXIT_FAILURE, "%s: illegal number of chunks.", optarg);
break;
default:
usage();
}
argv += optind;
argc -= optind;
/* If '-a' is not specified, then we automatically expand the
* suffix length to accommodate splitting all input. We do this
* by moving the suffix pointer (fpnt) forward and incrementing
* sfxlen by one, thereby yielding an additional two characters
* and allowing all output files to sort such that 'cat *' yields
* the input in order. I.e., the order is '... xyy xyz xzaaa
* xzaab ... xzyzy, xzyzz, xzzaaaa, xzzaaab' and so on. */
if (autosfx && (fpnt[0] == 'y') && (strspn(fpnt+1, "z") == strlen(fpnt+1))) {
if ((fname = realloc(fname, strlen(fname) + sfxlen + 2 + 1)) == NULL)
err(EXIT_FAILURE, NULL);
/* NOTREACHED */