summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJérémy Compostella <jeremy.compostella@gmail.com>2012-02-19 13:52:47 +0100
committerPádraig Brady <P@draigBrady.com>2012-02-20 13:30:26 +0000
commit69fed0dfecef60976e6144ea953493ec3efe517b (patch)
treec3aac97432a0d257cad1405586dad875c5892d6f
parent7f48aa570d93347aa72d86a034e36377db6a22b9 (diff)
downloadcoreutils-69fed0dfecef60976e6144ea953493ec3efe517b.tar.xz
split: add the --additional-suffix option
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
-rw-r--r--NEWS3
-rw-r--r--doc/coreutils.texi5
-rw-r--r--src/split.c27
-rw-r--r--tests/Makefile.am1
-rwxr-xr-xtests/split/additional-suffix44
5 files changed, 77 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 3b15d39c6..e2e8fc5c3 100644
--- 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
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 562444302..cc300a816 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -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
diff --git a/src/split.c b/src/split.c
index 0e65001b8..68c9a34e6 100644
--- a/src/split.c
+++ b/src/split.c
@@ -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 ();
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2fee97d18..74ff4702c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..8cfd3c1e6
--- /dev/null
+++ b/tests/split/additional-suffix
@@ -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