]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
split: add new --hex-suffixes option
authorMichael Heimpold <mhei@heimpold.de>
Fri, 24 Mar 2017 20:36:39 +0000 (21:36 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 28 Mar 2017 03:32:58 +0000 (20:32 -0700)
* 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
doc/coreutils.texi
src/split.c
tests/split/numeric.sh

diff --git a/NEWS b/NEWS
index 6e7785483b6ef88789f8379b13b342197ae97d97..47d237be9b7f835d25aab4674cc4400b2d65902d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,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]
 
index 7edd1b90266bdc312e8c41c30fa4a9319ea5cebc..b8e24aa56f9d115af5a24688bde8e3dfc9b18503 100644 (file)
@@ -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}
index 01f97afadc8e0a06b3c7573381dad206b95746d3..a6fbd1d50a9daecec78a3e7d6929486006debd64 100644 (file)
@@ -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);
                 }
index 79277ca41dfc5ec2b0b016dbffcf0c71d411a5d6..d6b760b0b8813add64a06a178aef4d97bd4e4ae8 100755 (executable)
@@ -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.
 
 . "${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