split(1): auto-extend suffix length if required

If the input cannot be split into the number of files resulting from the
default suffix length, automatically extend the suffix length rather
than bailing out with 'too many files'.

Suffixes are extended such that the resulting files continue to sort
lexically and "cat *" would reproduce the input. For example, splitting
a 1M lines file into (default) 1000 lines per file would yield files
named 'xaa', 'xab', ..., 'xyy', 'xyz', 'xzaaa', 'xzaab', ..., 'xzanl'.

If '-a' is specified, the suffix length is not auto-extended.

This behavior matches GNU sort(1) since around version 8.16.

Reviewed by:	christos
Approved by:	kevans
Different Revision:	https://reviews.freebsd.org/D38279
This commit is contained in:
Jan Schaumann 2023-05-30 15:55:38 +03:00 committed by Christos Margiolis
parent c45d6b0ec0
commit c4f7198f47
2 changed files with 37 additions and 2 deletions

View file

@ -28,7 +28,7 @@
.\" @(#)split.1 8.3 (Berkeley) 4/16/94
.\" $FreeBSD$
.\"
.Dd April 18, 2023
.Dd May 26, 2023
.Dt SPLIT 1
.Os
.Sh NAME
@ -151,7 +151,11 @@ characters in the range
.Dq Li a Ns - Ns Li z .
If
.Fl a
is not specified, two letters are used as the suffix.
is not specified, two letters are used as the initial suffix.
If the output does not fit into the resulting number of files and the
.Fl d
flag is not specified, then the suffix length is automatically extended as
needed such that all output files continue to sort in lexical order.
.Pp
If the
.Ar prefix

View file

@ -75,6 +75,7 @@ static regex_t rgx;
static int pflag;
static bool dflag;
static long sufflen = 2; /* File name suffix length. */
static int autosfx = 1; /* Whether to auto-extend the suffix length. */
static void newfile(void);
static void split1(void);
@ -116,6 +117,7 @@ main(int argc, char **argv)
if ((sufflen = strtol(optarg, &ep, 10)) <= 0 || *ep)
errx(EX_USAGE,
"%s: illegal suffix length", optarg);
autosfx = 0;
break;
case 'b': /* Byte count. */
errno = 0;
@ -366,6 +368,35 @@ newfile(void)
}
pattlen = end - beg + 1;
/*
* If '-a' is not specified, then we automatically expand the
* suffix length to accomodate splitting all input. We do this
* by moving the suffix pointer (fpnt) forward and incrementing
* sufflen 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 (!dflag && autosfx && (fpnt[0] == 'y') &&
strspn(fpnt+1, "z") == strlen(fpnt+1)) {
fpnt = fname + strlen(fname) - sufflen;
fpnt[sufflen + 2] = '\0';
fpnt[0] = end;
fpnt[1] = beg;
/* Basename | Suffix
* before:
* x | yz
* after:
* xz | a.. */
fpnt++;
sufflen++;
/* Reset so we start back at all 'a's in our extended suffix. */
tfnum = 0;
fnum = 0;
}
/* maxfiles = pattlen^sufflen, but don't use libm. */
for (maxfiles = 1, i = 0; i < sufflen; i++)
if (LONG_MAX / pattlen < maxfiles)