coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] split: add parameter -x to allow hexadecimal suffixes


From: Michael Heimpold
Subject: [PATCH v2] split: add parameter -x to allow hexadecimal suffixes
Date: Fri, 24 Mar 2017 21:36:39 +0100

---

Hi Pádraig,

thanks for your last response. Yes, I know that this seems to be
edge case usage, but I also don't see that this may hurt anybody.

So I've tried to make this patch complete, just to have it pushed
it out. But feel free to consider it or not. I'll be fine with
every decision :-)

Best regards,
Michael


Changelog:

v2:
- added test and documentation


v1:
- initial submission

 doc/coreutils.texi         |  7 +++++
 src/split.c                | 18 +++++++++----
 tests/local.mk             |  1 +
 tests/split/hexadecimal.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 5 deletions(-)
 create mode 100755 tests/split/hexadecimal.sh

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 7edd1b9..6232d6e 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3243,6 +3243,13 @@ 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 --hexadecimal-suffixes[=@var{from}]
+@opindex -x
+@opindex --hexadecimal-suffixes
+This is almost the same as @option{--numeric-suffixes}, but hexadecimal
+numbers are used.
+
 @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 9662336..e9adee8 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'},
+  {"hexadecimal-suffixes", optional_argument, NULL, 'x'},
   {"filter", required_argument, NULL, FILTER_OPTION},
   {"verbose", no_argument, NULL, VERBOSE_OPTION},
   {"separator", required_argument, NULL, 't'},
@@ -240,8 +241,9 @@ default size is 1000 lines, and default PREFIX is 'x'.\n\
   -b, --bytes=SIZE        put SIZE bytes per output file\n\
   -C, --line-bytes=SIZE   put at most SIZE bytes of records per output file\n\
   -d                      use numeric suffixes starting at 0, not alphabetic\n\
-      --numeric-suffixes[=FROM]  same as -d, but allow setting the start value\
-\n\
+      --numeric-suffixes[=FROM]  same as -d, but allow setting the start 
value\n\
+  -x                      use hexadecimal suffixes starting at 0, not 
alphabetic\n\
+      --hexadecimal-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\
@@ -1314,7 +1316,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;
@@ -1453,13 +1455,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/local.mk b/tests/local.mk
index 9f1a853..9b38169 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -377,6 +377,7 @@ all_tests =                                 \
   tests/split/r-chunk.sh                       \
   tests/split/record-sep.sh                    \
   tests/split/numeric.sh                       \
+  tests/split/hexadecimal.sh                   \
   tests/split/guard-input.sh                   \
   tests/misc/stat-birthtime.sh                 \
   tests/misc/stat-fmt.sh                       \
diff --git a/tests/split/hexadecimal.sh b/tests/split/hexadecimal.sh
new file mode 100755
index 0000000..fbac01b
--- /dev/null
+++ b/tests/split/hexadecimal.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+# Show that split --hexadecimal-suffixes[=from] works.
+
+# Copyright (C) 2012-2017 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${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 --hexadecimal-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 --hexadecimal-suffixes=X
+split --hexadecimal-suffixes=9 --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 x09 || fail=1
+compare exp-2 x0a || fail=1
+compare exp-3 x0b || fail=1
+
+# Check that split failed when suffix length is not large enough for
+# the hexadecimal suffix start value
+returns_ 1 split -a 3 --hexadecimal-suffixes=abcd in 2>/dev/null || fail=1
+
+# check invalid --hexadecimal-suffixes start values are flagged
+returns_ 1 split --hexadecimal-suffixes=-1 in 2> /dev/null || fail=1
+returns_ 1 split --hexadecimal-suffixes=one in 2> /dev/null || fail=1
+
+Exit $fail
-- 
2.7.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]