Okay, here's a version that supports argument input in --files0-from=F style.
Bo
From 3108b79cbbb5d6c2fe3c2f8d5037f166cb0f1ca6 Mon Sep 17 00:00:00 2001
From: Bo Borgerson <[EMAIL PROTECTED]>
Date: Thu, 3 Apr 2008 18:42:57 -0400
Subject: [PATCH] Add new sort option --files0-from=F
src/sort.c: support new option
tests/misc/sort-files0-from: test new option
tests/misc/Makefile.am: indicate new test
docs/coreutils.texti: explain new option
NEWS: advertise new option
Signed-off-by: Bo Borgerson <[EMAIL PROTECTED]>
---
NEWS | 5 ++
doc/coreutils.texi | 16 +++++++
src/sort.c | 58 +++++++++++++++++++++++-
tests/misc/Makefile.am | 1 +
tests/misc/sort-files0-from | 105 +++++++++++++++++++++++++++++++++++++++++++
5 files changed, 183 insertions(+), 2 deletions(-)
create mode 100755 tests/misc/sort-files0-from
diff --git a/NEWS b/NEWS
index e208b30..492c4e9 100644
--- a/NEWS
+++ b/NEWS
@@ -55,6 +55,11 @@ GNU coreutils NEWS -*- outline -*-
options --general-numeric-sort/-g, --month-sort/-M, --numeric-sort/-n
and --random-sort/-R, resp.
+ sort accepts a new option, --files0-from=F, that specifies a file
+ containing a null-separated list of files to sort. This list is used
+ instead of filenames passed on the command-line to avoid problems with
+ maximum command-line (argv) length.
+
** Improvements
id and groups work around an AFS-related bug whereby those programs
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index ee7dbb2..5415394 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3667,6 +3667,22 @@ Terminate with an error if @var{prog} exits with nonzero status.
Whitespace and the backslash character should not appear in
@var{prog}; they are reserved for future use.
[EMAIL PROTECTED] [EMAIL PROTECTED]
[EMAIL PROTECTED] [EMAIL PROTECTED]
[EMAIL PROTECTED] including files from @command{du}
+Rather than processing files named on the command line, process those
+named in file @var{FILE}; each name is terminated by a null byte.
+This is useful when the list of file names is so long that it may exceed
+a command line length limitation.
+In such cases, running @command{sort} via @command{xargs} is undesirable
+because it splits the list into pieces and gives each piece to a different
+instance of @command{sort}, with the resulting output being multiple sets
+of sorted data concatenated together.
+One way to produce a list of null-byte-terminated file names is with @sc{gnu}
[EMAIL PROTECTED], using its @option{-print0} predicate.
+
+Do not specify any @var{FILE} on the command line when using this option.
+
@item -k @var{pos1}[,@var{pos2}]
@itemx [EMAIL PROTECTED],@var{pos2}]
@opindex -k
diff --git a/src/sort.c b/src/sort.c
index 8b2eec5..8342399 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -37,6 +37,7 @@
#include "posixver.h"
#include "quote.h"
#include "randread.h"
+#include "readtokens0.h"
#include "stdio--.h"
#include "stdlib--.h"
#include "strnumcmp.h"
@@ -304,8 +305,9 @@ usage (int status)
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
+ or: %s [OPTION]... --files0-from=F\n\
"),
- program_name);
+ program_name, program_name);
fputs (_("\
Write sorted concatenation of all FILE(s) to standard output.\n\
\n\
@@ -342,6 +344,9 @@ Other options:\n\
-C, --check=quiet, --check=silent like -c, but do not report first bad line\n\
--compress-program=PROG compress temporaries with PROG;\n\
decompress them with PROG -d\n\
+ --files0-from=F read input from the files specified by\n\
+ NUL-terminated names in file F\n\
+ -L, --max-line-length print the length of the longest line\n\
-k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)\n\
-m, --merge merge already sorted files; do not sort\n\
"), stdout);
@@ -395,7 +400,8 @@ enum
CHECK_OPTION = CHAR_MAX + 1,
COMPRESS_PROGRAM_OPTION,
RANDOM_SOURCE_OPTION,
- SORT_OPTION
+ SORT_OPTION,
+ FILES0_FROM_OPTION
};
static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z";
@@ -407,6 +413,7 @@ static struct option const long_options[] =
{"compress-program", required_argument, NULL, COMPRESS_PROGRAM_OPTION},
{"dictionary-order", no_argument, NULL, 'd'},
{"ignore-case", no_argument, NULL, 'f'},
+ {"files0-from", required_argument, NULL, FILES0_FROM_OPTION},
{"general-numeric-sort", no_argument, NULL, 'g'},
{"ignore-nonprinting", no_argument, NULL, 'i'},
{"key", required_argument, NULL, 'k'},
@@ -2752,6 +2759,8 @@ main (int argc, char **argv)
bool posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
bool obsolete_usage = (posix2_version () < 200112);
char **files;
+ char *files_from = NULL;
+ struct Tokens tok;
char const *outfile = NULL;
initialize_main (&argc, &argv);
@@ -2955,6 +2964,10 @@ main (int argc, char **argv)
compress_program = optarg;
break;
+ case FILES0_FROM_OPTION:
+ files_from = optarg;
+ break;
+
case 'k':
key = key_init (&key_buf);
@@ -3099,6 +3112,47 @@ main (int argc, char **argv)
}
}
+ if (files_from)
+ {
+ FILE *stream;
+
+ /* When using --files0-from=F, you may not specify any files
+ on the command-line. */
+ if (nfiles)
+ {
+ error (0, 0, _("extra operand %s"), quote (files[0]));
+ fprintf (stderr, "%s\n",
+ _("File operands cannot be combined with --files0-from."));
+ usage (SORT_FAILURE);
+ }
+
+ if (STREQ (files_from, "-"))
+ stream = stdin;
+ else
+ {
+ stream = fopen (files_from, "r");
+ if (stream == NULL)
+ error (SORT_FAILURE, errno, _("cannot open %s for reading"),
+ quote (files_from));
+ }
+
+ readtokens0_init (&tok);
+
+ if (! readtokens0 (stream, &tok) || fclose (stream) != 0)
+ error (SORT_FAILURE, 0, _("cannot read file names from %s"),
+ quote (files_from));
+
+ if (tok.n_tok)
+ {
+ free (files);
+ files = tok.tok;
+ nfiles = tok.n_tok;
+ }
+ else
+ error (SORT_FAILURE, 0, _("no input from %s"),
+ quote (files_from));
+ }
+
/* Inheritance of global options to individual keys. */
for (key = keylist; key; key = key->next)
{
diff --git a/tests/misc/Makefile.am b/tests/misc/Makefile.am
index 17a0ec0..7fcd0b8 100644
--- a/tests/misc/Makefile.am
+++ b/tests/misc/Makefile.am
@@ -98,6 +98,7 @@ TESTS = \
shred-remove \
shuf \
sort-compress \
+ sort-files0-from \
sort-merge \
sort-rand \
split-a \
diff --git a/tests/misc/sort-files0-from b/tests/misc/sort-files0-from
new file mode 100755
index 0000000..c49b72e
--- /dev/null
+++ b/tests/misc/sort-files0-from
@@ -0,0 +1,105 @@
+#!/bin/sh
+# Test "sort --files0-from=F".
+
+# Copyright (C) 2002, 2003, 2005-2008 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=.}
+. $srcdir/../require-perl
+
+me=`echo $0|sed 's,.*/,,'`
+exec $PERL -w -I$srcdir/.. -MCoreutils -M"CuTmpdir qw($me)" -- - <<\EOF
+require 5.003;
+use strict;
+
+(my $program_name = $0) =~ s|.*/||;
+my $prog = 'sort';
+
+# Turn off localisation of executable's ouput.
[EMAIL PROTECTED](LANGUAGE LANG LC_ALL)} = ('C') x 3;
+
+`echo a > in1`;
+`echo b > in2`;
+`echo c > in3`;
+
+my $fn = "arglist1";
+my $list0 = "in1\0in2\0in3\0";
+open my $fh, ">", $fn or warn "Couldn't open '$fn' for write: $!";
+print $fh $list0;
+close $fh;
+
+`touch empty`;
+
+my $usage_err = <<EO_USAGE;
+$prog: extra operand `extra'
+File operands cannot be combined with --files0-from.
+Try `sort --help' for more information.
+EO_USAGE
+
+my @Tests =
+ (
+
+ # nonexistent files0-from input file
+ ['f1', "--files0-from=nonexistent", {EXIT => 2},
+ {ERR_SUBST => 's/reading:.+/reading/'},
+ {ERR => "$prog: cannot open `nonexistent' for reading\n"}],
+
+ # empty files0-from input file
+ ['f2', "--files0-from=empty", {EXIT => 2},
+ {ERR => "$prog: no input from `empty'\n"}],
+
+ # command-line argument wth files0-from input
+ ['f3', "--files0-from=$fn", {IN => {extra => 'operand'}}, {EXIT => 2},
+ {ERR => $usage_err}],
+
+ # successful files0-from file
+ ['f4', "--files0-from=$fn", {OUT => "a\nb\nc\n"}],
+
+ # successful files0-from stdin
+ ['f5', '--files0-from=-', '<', {IN => $list0 }, {OUT => "a\nb\nc\n"}],
+
+ # one NUL
+ ['nul-1', '--files0-from=-', '<', {IN=>"\0"}, {EXIT=>2},
+ {ERR => "$prog: open failed: : No such file or directory\n"}],
+
+ # two NULs, one fatal error
+ ['nul-2', '--files0-from=-', '<', {IN=>"\0\0"}, {EXIT=>2},
+ {ERR => "$prog: open failed: : No such file or directory\n"}],
+
+ # one file name, no NUL
+ ['1', '--files0-from=-', '<', {IN=>"in1"}, {OUT=>"a\n"}],
+
+ # one file name, with NUL
+ ['1a', '--files0-from=-', '<', {IN=>"in1\0"}, {OUT=>"a\n"}],
+
+ # two file names, no final NUL
+ ['2', '--files0-from=-', '<', {IN=>"in1\0in2"}, {OUT=>"a\nb\n"}],
+
+ # two file names, with final NUL
+ ['2a', '--files0-from=-', '<', {IN=>"in1\0in2\0"}, {OUT=>"a\nb\n"}],
+
+ # Ensure that sort dies even with a good file after a null filename
+ ['zero-len', '--files0-from=-', '<', {IN=>{f=>"\0in1\0"}},
+ {ERR => "$prog: open failed: : No such file or directory\n"},
+ {EXIT=>2} ],
+
+ );
+
+my $save_temps = $ENV{DEBUG};
+my $verbose = $ENV{VERBOSE};
+
+my $fail = run_tests ($program_name, $prog, [EMAIL PROTECTED], $save_temps, $verbose);
+exit $fail;
+EOF
--
1.5.2.5
_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils