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

From: Jérémy Compostella <[email-masked]>
Date: Fri, 27 Jan 2012 18:28:39 +0100

Eric, Peng, others,

> On 06/15/2010 09:23 PM, Peng Yu wrote:
> > I need to add an additional common suffix to the files splited by
> > split. Right now, I have to use mv to do so. But I feel it is
> > convenient to have an option to add the suffix. Is this feature going
> > to be considered to be added in the future?
> 
> Hmm, considering that we recently taught mktemp how to honor suffixes,
> it does indeed sound like this might be a useful feature addition.  It's
> probably not going to be my highest priority to write such a patch, but
> I'll gladly review any patch written by someone else.

I'm interested in this feature too so I wrote the attached patch which
implements it by adding the "--suffix=SUFF" option.

As this is my first contribution to this project, I'm not conformable
with coreutils coding standard or documentation needs, so feel free to
comment the submitted patch. I will take into account whatever you ask
for.

Cheers,

Jérémy
---
>From cc808cd440c0f578a4f2f36345f8f1bc48ebe0ae Mon Sep 17 00:00:00 2001
From: Jeremy Compostella <[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" option.

Signed-off-by: Jeremy Compostella <[email-masked]>
---
 src/split.c |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/split.c b/src/split.c
index 5fbce0e..c86a014 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. SUFF must\n\
+                          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 ();
-- 
1.7.2.5