summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/coreutils.texi11
-rw-r--r--src/split.c22
-rwxr-xr-xtests/split/suffix-auto-length.sh21
3 files changed, 44 insertions, 10 deletions
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 6a69b75ca..4ae06c30b 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3181,9 +3181,14 @@ specified, will auto increase the length by 2 as required.
@opindex --numeric-suffixes
Use digits in suffixes rather than lower-case letters. The numerical
suffix counts from @var{from} if specified, 0 otherwise.
-Note specifying a @var{from} value also disables the default
-auto suffix length expansion described above, and so you may also
-want to specify @option{-a} to allow suffixes beyond @samp{99}.
+
+@var{from} is used to either set the initial suffix for a single run,
+or to set the suffix offset for independently split inputs, and consequently
+the auto suffix length expansion described above is disabled. Therefore
+you may also want to use option @option{-a} to allow suffixes beyond @samp{99}.
+Note if option @option{--number} is specified and the number of files is less
+than @var{from}, a single run is assumed and the minimum suffix length
+required is automatically determined.
@item --additional-suffix=@var{suffix}
@opindex --additional-suffix
diff --git a/src/split.c b/src/split.c
index 5d6043f70..b6fe2ddef 100644
--- a/src/split.c
+++ b/src/split.c
@@ -39,6 +39,7 @@
#include "sig2str.h"
#include "xfreopen.h"
#include "xdectoint.h"
+#include "xstrtol.h"
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "split"
@@ -173,9 +174,26 @@ set_suffix_length (uintmax_t n_units, enum Split_type split_type)
if (split_type == type_chunk_bytes || split_type == type_chunk_lines
|| split_type == type_rr)
{
+ uintmax_t n_units_end = n_units;
+ if (numeric_suffix_start)
+ {
+ uintmax_t n_start;
+ strtol_error e = xstrtoumax (numeric_suffix_start, NULL, 10,
+ &n_start, "");
+ if (e == LONGINT_OK && n_start <= UINTMAX_MAX - n_units)
+ {
+ /* Restrict auto adjustment so we don't keep
+ incrementing a suffix size arbitrarily,
+ as that would break sort order for files
+ generated from multiple split runs. */
+ if (n_start < n_units)
+ n_units_end += n_start;
+ }
+
+ }
size_t alphabet_len = strlen (suffix_alphabet);
- bool alphabet_slop = (n_units % alphabet_len) != 0;
- while (n_units /= alphabet_len)
+ bool alphabet_slop = (n_units_end % alphabet_len) != 0;
+ while (n_units_end /= alphabet_len)
suffix_needed++;
suffix_needed += alphabet_slop;
suffix_auto = false;
diff --git a/tests/split/suffix-auto-length.sh b/tests/split/suffix-auto-length.sh
index 6ee220066..6ef221a16 100755
--- a/tests/split/suffix-auto-length.sh
+++ b/tests/split/suffix-auto-length.sh
@@ -1,5 +1,5 @@
#!/bin/sh
-# Test the suffix auto widening functionality
+# Test the suffix auto width functionality
# Copyright (C) 2012-2015 Free Software Foundation, Inc.
@@ -20,14 +20,14 @@
print_ver_ split
-# ensure this feature is off when start number specified
-truncate -s12 file.in
+# ensure auto widening is off when start number specified
+truncate -s12 file.in || framework_failure_
returns_ 1 split file.in -b1 --numeric=89 || fail=1
test "$(ls -1 x* | wc -l)" = 11 || fail=1
rm -f x*
-# ensure this feature works when no start num specified
-truncate -s91 file.in
+# ensure auto widening works when no start num specified
+truncate -s91 file.in || framework_failure_
for prefix in 'x' 'xx' ''; do
for add_suffix in '.txt' ''; do
split file.in "$prefix" -b1 --numeric --additional-suffix="$add_suffix" \
@@ -39,4 +39,15 @@ for prefix in 'x' 'xx' ''; do
done
done
+# ensure auto width with --number and start num < number of files
+# That's the single run use case which is valid to adjust suffix len
+truncate -s100 file.in || framework_failure_
+split --numeric-suffixes=1 --number=r/100 file.in || fail=1
+rm -f x*
+
+# ensure no auto width with --number and start num >= number of files
+# That's the multi run use case which is invalid to adjust suffix len
+# as that would result in an incorrect order for the total output file set
+returns_ 1 split --numeric-suffixes=100 --number=r/100 file.in || fail=1
+
Exit $fail