From bd80ac876c64fa0fac409b24311d30bd43d34dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Compostella?= Date: Sun, 19 Feb 2012 13:52:47 +0100 Subject: [PATCH] split: additional suffix for split (bug#6554) Add the --additional-suffix option, to append an additional suffix to output file names. * src/split.c (next_file_name): Append additionnal_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. --additional-suffix option tests. * tests/Makefile.am (TESTS): Add it. Requested by Peng Yu. --- NEWS | 3 ++ doc/coreutils.texi | 5 ++++ src/split.c | 26 ++++++++++++++++++++++- tests/Makefile.am | 1 + tests/split/additional-suffix | 44 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100755 tests/split/additional-suffix diff --git a/NEWS b/NEWS index 3b15d39..e6ca61f 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 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 5624443..cc300a8 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. address@hidden address@hidden address@hidden --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 0e65001..c21870c 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}, @@ -196,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\ + --additional-suffix=SUFFIX append an additional SUFFIX to output file\n\ + names. SUFFIX 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[=FROM] use numeric suffixes instead of alphabetic.\n\ @@ -241,13 +249,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 +1063,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 2fee97d..74ff470 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 0000000..8cfd3c1 --- /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 . + +. "${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 -- 1.7.2.5