split: add the --additional-suffix option
authorJérémy Compostella <jeremy.compostella@gmail.com>
Sun, 19 Feb 2012 12:52:47 +0000 (13:52 +0100)
committerPádraig Brady <P@draigBrady.com>
Mon, 20 Feb 2012 13:30:26 +0000 (13:30 +0000)
Add the --additional-suffix option, to append an
additional static suffix to output file names.

* src/split.c (next_file_name): Append suffix to output file names.
(main): Handle new --additional-suffix option.
* NEWS (New features): Mention it.
* doc/coreutils.texi (split invocation): Mention it.
* tests/split/additional-suffix: New file. Test --additional-suffix.
* tests/Makefile.am (TESTS): Add it.
Requested by Peng Yu, in bug 6554

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

diff --git a/NEWS b/NEWS
index 3b15d39..e2e8fc5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   split now accepts an optional "from" argument to --numeric-suffixes,
   which changes the start number from the default of 0.
 
+  split now accepts the --additional-suffix option, to append an
+  additional static suffix to output file names.
+
 ** Bug fixes
 
   mv now lets you move a symlink onto a same-inode destination file that
index 5624443..cc300a8 100644 (file)
@@ -3090,6 +3090,11 @@ Use suffixes of length @var{length}.  The default @var{length} is 2.
 Use digits in suffixes rather than lower-case letters.  The numerical
 suffix counts from @var{from} if specified, 0 otherwise.
 
+@itemx --additional-suffix=@var{suffix}
+@opindex --additional-suffix
+Append an additional @var{suffix} to output file names. @var{suffix}
+must not contain slash.
+
 @item -e
 @itemx --elide-empty-files
 @opindex -e
index 0e65001..68c9a34 100644 (file)
@@ -83,6 +83,9 @@ static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
 /* Numerical suffix start value.  */
 static const char *numeric_suffix_start;
 
+/* Additional suffix to append to output file names.  */
+static char const *additional_suffix;
+
 /* Name of input file.  May be "-".  */
 static char *infile;
 
@@ -113,7 +116,8 @@ enum
 {
   VERBOSE_OPTION = CHAR_MAX + 1,
   FILTER_OPTION,
-  IO_BLKSIZE_OPTION
+  IO_BLKSIZE_OPTION,
+  ADDITIONAL_SUFFIX_OPTION
 };
 
 static struct option const longopts[] =
@@ -125,6 +129,8 @@ static struct option const longopts[] =
   {"elide-empty-files", no_argument, NULL, 'e'},
   {"unbuffered", no_argument, NULL, 'u'},
   {"suffix-length", required_argument, NULL, 'a'},
+  {"additional-suffix", required_argument, NULL,
+   ADDITIONAL_SUFFIX_OPTION},
   {"numeric-suffixes", optional_argument, NULL, 'd'},
   {"filter", required_argument, NULL, FILTER_OPTION},
   {"verbose", no_argument, NULL, VERBOSE_OPTION},
@@ -195,7 +201,8 @@ is -, read standard input.\n\
 Mandatory arguments to long options are mandatory for short options too.\n\
 "), stdout);
       fprintf (stdout, _("\
-  -a, --suffix-length=N   use suffixes of length N (default %d)\n\
+  -a, --suffix-length=N   generate suffixes of length N (default %d)\n\
+      --additional-suffix=SUFFIX  append an additional SUFFIX to file names.\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[=FROM]  use numeric suffixes instead of alphabetic.\n\
@@ -241,13 +248,16 @@ next_file_name (void)
       /* Allocate and initialize the first file name.  */
 
       size_t outbase_length = strlen (outbase);
-      size_t outfile_length = outbase_length + suffix_length;
+      size_t addsuf_length = additional_suffix ? strlen (additional_suffix) : 0;
+      size_t outfile_length = outbase_length + suffix_length + addsuf_length;
       if (outfile_length + 1 < outbase_length)
         xalloc_die ();
       outfile = xmalloc (outfile_length + 1);
       outfile_mid = outfile + outbase_length;
       memcpy (outfile, outbase, outbase_length);
       memset (outfile_mid, suffix_alphabet[0], suffix_length);
+      if (additional_suffix)
+        memcpy (outfile_mid + suffix_length, additional_suffix, addsuf_length);
       outfile[outfile_length] = 0;
       sufindex = xcalloc (suffix_length, sizeof *sufindex);
 
@@ -1052,6 +1062,17 @@ main (int argc, char **argv)
           }
           break;
 
+        case ADDITIONAL_SUFFIX_OPTION:
+          if (last_component (optarg) != optarg)
+            {
+              error (0, 0,
+                     _("invalid suffix %s, contains directory separator"),
+                     quote (optarg));
+              usage (EXIT_FAILURE);
+            }
+          additional_suffix = optarg;
+          break;
+
         case 'b':
           if (split_type != type_undef)
             FAIL_ONLY_ONE_WAY ();
index 2fee97d..74ff470 100644 (file)
@@ -254,6 +254,7 @@ TESTS =                                             \
   misc/sort-NaN-infloop                                \
   split/filter                                 \
   split/suffix-length                          \
+  split/additional-suffix                      \
   split/b-chunk                                        \
   split/fail                                   \
   split/lines                                  \
diff --git a/tests/split/additional-suffix b/tests/split/additional-suffix
new file mode 100755 (executable)
index 0000000..8cfd3c1
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+# show that 'split --additional-suffix=SUFFIX' 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
+
+printf '1\n2\n3\n4\n5\n' > in || framework_failure_
+
+split --lines=2 --additional-suffix=.txt in > out || 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 xaa.txt || fail=1
+compare exp-2 xab.txt || fail=1
+compare exp-3 xac.txt || fail=1
+
+# Additional suffix must not contain slash
+split --lines=2 --additional-suffix=a/b in 2>/dev/null > out && fail=1
+
+Exit $fail