Subject: bug#6554: [PATCH] split: Additional suffix for split (bug#6554)

From: Jérémy Compostella <[email-masked]>
Date: Mon, 06 Feb 2012 14:55:24 +0100

Pádraig, all,

I took into account general comments on my commits. I attached the
improved patch for feature. Improvements are:
- Add by file description,
- I put back accents on my name (Jérémy instead of Jeremy),
- I referenced from who this feature was requested.

Cheers,

Jérémy
---
>From 81e12245be60ef4e9b06ab18fa9e92bce81ea63c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Compostella?= <[email-masked]>
Date: Fri, 27 Jan 2012 18:14:34 +0100
Subject: [PATCH] split: Additional suffix for split (bug#6554)

Add support to an additionnal suffix with the new `--suffix=SUFF'
option.  SUFF is appended to each output filename right after the
dynamic suffix.

* src/split.c (next_file_name): Append fixed_suffix to outfile.
(main): Handle --suffix new option.
* NEWS (New features): Mention it.
* doc/coreutils.texi (split invocation): Mention it.
* tests/split/suffix: New file. --suffix option tests.
* tests/Makefile.am (TESTS): Add it.
Requested by Peng Yu.
---
 NEWS               |    6 ++++++
 doc/coreutils.texi |    6 ++++++
 src/split.c        |   24 ++++++++++++++++++++++--
 tests/Makefile.am  |    1 +
 tests/split/suffix |   41 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100755 tests/split/suffix

diff --git a/NEWS b/NEWS
index 9eebbf6..fa056e1 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,12 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** New features
+
+  split now accepts the new --suffix=SUFF option. When SUFF is
+  specified, SUFF is append to each output filenames right after the
+  dynamic suffix.
+
 ** Bug fixes
 
   mv now lets you move a symlink onto a same-inode destination file that
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 52838e7..db12dbb 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3083,6 +3083,12 @@ and so can be a pipe for example.
 @opindex --suffix-length
 Use suffixes of length @var{length}.  The default @var{length} is 2.
 
+@itemx --suffix=@var{suff}
+@opindex -a
+@opindex --suffix-length
+Append @var{suff} to each output filename right after the dynamic
+suffix. @var{suff} must not contain slash.
+
 @item -d
 @itemx --numeric-suffixes
 @opindex -d
diff --git a/src/split.c b/src/split.c
index 1d0310c..af532fa 100644
--- a/src/split.c
+++ b/src/split.c
@@ -80,6 +80,13 @@ static size_t suffix_length;
 /* Alphabet of characters to use in suffix.  */
 static char const *suffix_alphabet = "abcdefghijklmnopqrstuvwxyz";
 
+/* Length of fixed suffix. */
+static size_t fixed_suffix_len = 0;
+
+/* Fixed suffix to append to OUTFILE right after the dynamic
+   suffix.  */
+static char const *fixed_suffix;
+
 /* Name of input file.  May be "-".  */
 static char *infile;
 
@@ -110,7 +117,8 @@ enum
 {
   VERBOSE_OPTION = CHAR_MAX + 1,
   FILTER_OPTION,
-  IO_BLKSIZE_OPTION
+  IO_BLKSIZE_OPTION,
+  SUFFIX_OPTION
 };
 
 static struct option const longopts[] =
@@ -122,6 +130,7 @@ static struct option const longopts[] =
   {"elide-empty-files", no_argument, NULL, 'e'},
   {"unbuffered", no_argument, NULL, 'u'},
   {"suffix-length", required_argument, NULL, 'a'},
+  {"suffix", required_argument, NULL, SUFFIX_OPTION},
   {"numeric-suffixes", no_argument, NULL, 'd'},
   {"filter", required_argument, NULL, FILTER_OPTION},
   {"verbose", no_argument, NULL, VERBOSE_OPTION},
@@ -193,6 +202,8 @@ 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\
+      --suffix=SUFF       append SUFF to each output filename right after\n\
+                          the dynamic suffix. SUFF must not contain slash\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\
@@ -237,13 +248,15 @@ 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 outfile_length = outbase_length + suffix_length + fixed_suffix_len;
       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 (fixed_suffix_len)
+        memcpy (outfile_mid + suffix_length, fixed_suffix, fixed_suffix_len);
       outfile[outfile_length] = 0;
       sufindex = xcalloc (suffix_length, sizeof *sufindex);
 
@@ -1036,6 +1049,13 @@ main (int argc, char **argv)
           }
           break;
 
+        case SUFFIX_OPTION:
+          {
+            fixed_suffix = optarg;
+            fixed_suffix_len = strlen (fixed_suffix);
+          }
+          break;
+
         case 'b':
           if (split_type != type_undef)
             FAIL_ONLY_ONE_WAY ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a94aaa2..28a7ef6 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -254,6 +254,7 @@ TESTS =						\
   misc/sort-NaN-infloop				\
   split/filter					\
   split/suffix-length				\
+  split/suffix					\
   split/b-chunk					\
   split/fail					\
   split/lines					\
diff --git a/tests/split/suffix b/tests/split/suffix
new file mode 100755
index 0000000..8282359
--- /dev/null
+++ b/tests/split/suffix
@@ -0,0 +1,41 @@
+#!/bin/sh
+# show that 'split --suffix=SUFF' 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 --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
+
+Exit $fail
-- 
1.7.2.5