summaryrefslogtreecommitdiff
path: root/src/split.c
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2015-05-06 01:48:40 +0100
committerPádraig Brady <P@draigBrady.com>2015-05-13 12:43:40 +0100
commit703747f892d3ec4713b97d4bff770a80122cd1ef (patch)
tree97caf65ccdb43c5f01785ade78dbaf29c3cfc9ae /src/split.c
parent79111d1553ff6603199b9e8e5f27269d55b095fb (diff)
downloadcoreutils-703747f892d3ec4713b97d4bff770a80122cd1ef.tar.xz
split: auto set suffix len for --numeric-suffixes=<N --number=N
Supporting `split --numeric-suffixes=1 -n100` for example. * doc/coreutils.texi (split invocation): Mention the two use cases for the FROM parameter, and the consequences on the suffix length determination. * src/split.c (set_suffix_length): Use the --numeric-suffixes FROM parameter in the suffix width calculation, when it's less than the number of files specified in --number. * tests/split/suffix-auto-length.sh: Add test cases. Fixes http://bugs.gnu.org/20511
Diffstat (limited to 'src/split.c')
-rw-r--r--src/split.c22
1 files changed, 20 insertions, 2 deletions
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;