Add support for the -a option, which should fix standards/11226.

Code from <hiramatu@boreas.dti.ne.jp>, with improvements from me.
Manual page updates from FreeBSD.
This commit is contained in:
bjh21 2003-06-10 16:32:54 +00:00
parent 5d17facb78
commit 2ef4680e17
2 changed files with 52 additions and 16 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: split.1,v 1.8 2001/12/08 19:16:19 wiz Exp $
.\" $NetBSD: split.1,v 1.9 2003/06/10 16:32:54 bjh21 Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@ -33,7 +33,7 @@
.\"
.\" @(#)split.1 8.3 (Berkeley) 4/16/94
.\"
.Dd April 16, 1994
.Dd June 10, 2003
.Dt SPLIT 1
.Os
.Sh NAME
@ -41,6 +41,7 @@
.Nd split a file into pieces
.Sh SYNOPSIS
.Nm
.Op Fl a Ar suffix_length
.Op Fl b Ar byte_count[k|m]
.Op Fl l Ar line_count
.Op Ar file Op Ar name
@ -54,6 +55,10 @@ and breaks it up into files of 1000 lines each.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl a
Use
.Ar suffix_length
letters to form the suffix of the file name.
.It Fl b
Create smaller files
.Ar byte_count
@ -79,19 +84,29 @@ of the input file which is to be split.
If a second additional argument is specified, it is used as a prefix
for the names of the files into which the file is split.
In this case, each file into which the file is split is named by the
prefix followed by a lexically ordered suffix in the range of
.Dq Li aa-zz .
prefix followed by a lexically ordered suffix using
.Ar suffix_length
characters in the range
.Dq Li a-z .
If
.Fl a
is not specified, two letters are used as the suffix.
.Pp
If the
.Ar name
argument is not specified, the file is split into lexically ordered
files named in the range of
.Dq Li xaa-zzz .
files named with prefixes in the range of
.Dq Li x-z
and with suffixes as above.
.Sh HISTORY
A
.Nm
command appeared in
.At v6 .
The
.Fl a
option was introduce in
.Nx 2.0 .
.Sh BUGS
For historical reasons, if you specify
.Ar name ,
@ -99,3 +114,6 @@ For historical reasons, if you specify
can only create 676 separate
files.
The default naming convention allows 2028 separate files.
The
.Fl a
option can be used to work around this limitation.

View File

@ -1,4 +1,4 @@
/* $NetBSD: split.c,v 1.9 2003/03/20 14:12:50 christos Exp $ */
/* $NetBSD: split.c,v 1.10 2003/06/10 16:32:54 bjh21 Exp $ */
/*
* Copyright (c) 1987, 1993, 1994
@ -43,7 +43,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)split.c 8.3 (Berkeley) 4/25/94";
#endif
__RCSID("$NetBSD: split.c,v 1.9 2003/03/20 14:12:50 christos Exp $");
__RCSID("$NetBSD: split.c,v 1.10 2003/06/10 16:32:54 bjh21 Exp $");
#endif /* not lint */
#include <sys/param.h>
@ -61,6 +61,7 @@ __RCSID("$NetBSD: split.c,v 1.9 2003/03/20 14:12:50 christos Exp $");
static int file_open; /* If a file open. */
static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
static char fname[MAXPATHLEN]; /* File name prefix. */
static int sfxlen = 2; /* suffix length. */
int main(int, char **);
static void newfile(void);
@ -77,7 +78,7 @@ main(int argc, char *argv[])
unsigned long long bytecnt = 0; /* Byte count to split on. */
unsigned long long numlines = 0;/* Line count to split on. */
while ((ch = getopt(argc, argv, "-0123456789b:l:")) != -1)
while ((ch = getopt(argc, argv, "-0123456789b:l:a:")) != -1)
switch (ch) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
@ -117,6 +118,10 @@ main(int argc, char *argv[])
if ((numlines = strtoull(optarg, &ep, 10)) <= 0 || *ep)
errx(1, "%s: illegal line count.", optarg);
break;
case 'a': /* Suffix length. */
if ((sfxlen = strtol(optarg, &ep, 10)) <= 0 || *ep)
errx(1, "%s: illegal suffix length.", optarg);
break;
default:
usage();
}
@ -129,8 +134,16 @@ main(int argc, char *argv[])
err(1, "%s", *argv);
++argv;
}
if (*argv != NULL) /* File name prefix. */
(void)strcpy(fname, *argv++);
if (*argv != NULL) {
if (strlen(*argv) + sfxlen > NAME_MAX)
errx(EXIT_FAILURE, "Output file name too long");
(void)strcpy(fname, *argv++); /* File name prefix. */
} else {
if (1 + sfxlen > NAME_MAX)
errx(EXIT_FAILURE, "Output file name too long");
}
if (*argv != NULL)
usage();
@ -255,6 +268,7 @@ newfile(void)
static int fnum;
static int defname;
static char *fpnt;
int quot, i;
if (ofd == -1) {
if (fname[0] == '\0') {
@ -271,15 +285,18 @@ newfile(void)
* Hack to increase max files; original code wandered through
* magic characters. Maximum files is 3 * 26 * 26 == 2028
*/
#define MAXFILES 676
if (fnum == MAXFILES) {
fpnt[sfxlen] = '\0';
quot = fnum;
for (i = sfxlen - 1; i >= 0; i--) {
fpnt[i] = quot % 26 + 'a';
quot = quot / 26;
}
if (quot > 0) {
if (!defname || fname[0] == 'z')
errx(1, "too many files.");
++fname[0];
fnum = 0;
}
fpnt[0] = fnum / 26 + 'a';
fpnt[1] = fnum % 26 + 'a';
++fnum;
if (!freopen(fname, "w", stdout))
err(1, "%s", fname);
@ -307,6 +324,7 @@ static void
usage(void)
{
(void)fprintf(stderr,
"Usage: %s [-b byte_count] [-l line_count] [file [prefix]]\n", getprogname());
"Usage: %s [-b byte_count] [-l line_count] [-a suffix_length] "
"[file [prefix]]\n", getprogname());
exit(1);
}