]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
split: support optional start value for --numeric-suffixes
authorJérémy Compostella <jeremy.compostella@gmail.com>
Sun, 29 Jan 2012 14:20:31 +0000 (15:20 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 18 Feb 2012 02:09:32 +0000 (02:09 +0000)
Allow changing the --numeric-suffixes start number
from the default of 0.

* src/split.c (next_file_name): Initialize the suffix index
and the output filename according to start value.
(main): Check that the suffix length is large enough for the
numerical suffix start value.
* doc/coreutils.texi (split invocation): Mention it.
* NEWS (New features): Mention it.
* tests/split/numeric: New file. Test --numeric-suffixes[=FROM].
* tests/Makefile.am (TESTS): Reference the new test.

NEWS
doc/coreutils.texi
src/split.c
tests/Makefile.am
tests/split/numeric [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 7973b5564e855ebab11fd0628f01b14d822800f3..593a1b4f2ab6e278519097c2cb91f74c7a8fc82c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   dd now accepts the count_bytes, skip_bytes iflags and the seek_bytes
   oflag, to more easily allow processing portions of a file.
 
+  split now accepts an optional "from" argument to --numeric-suffixes,
+  which changes the start number from the default of 0.
+
 ** Bug fixes
 
   mv now lets you move a symlink onto a same-inode destination file that
index 8c6a2870f4be97d257da711a98c84a27e9346232..9f378e8c43a5dc1272cdd0b79b7f8f1dd14951f9 100644 (file)
@@ -3084,10 +3084,11 @@ and so can be a pipe for example.
 Use suffixes of length @var{length}.  The default @var{length} is 2.
 
 @item -d
-@itemx --numeric-suffixes
+@itemx --numeric-suffixes[=@var{from}]
 @opindex -d
 @opindex --numeric-suffixes
-Use digits in suffixes rather than lower-case letters.
+Use digits in suffixes rather than lower-case letters.  The numerical
+suffix counts from @var{from} if specified, 0 otherwise.
 
 @item -e
 @itemx --elide-empty-files
index 1d0310cf2241e138c16972acf985846b93f55687..0e65001b80d5e5c35d22c4ee12aa8c361a49c9b3 100644 (file)
@@ -80,6 +80,9 @@ static size_t suffix_length;
 /* Alphabet of characters to use in suffix.  */
 static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
 
+/* Numerical suffix start value.  */
+static const char *numeric_suffix_start;
+
 /* Name of input file.  May be "-".  */
 static char *infile;
 
@@ -122,7 +125,7 @@ static struct option const longopts[] =
   {"elide-empty-files", no_argument, NULL, 'e'},
   {"unbuffered", no_argument, NULL, 'u'},
   {"suffix-length", required_argument, NULL, 'a'},
-  {"numeric-suffixes", no_argument, NULL, 'd'},
+  {"numeric-suffixes", optional_argument, NULL, 'd'},
   {"filter", required_argument, NULL, FILTER_OPTION},
   {"verbose", no_argument, NULL, VERBOSE_OPTION},
   {"-io-blksize", required_argument, NULL,
@@ -195,7 +198,8 @@ Mandatory arguments to long options are mandatory for short options too.\n\
   -a, --suffix-length=N   use suffixes of length N (default %d)\n\
   -b, --bytes=SIZE        put SIZE bytes per output file\n\
   -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file\n\
-  -d, --numeric-suffixes  use numeric suffixes instead of alphabetic\n\
+  -d, --numeric-suffixes[=FROM]  use numeric suffixes instead of alphabetic.\n\
+                                   FROM changes the start value (default 0).\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 per output file\n\
@@ -247,6 +251,18 @@ next_file_name (void)
       outfile[outfile_length] = 0;
       sufindex = xcalloc (suffix_length, sizeof *sufindex);
 
+      if (numeric_suffix_start)
+        {
+          /* Update the output file name.  */
+          size_t i = strlen (numeric_suffix_start);
+          memcpy (outfile_mid + suffix_length - i, numeric_suffix_start, i);
+
+          /* Update the suffix index.  */
+          size_t *sufindex_end = sufindex + suffix_length;
+          while (i-- != 0)
+            *--sufindex_end = numeric_suffix_start[i] - '0';
+        }
+
 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
       /* POSIX requires that if the output file name is too long for
          its directory, 'split' must fail without creating any files.
@@ -1142,6 +1158,23 @@ main (int argc, char **argv)
 
         case 'd':
           suffix_alphabet = "0123456789";
+          if (optarg)
+            {
+              if (strlen (optarg) != strspn (optarg, suffix_alphabet))
+                {
+                  error (0, 0,
+                         _("%s: invalid start value for numerical suffix"),
+                         optarg);
+                  usage (EXIT_FAILURE);
+                }
+              else
+                {
+                  /* Skip any leading zero.  */
+                  while (*optarg == '0' && *(optarg + 1) != '\0')
+                    optarg++;
+                  numeric_suffix_start = optarg;
+                }
+            }
           break;
 
         case 'e':
@@ -1212,6 +1245,15 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
+  /* Check that the suffix length is large enough for the numerical
+     suffix start value.  */
+  if (numeric_suffix_start && strlen (numeric_suffix_start) > suffix_length)
+    {
+      error (0, 0, _("numerical suffix start value is too large "
+                     "for the suffix length"));
+      usage (EXIT_FAILURE);
+    }
+
   /* Open the input file.  */
   if (! STREQ (infile, "-")
       && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
index 7b5368195b2c4a64387250b6b39f56762d3e40f7..eed6ed624ea7eb9d63da2e14d56ddfbab076257f 100644 (file)
@@ -259,6 +259,7 @@ TESTS =                                             \
   split/lines                                  \
   split/l-chunk                                        \
   split/r-chunk                                        \
+  split/numeric                                        \
   misc/stat-birthtime                          \
   misc/stat-fmt                                        \
   misc/stat-hyphen                             \
diff --git a/tests/split/numeric b/tests/split/numeric
new file mode 100755 (executable)
index 0000000..ad22df6
--- /dev/null
@@ -0,0 +1,65 @@
+#!/bin/sh
+# Show that split --numeric-suffixes[=from] works.
+
+# Copyright (C) 2012 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=.}/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
+
+# Check that split failed when suffix length is not large enough for
+# the numerical suffix start value
+split -a 3 --numeric-suffixes=1000 in 2> /dev/null && fail=1
+
+# check invalid --numeric-suffixes start values are flagged
+split --numeric-suffixes=-1 in 2> /dev/null && fail=1
+split --numeric-suffixes=one in 2> /dev/null && fail=1
+
+Exit $fail