summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schwab <schwab@suse.de>2008-02-20 08:36:56 +0100
committerJim Meyering <meyering@redhat.com>2008-02-20 08:41:29 +0100
commitcb3147d29860195780f745faf42e2b8a02bdcbf5 (patch)
tree4fe7240b19d7d861907ae2f5713361c3aea8034a
parentc78039b87de6c636a167d3206081cced54af08e2 (diff)
downloadcoreutils-cb3147d29860195780f745faf42e2b8a02bdcbf5.tar.xz
sort: add --sort=... option.
* src/sort.c (SORT_OPTION): New enum. (sort_args, sort_types): Define. (usage, long_options, main): New option --sort. * tests/sort/Test.pm: Test it. * doc/coreutils.texi (sort invocation): Document --sort option. * NEWS: Mention this.
-rw-r--r--NEWS5
-rw-r--r--doc/coreutils.texi8
-rw-r--r--src/sort.c22
-rw-r--r--tests/sort/Test.pm4
4 files changed, 37 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index 233e514d3..5a5a0a0af 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,11 @@ GNU coreutils NEWS -*- outline -*-
join now verifies that the inputs are in sorted order. This check can
be turned off with the --nocheck-order option.
+ sort accepts the new option --sort=WORD, where WORD can be one of
+ general-numeric, month, numeric or random. These are equivalent to the
+ options --general-numeric-sort/-g, --month-sort/-M, --numeric-sort/-n
+ and --random-sort/-R, resp.
+
** Improvements
ls --color no longer outputs unnecessary escape sequences
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index e8ccb4bd2..040a1de9c 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3536,8 +3536,10 @@ The @env{LC_CTYPE} locale determines character types.
@item -g
@itemx --general-numeric-sort
+@itemx --sort=general-numeric
@opindex -g
@opindex --general-numeric-sort
+@opindex --sort
@cindex general numeric sort
@vindex LC_NUMERIC
Sort numerically, using the standard C function @code{strtod} to convert
@@ -3580,8 +3582,10 @@ This option has no effect if the stronger @option{--dictionary-order}
@item -M
@itemx --month-sort
+@itemx --sort=month
@opindex -M
@opindex --month-sort
+@opindex --sort
@cindex months, sorting by
@vindex LC_TIME
An initial string, consisting of any amount of blanks, followed
@@ -3594,8 +3598,10 @@ can change this.
@item -n
@itemx --numeric-sort
+@itemx --sort=numeric
@opindex -n
@opindex --numeric-sort
+@opindex --sort
@cindex numeric sort
@vindex LC_NUMERIC
Sort numerically. The number begins each line and consists
@@ -3623,8 +3629,10 @@ appear earlier in the output instead of later.
@item -R
@itemx --random-sort
+@itemx --sort=random
@opindex -R
@opindex --random-sort
+@opindex --sort
@cindex random sort
Sort by hashing the input keys and then sorting the hash values.
Choose the hash function at random, ensuring that it is free of
diff --git a/src/sort.c b/src/sort.c
index 1183fc5cc..8b2eec542 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1,5 +1,5 @@
/* sort - sort lines of text (with all kinds of options).
- Copyright (C) 1988, 1991-2007 Free Software Foundation, Inc.
+ Copyright (C) 1988, 1991-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
@@ -329,6 +329,9 @@ Ordering options:\n\
-n, --numeric-sort compare according to string numerical value\n\
-R, --random-sort sort by random hash of keys\n\
--random-source=FILE get random bytes from FILE (default /dev/urandom)\n\
+ --sort=WORD sort according to WORD:\n\
+ general-numeric -g, month -M, numeric -n,\n\
+ random -R\n\
-r, --reverse reverse the result of comparisons\n\
\n\
"), stdout);
@@ -391,7 +394,8 @@ enum
{
CHECK_OPTION = CHAR_MAX + 1,
COMPRESS_PROGRAM_OPTION,
- RANDOM_SOURCE_OPTION
+ RANDOM_SOURCE_OPTION,
+ SORT_OPTION
};
static char const short_options[] = "-bcCdfgik:mMno:rRsS:t:T:uy:z";
@@ -411,6 +415,7 @@ static struct option const long_options[] =
{"numeric-sort", no_argument, NULL, 'n'},
{"random-sort", no_argument, NULL, 'R'},
{"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
+ {"sort", required_argument, NULL, SORT_OPTION},
{"output", required_argument, NULL, 'o'},
{"reverse", no_argument, NULL, 'r'},
{"stable", no_argument, NULL, 's'},
@@ -434,6 +439,16 @@ static char const check_types[] =
};
ARGMATCH_VERIFY (check_args, check_types);
+static char const *const sort_args[] =
+{
+ "general-numeric", "month", "numeric", "random", NULL
+};
+static char const sort_types[] =
+{
+ 'g', 'M', 'n', 'R'
+};
+ARGMATCH_VERIFY (sort_args, sort_types);
+
/* The set of signals that are caught. */
static sigset_t caught_signals;
@@ -2902,6 +2917,9 @@ main (int argc, char **argv)
files[nfiles++] = optarg;
break;
+ case SORT_OPTION:
+ c = XARGMATCH ("--sort", optarg, sort_args, sort_types);
+ /* Fall through. */
case 'b':
case 'd':
case 'f':
diff --git a/tests/sort/Test.pm b/tests/sort/Test.pm
index e4d98be47..0462973c0 100644
--- a/tests/sort/Test.pm
+++ b/tests/sort/Test.pm
@@ -274,6 +274,7 @@ my @tv = (
["incompat4", '-c -o /dev/null', '', '', 2],
["incompat5", '-C -o /dev/null', '', '', 2],
["incompat6", '-cC', '', '', 2],
+["incompat7", '--sort=random -n', '', '', 2],
# -t '\0' is accepted, as of coreutils-5.0.91
['nul-tab', "-k2,2 -t '\\0'", "a\0z\01\nb\0y\02\n", "b\0y\02\na\0z\01\n", 0],
@@ -289,6 +290,9 @@ my @tv = (
# Exercise the code that enlarges the line buffer. See the thread here:
# http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/11006
['realloc-buf', '-S1', 'a'x4000 ."\n", 'a'x4000 ."\n", 0],
+
+["sort-numeric", '--sort=numeric', ".01\n0\n", "0\n.01\n", 0],
+["sort-gennum", '--sort=general-numeric', "1e2\n2e1\n", "2e1\n1e2\n", 0],
);
sub test_vector