From 9a4296b5c44d29df4a452a7b61749490280caf1e Mon Sep 17 00:00:00 2001 From: Michael Heimpold Date: Fri, 24 Mar 2017 21:36:39 +0100 Subject: [PATCH] split: add new --hex-suffixes option * doc/coreutils.texi (split invocation): Document the new option. * src/split.c (usage): Likewise. (main): Process the new option much like --numeric-suffixes, but with an adjusted alphabet. * tests/split/numeric.sh: Refactor to support --hex mode. * NEWS: Mention the new feature. --- NEWS | 5 ++++ doc/coreutils.texi | 6 +++++ src/split.c | 15 +++++++++--- tests/split/numeric.sh | 64 ++++++++++++++++++++------------------------------ 4 files changed, 48 insertions(+), 42 deletions(-) diff --git a/NEWS b/NEWS index 4604318..0281042 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,11 @@ GNU coreutils NEWS -*- outline -*- ignoring some non checksum lines. This also affects sha*sum and b2sum. [bug introduced in coreutils-8.14] +** New features + + split supports a new --hex-suffixes[=from] option to create files with + lower case hexadecimal suffixes, similar to the --numeric-suffixes option. + * Noteworthy changes in release 8.27 (2017-03-08) [stable] diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 7edd1b9..b8e24aa 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -3243,6 +3243,12 @@ 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 -x +@itemx --hex-suffixes[=@var{from}] +@opindex -x +@opindex --hex-suffixes +Like @option{--numeric-suffixes}, but use hexadecimal numbers (in lower case). + @item --additional-suffix=@var{suffix} @opindex --additional-suffix Append an additional @var{suffix} to output file names. @var{suffix} diff --git a/src/split.c b/src/split.c index 01f97af..a6fbd1d 100644 --- a/src/split.c +++ b/src/split.c @@ -141,6 +141,7 @@ static struct option const longopts[] = {"additional-suffix", required_argument, NULL, ADDITIONAL_SUFFIX_OPTION}, {"numeric-suffixes", optional_argument, NULL, 'd'}, + {"hex-suffixes", optional_argument, NULL, 'x'}, {"filter", required_argument, NULL, FILTER_OPTION}, {"verbose", no_argument, NULL, VERBOSE_OPTION}, {"separator", required_argument, NULL, 't'}, @@ -242,6 +243,8 @@ default size is 1000 lines, and default PREFIX is 'x'.\n\ -d use numeric suffixes starting at 0, not alphabetic\n\ --numeric-suffixes[=FROM] same as -d, but allow setting the start value\ \n\ + -x use hex suffixes starting at 0, not alphabetic\n\ + --hex-suffixes[=FROM] same as -x, but allow setting the start value\n\ -e, --elide-empty-files do not generate empty output files with '-n'\n\ --filter=COMMAND write to shell COMMAND; file name is $FILE\n\ -l, --lines=NUMBER put NUMBER lines/records per output file\n\ @@ -1322,7 +1325,7 @@ main (int argc, char **argv) int this_optind = optind ? optind : 1; char *slash; - c = getopt_long (argc, argv, "0123456789C:a:b:del:n:t:u", + c = getopt_long (argc, argv, "0123456789C:a:b:del:n:t:ux", longopts, NULL); if (c == -1) break; @@ -1461,13 +1464,19 @@ main (int argc, char **argv) break; case 'd': - suffix_alphabet = "0123456789"; + case 'x': + if (c == 'd') + suffix_alphabet = "0123456789"; + else + suffix_alphabet = "0123456789abcdef"; if (optarg) { if (strlen (optarg) != strspn (optarg, suffix_alphabet)) { error (0, 0, - _("%s: invalid start value for numerical suffix"), + (c == 'd') ? + _("%s: invalid start value for numerical suffix") : + _("%s: invalid start value for hexadecimal suffix"), quote (optarg)); usage (EXIT_FAILURE); } diff --git a/tests/split/numeric.sh b/tests/split/numeric.sh index 79277ca..d6b760b 100755 --- a/tests/split/numeric.sh +++ b/tests/split/numeric.sh @@ -1,5 +1,5 @@ #!/bin/sh -# Show that split --numeric-suffixes[=from] works. +# Test --{hex,numeric}-suffixes[=from] # Copyright (C) 2012-2017 Free Software Foundation, Inc. @@ -19,47 +19,33 @@ . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src print_ver_ split -# Check default start from 0 printf '1\n2\n3\n4\n5\n' > in || framework_failure_ -split --numeric-suffixes --lines=2 in || fail=1 -cat <<\EOF > exp-1 -1 -2 -EOF -cat <<\EOF > exp-2 -3 -4 -EOF -cat <<\EOF > exp-3 -5 -EOF -compare exp-1 x00 || fail=1 -compare exp-2 x01 || fail=1 -compare exp-3 x02 || fail=1 -# Check --numeric-suffixes=X -split --numeric-suffixes=1 --lines=2 in || fail=1 -cat <<\EOF > exp-1 -1 -2 -EOF -cat <<\EOF > exp-2 -3 -4 -EOF -cat <<\EOF > exp-3 -5 -EOF -compare exp-1 x01 || fail=1 -compare exp-2 x02 || fail=1 -compare exp-3 x03 || fail=1 +printf '%s\n' 1 2 > exp-0 || framework_failure_ +printf '%s\n' 3 4 > exp-1 || framework_failure_ +printf '%s\n' 5 > exp-2 || framework_failure_ -# Check that split failed when suffix length is not large enough for -# the numerical suffix start value -returns_ 1 split -a 3 --numeric-suffixes=1000 in 2>/dev/null || fail=1 +for mode in 'numeric' 'hex'; do -# check invalid --numeric-suffixes start values are flagged -returns_ 1 split --numeric-suffixes=-1 in 2> /dev/null || fail=1 -returns_ 1 split --numeric-suffixes=one in 2> /dev/null || fail=1 + for start in 0 9; do + mode_option="--$mode-suffixes" + # check with and without specified start value + test $start != '0' && mode_option="$mode_option=$start" + split $mode_option --lines=2 in || fail=1 + + test $mode = 'hex' && format=x || format=d + for i in $(seq $start $(($start+2))); do + compare exp-$(($i-$start)) x$(printf %02$format $i) || fail=1 + done + done + + # Check that split failed when suffix length is not large enough for + # the numerical suffix start value + returns_ 1 split -a 3 --$mode-suffixes=1000 in 2>/dev/null || fail=1 + + # check invalid --$mode-suffixes start values are flagged + returns_ 1 split --$mode-suffixes=-1 in 2> /dev/null || fail=1 + returns_ 1 split --$mode-suffixes=one in 2> /dev/null || fail=1 +done Exit $fail -- 2.9.3