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

From: Jérémy Compostella <[email-masked]>
Date: Sun, 29 Jan 2012 00:57:59 +0100

Pádraig Brady wrotes:
> Thanks a lot for working on this.

> Could you give a real world example where
> you find this useful, just for the record.
It's useful in some cases where the chunk files type is known. As for
example, when I split a file which is a concatenation of several bitmap
files of the same size. Automatically append the appropriate suffix
"type" is time saving and comfortable. Another example is when the split
does not alter the file "type" as in the example you provided below,
input and output files are "txt" files. Having the appropriate suffix
automatically appended to the chunk file names make these files easily
available to applications which care about the file suffix.

> I should note that --suffix is incompatible
> with a variable length generated suffix.
> I.E. one that would allow for arbitrary sized input:
> http://lists.gnu.org/archive/html/bug-coreutils/2009-09/msg00220.html
> Though I guess that functionality could still be
> provided if required, by specifying --suffix-length=auto
> and having that mutually exclusive with --suffix.
First, after having carefully read the thread you are talking about I
did not find any implementation of this in the current coreutils
repository. Anyway, this feature is interesting and I would be glad to
implement it if needs too but in another commit.

Second, I don't get your point. Why it should not be possible to append
a fixed length suffix to output files with a variable length dynamic
suffix ? I do not figure out why it's incompatible. What do I
misunderstood ?

For example, the following hypothetical split command call looks
perfectly compatible:
$ split -n10 --suffix-length=auto --suffix=.txt file.txt file.
$ ls
  file.aa.txt
  file.ab.txt
  file.ac.txt
  file.ad.txt
  file.ae.txt
  file.af.txt
  file.ag.txt
  file.ah.txt
  file.ai.txt
  file.aj.txt
  [...]
  file.yv.txt
  file.yw.txt
  file.yx.txt
  file.yy.txt
  file.yz.txt
  file.zaaa.txt
  file.zaab.txt
  file.zaac.txt
  file.zaad.txt
  file.zaae.txt
  file.zaaf.txt

> As for the patch, it seems to work :)
> 
> t$ seq 10 > file.txt
> t$ ../split -n10 --suffix=.txt file.txt file.
> t$ l
> -rw-rw-r--. 1 padraig 21 Jan 28 14:06 file.txt
> -rw-rw-r--. 1 padraig  3 Jan 28 14:06 file.aj.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ai.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ah.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ag.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.af.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ae.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ad.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ac.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.ab.txt
> -rw-rw-r--. 1 padraig  2 Jan 28 14:06 file.aa.txt

> We'd need some corresponding documentation in doc/coreutils.texi,
> and an entry in NEWS.
I added the corresponding documentation in both of files. I've done my
best but feel free to comment my additions.

> Also even thought the patch is small, you'd need to start
> the copyright assignment process for a new parameter to `split`.
I filled out and sent my coreutils assignment form.

Cheers,

Jérémy
---
>From 587e15898a947cf22539bf7f782b7963284dc1e4 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=SUFF'
option.  SUFF is appended to each output filename right after the
dynamic suffix.

Signed-off-by: Jeremy Compostella <[email-masked]>
---
 NEWS               |    5 +++++
 doc/coreutils.texi |    6 ++++++
 src/split.c        |   24 ++++++++++++++++++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 2b0926f..398405d 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ 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.
 
 * Noteworthy changes in release 8.15 (2012-01-06) [stable]
 
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 0d3b739..9ccadf5 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 5fbce0e..fa7d30a 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 ();
-- 
1.7.2.5