2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-21 20:00:35 +00:00
|
|
|
/*
|
2005-01-24 07:00:02 +00:00
|
|
|
* SuS3 compliant sort implementation for busybox
|
1999-12-21 20:00:35 +00:00
|
|
|
*
|
2005-01-24 07:00:02 +00:00
|
|
|
* Copyright (C) 2004 by Rob Landley <rob@landley.net>
|
1999-12-21 20:00:35 +00:00
|
|
|
*
|
2006-02-24 02:30:39 +00:00
|
|
|
* MAINTAINER: Rob Landley <rob@landley.net>
|
2006-09-17 16:28:10 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-12-21 20:00:35 +00:00
|
|
|
*
|
2005-01-24 07:00:02 +00:00
|
|
|
* See SuS3 sort standard at:
|
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
|
1999-12-21 20:00:35 +00:00
|
|
|
*/
|
2016-11-23 13:46:56 +00:00
|
|
|
//config:config SORT
|
2017-07-18 20:01:24 +00:00
|
|
|
//config: bool "sort (7.4 kb)"
|
2016-11-23 13:46:56 +00:00
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 07:50:55 +00:00
|
|
|
//config: sort is used to sort lines of text in specified files.
|
2016-11-23 13:46:56 +00:00
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SORT_BIG
|
2018-04-06 14:44:50 +00:00
|
|
|
//config: bool "Full SuSv3 compliant sort (support -ktcbdfiogM)"
|
2016-11-23 13:46:56 +00:00
|
|
|
//config: default y
|
|
|
|
//config: depends on SORT
|
|
|
|
//config: help
|
2018-04-06 14:44:50 +00:00
|
|
|
//config: Without this, sort only supports -rusz, and an integer version
|
2017-07-21 07:50:55 +00:00
|
|
|
//config: of -n. Selecting this adds sort keys, floating point support, and
|
|
|
|
//config: more. This adds a little over 3k to a nonstatic build on x86.
|
2016-11-23 13:46:56 +00:00
|
|
|
//config:
|
2017-07-21 07:50:55 +00:00
|
|
|
//config: The SuSv3 sort standard is available at:
|
|
|
|
//config: http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
|
2018-04-04 15:02:32 +00:00
|
|
|
//config:
|
|
|
|
//config:config FEATURE_SORT_OPTIMIZE_MEMORY
|
|
|
|
//config: bool "Use less memory (but might be slower)"
|
|
|
|
//config: default n # defaults to N since we are size-paranoid tribe
|
|
|
|
//config: depends on SORT
|
|
|
|
//config: help
|
|
|
|
//config: Attempt to use less memory (by storing only one copy
|
|
|
|
//config: of duplicated lines, and such). Useful if you work on huge files.
|
2016-11-23 13:46:56 +00:00
|
|
|
|
|
|
|
//applet:IF_SORT(APPLET_NOEXEC(sort, sort, BB_DIR_USR_BIN, BB_SUID_DROP, sort))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_SORT) += sort.o
|
1999-12-21 20:00:35 +00:00
|
|
|
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage:#define sort_trivial_usage
|
|
|
|
//usage: "[-nru"
|
2016-04-02 15:39:50 +00:00
|
|
|
//usage: IF_FEATURE_SORT_BIG("gMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "] [FILE]..."
|
|
|
|
//usage:#define sort_full_usage "\n\n"
|
|
|
|
//usage: "Sort lines of text\n"
|
|
|
|
//usage: IF_FEATURE_SORT_BIG(
|
2016-04-02 16:20:26 +00:00
|
|
|
//usage: "\n -o FILE Output to FILE"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "\n -c Check whether input is sorted"
|
2016-04-02 16:20:26 +00:00
|
|
|
//usage: "\n -b Ignore leading blanks"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "\n -f Ignore case"
|
|
|
|
//usage: "\n -i Ignore unprintable characters"
|
2016-04-02 16:20:26 +00:00
|
|
|
//usage: "\n -d Dictionary order (blank or alphanumeric only)"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: )
|
2015-09-19 20:06:40 +00:00
|
|
|
//-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G)
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: "\n -n Sort numbers"
|
|
|
|
//usage: IF_FEATURE_SORT_BIG(
|
2018-04-04 15:02:32 +00:00
|
|
|
//usage: "\n -g General numerical sort"
|
|
|
|
//usage: "\n -M Sort month"
|
2018-07-07 23:37:54 +00:00
|
|
|
//usage: "\n -V Sort version"
|
2015-09-19 20:06:40 +00:00
|
|
|
//usage: "\n -t CHAR Field separator"
|
|
|
|
//usage: "\n -k N[,M] Sort by Nth field"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage: )
|
|
|
|
//usage: "\n -r Reverse sort order"
|
|
|
|
//usage: "\n -s Stable (don't sort ties alphabetically)"
|
|
|
|
//usage: "\n -u Suppress duplicate lines"
|
|
|
|
//usage: "\n -z Lines are terminated by NUL, not newline"
|
2018-02-21 19:08:54 +00:00
|
|
|
///////: "\n -m Ignored for GNU compatibility"
|
|
|
|
///////: "\n -S BUFSZ Ignored for GNU compatibility"
|
|
|
|
///////: "\n -T TMPDIR Ignored for GNU compatibility"
|
2011-03-31 12:43:25 +00:00
|
|
|
//usage:
|
|
|
|
//usage:#define sort_example_usage
|
|
|
|
//usage: "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n"
|
|
|
|
//usage: "a\n"
|
|
|
|
//usage: "b\n"
|
|
|
|
//usage: "c\n"
|
|
|
|
//usage: "d\n"
|
|
|
|
//usage: "e\n"
|
|
|
|
//usage: "f\n"
|
|
|
|
//usage: IF_FEATURE_SORT_BIG(
|
|
|
|
//usage: "$ echo -e \"c 3\\nb 2\\nd 2\" | $SORT -k 2,2n -k 1,1r\n"
|
|
|
|
//usage: "d 2\n"
|
|
|
|
//usage: "b 2\n"
|
|
|
|
//usage: "c 3\n"
|
|
|
|
//usage: )
|
|
|
|
//usage: ""
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
1999-12-21 20:00:35 +00:00
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* These are sort types */
|
2007-01-01 18:18:04 +00:00
|
|
|
enum {
|
2018-07-07 23:37:54 +00:00
|
|
|
FLAG_n = 1 << 0, /* Numeric sort */
|
|
|
|
FLAG_g = 1 << 1, /* Sort using strtod() */
|
|
|
|
FLAG_M = 1 << 2, /* Sort date */
|
|
|
|
FLAG_V = 1 << 3, /* Sort version */
|
2005-01-24 07:00:02 +00:00
|
|
|
/* ucsz apply to root level only, not keys. b at root level implies bb */
|
2018-07-07 23:37:54 +00:00
|
|
|
FLAG_u = 1 << 4, /* Unique */
|
|
|
|
FLAG_c = 1 << 5, /* Check: no output, exit(!ordered) */
|
|
|
|
FLAG_s = 1 << 6, /* Stable sort, no ascii fallback at end */
|
|
|
|
FLAG_z = 1 << 7, /* Input and output is NUL terminated, not \n */
|
2005-01-24 07:00:02 +00:00
|
|
|
/* These can be applied to search keys, the previous four can't */
|
2018-07-07 23:37:54 +00:00
|
|
|
FLAG_b = 1 << 8, /* Ignore leading blanks */
|
|
|
|
FLAG_r = 1 << 9, /* Reverse */
|
|
|
|
FLAG_d = 1 << 10, /* Ignore !(isalnum()|isspace()) */
|
|
|
|
FLAG_f = 1 << 11, /* Force uppercase */
|
|
|
|
FLAG_i = 1 << 12, /* Ignore !isprint() */
|
|
|
|
FLAG_m = 1 << 13, /* ignored: merge already sorted files; do not sort */
|
|
|
|
FLAG_S = 1 << 14, /* ignored: -S, --buffer-size=SIZE */
|
|
|
|
FLAG_T = 1 << 15, /* ignored: -T, --temporary-directory=DIR */
|
|
|
|
FLAG_o = 1 << 16,
|
|
|
|
FLAG_k = 1 << 17,
|
|
|
|
FLAG_t = 1 << 18,
|
2007-01-01 18:18:04 +00:00
|
|
|
FLAG_bb = 0x80000000, /* Ignore trailing blanks */
|
2018-02-21 19:08:54 +00:00
|
|
|
FLAG_no_tie_break = 0x40000000,
|
2007-01-01 18:18:04 +00:00
|
|
|
};
|
2005-01-24 07:00:02 +00:00
|
|
|
|
2018-02-22 10:03:23 +00:00
|
|
|
static const char sort_opt_str[] ALIGN1 = "^"
|
2018-07-07 23:37:54 +00:00
|
|
|
"ngMVucszbrdfimS:T:o:k:*t:"
|
2018-02-22 10:03:23 +00:00
|
|
|
"\0" "o--o:t--t"/*-t, -o: at most one of each*/;
|
|
|
|
/*
|
|
|
|
* OPT_STR must not be string literal, needs to have stable address:
|
|
|
|
* code uses "strchr(OPT_STR,c) - OPT_STR" idiom.
|
|
|
|
*/
|
|
|
|
#define OPT_STR (sort_opt_str + 1)
|
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2005-04-16 19:39:00 +00:00
|
|
|
static char key_separator;
|
2005-01-24 07:00:02 +00:00
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
static struct sort_key {
|
2010-10-29 09:46:52 +00:00
|
|
|
struct sort_key *next_key; /* linked list */
|
|
|
|
unsigned range[4]; /* start word, start char, end word, end char */
|
2007-01-01 18:18:04 +00:00
|
|
|
unsigned flags;
|
2005-01-24 07:00:02 +00:00
|
|
|
} *key_list;
|
1999-12-22 00:30:29 +00:00
|
|
|
|
2018-02-22 10:03:23 +00:00
|
|
|
|
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
static char *get_key(char *str, struct sort_key *key, int flags)
|
1999-12-22 00:30:29 +00:00
|
|
|
{
|
2015-10-19 12:44:51 +00:00
|
|
|
int start = start; /* for compiler */
|
|
|
|
int end;
|
|
|
|
int len, j;
|
2008-05-13 02:27:31 +00:00
|
|
|
unsigned i;
|
2005-01-24 07:00:02 +00:00
|
|
|
|
|
|
|
/* Special case whole string, so we don't have to make a copy */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3]
|
|
|
|
&& !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb))
|
|
|
|
) {
|
|
|
|
return str;
|
|
|
|
}
|
2005-01-24 07:00:02 +00:00
|
|
|
|
2007-01-01 18:18:04 +00:00
|
|
|
/* Find start of key on first pass, end on second pass */
|
2006-11-26 15:48:03 +00:00
|
|
|
len = strlen(str);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
if (!key->range[2*j])
|
|
|
|
end = len;
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Loop through fields */
|
|
|
|
else {
|
2015-10-19 12:44:51 +00:00
|
|
|
unsigned char ch = 0;
|
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
end = 0;
|
|
|
|
for (i = 1; i < key->range[2*j] + j; i++) {
|
2007-01-01 18:18:04 +00:00
|
|
|
if (key_separator) {
|
2007-01-07 00:21:41 +00:00
|
|
|
/* Skip body of key and separator */
|
2015-10-19 12:44:51 +00:00
|
|
|
while ((ch = str[end]) != '\0') {
|
|
|
|
end++;
|
|
|
|
if (ch == key_separator)
|
2006-11-26 15:48:03 +00:00
|
|
|
break;
|
2007-01-01 18:18:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Skip leading blanks */
|
|
|
|
while (isspace(str[end]))
|
|
|
|
end++;
|
|
|
|
/* Skip body of key */
|
2015-10-19 12:44:51 +00:00
|
|
|
while (str[end] != '\0') {
|
2006-11-26 15:48:03 +00:00
|
|
|
if (isspace(str[end]))
|
|
|
|
break;
|
2007-01-01 18:18:04 +00:00
|
|
|
end++;
|
2006-11-26 15:48:03 +00:00
|
|
|
}
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-19 20:06:40 +00:00
|
|
|
/* Remove last delim: "abc:def:" => "abc:def" */
|
2015-10-19 12:44:51 +00:00
|
|
|
if (j && ch) {
|
|
|
|
//if (str[end-1] != key_separator)
|
|
|
|
// bb_error_msg(_and_die("BUG! "
|
|
|
|
// "str[start:%d,end:%d]:'%.*s'",
|
|
|
|
// start, end, (int)(end-start), &str[start]);
|
2015-09-19 20:06:40 +00:00
|
|
|
end--;
|
2015-10-19 12:44:51 +00:00
|
|
|
}
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2006-11-26 15:48:03 +00:00
|
|
|
if (!j) start = end;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
/* Strip leading whitespace if necessary */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_b)
|
2015-10-19 17:27:26 +00:00
|
|
|
/* not using skip_whitespace() for speed */
|
2006-11-26 15:48:03 +00:00
|
|
|
while (isspace(str[start])) start++;
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Strip trailing whitespace if necessary */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_bb)
|
|
|
|
while (end > start && isspace(str[end-1])) end--;
|
2015-10-19 17:27:26 +00:00
|
|
|
/* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (key->range[3]) {
|
2015-10-19 17:27:26 +00:00
|
|
|
end = key->range[3];
|
2006-11-26 15:48:03 +00:00
|
|
|
if (end > len) end = len;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2015-10-19 17:27:26 +00:00
|
|
|
/* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (key->range[1]) {
|
|
|
|
start += key->range[1] - 1;
|
|
|
|
if (start > len) start = len;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
/* Make the copy */
|
2015-09-19 20:06:40 +00:00
|
|
|
if (end < start)
|
|
|
|
end = start;
|
2006-11-26 15:48:03 +00:00
|
|
|
str = xstrndup(str+start, end-start);
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Handle -d */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_d) {
|
|
|
|
for (start = end = 0; str[end]; end++)
|
|
|
|
if (isspace(str[end]) || isalnum(str[end]))
|
|
|
|
str[start++] = str[end];
|
|
|
|
str[start] = '\0';
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
/* Handle -i */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_i) {
|
|
|
|
for (start = end = 0; str[end]; end++)
|
2009-11-18 10:34:43 +00:00
|
|
|
if (isprint_asciionly(str[end]))
|
2006-11-26 15:48:03 +00:00
|
|
|
str[start++] = str[end];
|
|
|
|
str[start] = '\0';
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
/* Handle -f */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_f)
|
|
|
|
for (i = 0; str[i]; i++)
|
|
|
|
str[i] = toupper(str[i]);
|
2005-01-24 07:00:02 +00:00
|
|
|
|
|
|
|
return str;
|
1999-12-22 00:30:29 +00:00
|
|
|
}
|
1999-12-21 20:00:35 +00:00
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
static struct sort_key *add_key(void)
|
1999-12-21 20:00:35 +00:00
|
|
|
{
|
2006-11-26 15:48:03 +00:00
|
|
|
struct sort_key **pkey = &key_list;
|
|
|
|
while (*pkey)
|
|
|
|
pkey = &((*pkey)->next_key);
|
2006-08-03 20:07:35 +00:00
|
|
|
return *pkey = xzalloc(sizeof(struct sort_key));
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
#define GET_LINE(fp) \
|
2007-01-01 18:18:04 +00:00
|
|
|
((option_mask32 & FLAG_z) \
|
2006-11-26 15:48:03 +00:00
|
|
|
? bb_get_chunk_from_file(fp, NULL) \
|
2008-03-26 20:04:27 +00:00
|
|
|
: xmalloc_fgetline(fp))
|
2005-01-24 07:00:02 +00:00
|
|
|
#else
|
2008-03-26 20:04:27 +00:00
|
|
|
#define GET_LINE(fp) xmalloc_fgetline(fp)
|
2005-01-24 07:00:02 +00:00
|
|
|
#endif
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Iterate through keys list and perform comparisons */
|
|
|
|
static int compare_keys(const void *xarg, const void *yarg)
|
|
|
|
{
|
2007-01-01 18:18:04 +00:00
|
|
|
int flags = option_mask32, retval = 0;
|
2006-11-26 15:48:03 +00:00
|
|
|
char *x, *y;
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2005-01-24 07:00:02 +00:00
|
|
|
struct sort_key *key;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
for (key = key_list; !retval && key; key = key->next_key) {
|
2007-01-01 18:18:04 +00:00
|
|
|
flags = key->flags ? key->flags : option_mask32;
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Chop out and modify key chunks, handling -dfib */
|
2006-11-26 15:48:03 +00:00
|
|
|
x = get_key(*(char **)xarg, key, flags);
|
|
|
|
y = get_key(*(char **)yarg, key, flags);
|
2005-01-24 07:00:02 +00:00
|
|
|
#else
|
|
|
|
/* This curly bracket serves no purpose but to match the nesting
|
2013-01-15 12:58:01 +00:00
|
|
|
* level of the for () loop we're not using */
|
2005-01-24 07:00:02 +00:00
|
|
|
{
|
2006-11-26 15:48:03 +00:00
|
|
|
x = *(char **)xarg;
|
|
|
|
y = *(char **)yarg;
|
2005-01-24 07:00:02 +00:00
|
|
|
#endif
|
|
|
|
/* Perform actual comparison */
|
2018-07-07 23:37:54 +00:00
|
|
|
switch (flags & (FLAG_n | FLAG_g | FLAG_M | FLAG_V)) {
|
2006-11-26 15:48:03 +00:00
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unknown sort type");
|
|
|
|
break;
|
2018-07-07 23:37:54 +00:00
|
|
|
#if defined(HAVE_STRVERSCMP) && HAVE_STRVERSCMP == 1
|
|
|
|
case FLAG_V:
|
|
|
|
retval = strverscmp(x, y);
|
|
|
|
break;
|
|
|
|
#endif
|
2006-11-26 15:48:03 +00:00
|
|
|
/* Ascii sort */
|
|
|
|
case 0:
|
2007-01-01 18:18:04 +00:00
|
|
|
#if ENABLE_LOCALE_SUPPORT
|
|
|
|
retval = strcoll(x, y);
|
|
|
|
#else
|
2006-11-26 15:48:03 +00:00
|
|
|
retval = strcmp(x, y);
|
2007-01-01 18:18:04 +00:00
|
|
|
#endif
|
2006-11-26 15:48:03 +00:00
|
|
|
break;
|
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
|
|
|
case FLAG_g: {
|
|
|
|
char *xx, *yy;
|
2006-11-26 15:48:54 +00:00
|
|
|
double dx = strtod(x, &xx);
|
|
|
|
double dy = strtod(y, &yy);
|
2006-11-26 15:48:03 +00:00
|
|
|
/* not numbers < NaN < -infinity < numbers < +infinity) */
|
|
|
|
if (x == xx)
|
|
|
|
retval = (y == yy ? 0 : -1);
|
|
|
|
else if (y == yy)
|
|
|
|
retval = 1;
|
|
|
|
/* Check for isnan */
|
|
|
|
else if (dx != dx)
|
|
|
|
retval = (dy != dy) ? 0 : -1;
|
|
|
|
else if (dy != dy)
|
|
|
|
retval = 1;
|
|
|
|
/* Check for infinity. Could underflow, but it avoids libm. */
|
|
|
|
else if (1.0 / dx == 0.0) {
|
|
|
|
if (dx < 0)
|
|
|
|
retval = (1.0 / dy == 0.0 && dy < 0) ? 0 : -1;
|
|
|
|
else
|
|
|
|
retval = (1.0 / dy == 0.0 && dy > 0) ? 0 : 1;
|
|
|
|
} else if (1.0 / dy == 0.0)
|
|
|
|
retval = (dy < 0) ? 1 : -1;
|
|
|
|
else
|
|
|
|
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FLAG_M: {
|
|
|
|
struct tm thyme;
|
|
|
|
int dx;
|
|
|
|
char *xx, *yy;
|
1999-12-21 20:00:35 +00:00
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
xx = strptime(x, "%b", &thyme);
|
|
|
|
dx = thyme.tm_mon;
|
|
|
|
yy = strptime(y, "%b", &thyme);
|
|
|
|
if (!xx)
|
|
|
|
retval = (!yy) ? 0 : -1;
|
|
|
|
else if (!yy)
|
|
|
|
retval = 1;
|
|
|
|
else
|
2015-10-19 17:13:06 +00:00
|
|
|
retval = dx - thyme.tm_mon;
|
2006-11-26 15:48:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Full floating point version of -n */
|
|
|
|
case FLAG_n: {
|
2006-11-26 15:48:54 +00:00
|
|
|
double dx = atof(x);
|
|
|
|
double dy = atof(y);
|
2006-11-26 15:48:03 +00:00
|
|
|
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
|
|
|
break;
|
|
|
|
}
|
2006-11-26 15:48:54 +00:00
|
|
|
} /* switch */
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Free key copies. */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (x != *(char **)xarg) free(x);
|
|
|
|
if (y != *(char **)yarg) free(y);
|
2006-12-26 10:42:51 +00:00
|
|
|
/* if (retval) break; - done by for () anyway */
|
2005-01-24 07:00:02 +00:00
|
|
|
#else
|
2006-11-26 15:48:03 +00:00
|
|
|
/* Integer version of -n for tiny systems */
|
|
|
|
case FLAG_n:
|
|
|
|
retval = atoi(x) - atoi(y);
|
|
|
|
break;
|
2006-11-26 15:48:54 +00:00
|
|
|
} /* switch */
|
2005-01-24 07:00:02 +00:00
|
|
|
#endif
|
2007-01-01 18:18:04 +00:00
|
|
|
} /* for */
|
|
|
|
|
2018-02-21 19:08:54 +00:00
|
|
|
if (retval == 0) {
|
|
|
|
/* So far lines are "the same" */
|
|
|
|
|
|
|
|
if (option_mask32 & FLAG_s) {
|
2018-02-22 09:54:55 +00:00
|
|
|
/* "Stable sort": later line is "greater than",
|
2018-02-21 19:08:54 +00:00
|
|
|
* IOW: do not allow qsort() to swap equal lines.
|
|
|
|
*/
|
|
|
|
uint32_t *p32;
|
|
|
|
uint32_t x32, y32;
|
|
|
|
char *line;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
line = *(char**)xarg;
|
|
|
|
len = (strlen(line) + 4) & (~3u);
|
|
|
|
p32 = (void*)(line + len);
|
|
|
|
x32 = *p32;
|
|
|
|
line = *(char**)yarg;
|
|
|
|
len = (strlen(line) + 4) & (~3u);
|
|
|
|
p32 = (void*)(line + len);
|
|
|
|
y32 = *p32;
|
|
|
|
|
2018-02-22 09:54:55 +00:00
|
|
|
/* If x > y, 1, else -1 */
|
|
|
|
retval = (x32 > y32) * 2 - 1;
|
2018-02-21 19:08:54 +00:00
|
|
|
} else
|
|
|
|
if (!(option_mask32 & FLAG_no_tie_break)) {
|
|
|
|
/* fallback sort */
|
|
|
|
flags = option_mask32;
|
|
|
|
retval = strcmp(*(char **)xarg, *(char **)yarg);
|
|
|
|
}
|
2014-01-19 08:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FLAG_r)
|
|
|
|
return -retval;
|
2006-11-26 15:48:03 +00:00
|
|
|
|
|
|
|
return retval;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2000-09-28 17:49:59 +00:00
|
|
|
|
2007-01-02 05:43:30 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2007-01-01 18:18:04 +00:00
|
|
|
static unsigned str2u(char **str)
|
|
|
|
{
|
|
|
|
unsigned long lu;
|
|
|
|
if (!isdigit((*str)[0]))
|
|
|
|
bb_error_msg_and_die("bad field specification");
|
|
|
|
lu = strtoul(*str, str, 10);
|
|
|
|
if ((sizeof(long) > sizeof(int) && lu > INT_MAX) || !lu)
|
|
|
|
bb_error_msg_and_die("bad field specification");
|
|
|
|
return lu;
|
|
|
|
}
|
2007-01-02 05:43:30 +00:00
|
|
|
#endif
|
2007-01-01 18:18:04 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int sort_main(int argc UNUSED_PARAM, char **argv)
|
2005-01-24 07:00:02 +00:00
|
|
|
{
|
2018-02-21 19:08:54 +00:00
|
|
|
char **lines;
|
2007-02-17 18:11:45 +00:00
|
|
|
char *str_ignored, *str_o, *str_t;
|
|
|
|
llist_t *lst_k = NULL;
|
2015-10-19 17:13:06 +00:00
|
|
|
int i;
|
2009-12-13 01:09:22 +00:00
|
|
|
int linecount;
|
|
|
|
unsigned opts;
|
2018-04-04 15:02:32 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
|
|
|
|
bool can_drop_dups;
|
|
|
|
size_t prev_len = 0;
|
2018-04-06 14:44:50 +00:00
|
|
|
char *prev_line = (char*) "";
|
2018-04-04 15:02:32 +00:00
|
|
|
/* Postpone optimizing if the input is small, < 16k lines:
|
|
|
|
* even just free()ing duplicate lines takes time.
|
|
|
|
*/
|
|
|
|
size_t count_to_optimize_dups = 0x3fff;
|
|
|
|
#endif
|
2005-01-24 07:00:02 +00:00
|
|
|
|
2006-10-03 20:28:06 +00:00
|
|
|
xfunc_error_retval = 2;
|
2007-01-01 18:18:04 +00:00
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Parse command line options */
|
2018-02-22 10:03:23 +00:00
|
|
|
opts = getopt32(argv,
|
|
|
|
sort_opt_str,
|
2017-08-08 19:55:02 +00:00
|
|
|
&str_ignored, &str_ignored, &str_o, &lst_k, &str_t
|
|
|
|
);
|
2018-04-04 15:02:32 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
|
|
|
|
/* Can drop dups only if -u but no "complicating" options,
|
|
|
|
* IOW: if we do a full line compares. Safe options:
|
|
|
|
* -o FILE Output to FILE
|
|
|
|
* -z Lines are terminated by NUL, not newline
|
|
|
|
* -r Reverse sort order
|
|
|
|
* -s Stable (don't sort ties alphabetically)
|
|
|
|
* Not sure about these:
|
|
|
|
* -b Ignore leading blanks
|
|
|
|
* -f Ignore case
|
|
|
|
* -i Ignore unprintable characters
|
|
|
|
* -d Dictionary order (blank or alphanumeric only)
|
|
|
|
* -n Sort numbers
|
|
|
|
* -g General numerical sort
|
|
|
|
* -M Sort month
|
|
|
|
*/
|
|
|
|
can_drop_dups = ((opts & ~(FLAG_o|FLAG_z|FLAG_r|FLAG_s)) == FLAG_u);
|
|
|
|
/* Stable sort needs every line to be uniquely allocated,
|
|
|
|
* disable optimization to reuse strings:
|
|
|
|
*/
|
|
|
|
if (opts & FLAG_s)
|
|
|
|
count_to_optimize_dups = (size_t)-1L;
|
|
|
|
#endif
|
2009-12-13 01:09:22 +00:00
|
|
|
/* global b strips leading and trailing spaces */
|
|
|
|
if (opts & FLAG_b)
|
|
|
|
option_mask32 |= FLAG_bb;
|
2006-11-26 15:48:03 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2009-12-13 01:09:22 +00:00
|
|
|
if (opts & FLAG_t) {
|
2007-01-01 18:18:04 +00:00
|
|
|
if (!str_t[0] || str_t[1])
|
|
|
|
bb_error_msg_and_die("bad -t parameter");
|
|
|
|
key_separator = str_t[0];
|
|
|
|
}
|
2009-12-13 01:09:22 +00:00
|
|
|
/* note: below this point we use option_mask32, not opts,
|
|
|
|
* since that reduces register pressure and makes code smaller */
|
|
|
|
|
2015-10-19 17:13:06 +00:00
|
|
|
/* Parse sort key */
|
2007-02-17 18:11:45 +00:00
|
|
|
while (lst_k) {
|
2007-01-01 18:18:04 +00:00
|
|
|
enum {
|
|
|
|
FLAG_allowed_for_k =
|
|
|
|
FLAG_n | /* Numeric sort */
|
|
|
|
FLAG_g | /* Sort using strtod() */
|
|
|
|
FLAG_M | /* Sort date */
|
|
|
|
FLAG_b | /* Ignore leading blanks */
|
|
|
|
FLAG_r | /* Reverse */
|
|
|
|
FLAG_d | /* Ignore !(isalnum()|isspace()) */
|
|
|
|
FLAG_f | /* Force uppercase */
|
|
|
|
FLAG_i | /* Ignore !isprint() */
|
|
|
|
0
|
|
|
|
};
|
|
|
|
struct sort_key *key = add_key();
|
2008-06-15 05:40:56 +00:00
|
|
|
char *str_k = llist_pop(&lst_k);
|
2005-01-24 07:00:02 +00:00
|
|
|
|
2007-01-01 18:18:04 +00:00
|
|
|
i = 0; /* i==0 before comma, 1 after (-k3,6) */
|
|
|
|
while (*str_k) {
|
|
|
|
/* Start of range */
|
|
|
|
/* Cannot use bb_strtou - suffix can be a letter */
|
|
|
|
key->range[2*i] = str2u(&str_k);
|
|
|
|
if (*str_k == '.') {
|
|
|
|
str_k++;
|
|
|
|
key->range[2*i+1] = str2u(&str_k);
|
|
|
|
}
|
|
|
|
while (*str_k) {
|
2015-10-19 17:13:06 +00:00
|
|
|
int flag;
|
|
|
|
const char *idx;
|
2009-12-13 01:09:22 +00:00
|
|
|
|
2007-01-01 18:18:04 +00:00
|
|
|
if (*str_k == ',' && !i++) {
|
|
|
|
str_k++;
|
|
|
|
break;
|
|
|
|
} /* no else needed: fall through to syntax error
|
2009-12-13 01:09:22 +00:00
|
|
|
because comma isn't in OPT_STR */
|
2015-10-19 17:13:06 +00:00
|
|
|
idx = strchr(OPT_STR, *str_k);
|
|
|
|
if (!idx)
|
2007-01-01 18:18:04 +00:00
|
|
|
bb_error_msg_and_die("unknown key option");
|
2015-10-19 17:13:06 +00:00
|
|
|
flag = 1 << (idx - OPT_STR);
|
2007-01-01 18:18:04 +00:00
|
|
|
if (flag & ~FLAG_allowed_for_k)
|
|
|
|
bb_error_msg_and_die("unknown sort type");
|
|
|
|
/* b after ',' means strip _trailing_ space */
|
2009-12-13 01:09:22 +00:00
|
|
|
if (i && flag == FLAG_b)
|
|
|
|
flag = FLAG_bb;
|
2007-01-01 18:18:04 +00:00
|
|
|
key->flags |= flag;
|
|
|
|
str_k++;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2007-01-01 18:18:04 +00:00
|
|
|
#endif
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Open input files and read data */
|
2008-03-17 09:07:36 +00:00
|
|
|
argv += optind;
|
|
|
|
if (!*argv)
|
|
|
|
*--argv = (char*)"-";
|
2009-12-13 01:09:22 +00:00
|
|
|
linecount = 0;
|
|
|
|
lines = NULL;
|
2008-03-17 09:07:36 +00:00
|
|
|
do {
|
|
|
|
/* coreutils 6.9 compat: abort on first open error,
|
|
|
|
* do not continue to next file: */
|
2009-12-13 01:09:22 +00:00
|
|
|
FILE *fp = xfopen_stdin(*argv);
|
2006-11-26 15:48:03 +00:00
|
|
|
for (;;) {
|
2018-02-21 19:08:54 +00:00
|
|
|
char *line = GET_LINE(fp);
|
2009-12-13 01:09:22 +00:00
|
|
|
if (!line)
|
|
|
|
break;
|
2018-04-04 15:02:32 +00:00
|
|
|
|
|
|
|
#if ENABLE_FEATURE_SORT_OPTIMIZE_MEMORY
|
|
|
|
if (count_to_optimize_dups != 0)
|
|
|
|
count_to_optimize_dups--;
|
|
|
|
if (count_to_optimize_dups == 0) {
|
|
|
|
size_t len;
|
|
|
|
char *new_line;
|
|
|
|
|
|
|
|
/* On kernel/linux/arch/ *.[ch] files,
|
|
|
|
* this reduces memory usage by 6%.
|
|
|
|
* yes | head -99999999 | sort
|
|
|
|
* goes down from 1900Mb to 380 Mb.
|
|
|
|
*/
|
|
|
|
len = strlen(line);
|
|
|
|
if (len <= prev_len) {
|
2018-04-06 14:44:50 +00:00
|
|
|
new_line = prev_line + (prev_len - len);
|
2018-04-04 15:02:32 +00:00
|
|
|
if (strcmp(line, new_line) == 0) {
|
2018-04-06 14:44:50 +00:00
|
|
|
/* it's a tail of the prev line */
|
2018-04-04 15:02:32 +00:00
|
|
|
if (can_drop_dups && prev_len == len) {
|
2018-04-06 14:44:50 +00:00
|
|
|
/* it's identical to prev line */
|
2018-04-04 15:02:32 +00:00
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
line = new_line;
|
2018-04-06 14:44:50 +00:00
|
|
|
/* continue using longer prev_line
|
|
|
|
* for future tail tests.
|
|
|
|
*/
|
|
|
|
goto skip;
|
2018-04-04 15:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_len = len;
|
2018-04-06 14:44:50 +00:00
|
|
|
prev_line = line;
|
|
|
|
skip: ;
|
2018-04-04 15:02:32 +00:00
|
|
|
}
|
2018-04-04 15:07:21 +00:00
|
|
|
#else
|
|
|
|
//TODO: lighter version which only drops total dups if can_drop_dups == true
|
2018-04-04 15:02:32 +00:00
|
|
|
#endif
|
2008-07-08 05:14:36 +00:00
|
|
|
lines = xrealloc_vector(lines, 6, linecount);
|
2006-11-26 15:48:03 +00:00
|
|
|
lines[linecount++] = line;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2008-03-17 09:07:36 +00:00
|
|
|
fclose_if_not_stdin(fp);
|
|
|
|
} while (*++argv);
|
|
|
|
|
2006-11-26 15:48:03 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2015-10-19 17:13:06 +00:00
|
|
|
/* If no key, perform alphabetic sort */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (!key_list)
|
|
|
|
add_key()->range[0] = 1;
|
2015-10-19 17:13:06 +00:00
|
|
|
/* Handle -c */
|
2007-01-01 18:18:04 +00:00
|
|
|
if (option_mask32 & FLAG_c) {
|
|
|
|
int j = (option_mask32 & FLAG_u) ? -1 : 0;
|
2009-12-13 01:09:22 +00:00
|
|
|
for (i = 1; i < linecount; i++) {
|
2006-11-26 15:48:03 +00:00
|
|
|
if (compare_keys(&lines[i-1], &lines[i]) > j) {
|
2009-12-13 01:09:22 +00:00
|
|
|
fprintf(stderr, "Check line %u\n", i);
|
2007-11-16 12:39:16 +00:00
|
|
|
return EXIT_FAILURE;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2009-12-13 01:09:22 +00:00
|
|
|
}
|
2007-11-16 12:39:16 +00:00
|
|
|
return EXIT_SUCCESS;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-21 19:08:54 +00:00
|
|
|
|
|
|
|
/* For stable sort, store original line position beyond terminating NUL */
|
|
|
|
if (option_mask32 & FLAG_s) {
|
|
|
|
for (i = 0; i < linecount; i++) {
|
|
|
|
uint32_t *p32;
|
|
|
|
char *line;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
line = lines[i];
|
|
|
|
len = (strlen(line) + 4) & (~3u);
|
|
|
|
lines[i] = line = xrealloc(line, len + 4);
|
|
|
|
p32 = (void*)(line + len);
|
|
|
|
*p32 = i;
|
|
|
|
}
|
|
|
|
/*option_mask32 |= FLAG_no_tie_break;*/
|
|
|
|
/* ^^^redundant: if FLAG_s, compare_keys() does no tie break */
|
|
|
|
}
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Perform the actual sort */
|
2009-12-13 01:09:22 +00:00
|
|
|
qsort(lines, linecount, sizeof(lines[0]), compare_keys);
|
2015-10-19 17:13:06 +00:00
|
|
|
|
|
|
|
/* Handle -u */
|
2007-01-01 18:18:04 +00:00
|
|
|
if (option_mask32 & FLAG_u) {
|
2015-10-19 17:13:06 +00:00
|
|
|
int j = 0;
|
2018-02-21 19:08:54 +00:00
|
|
|
/* coreutils 6.3 drop lines for which only key is the same
|
|
|
|
* -- disabling last-resort compare, or else compare_keys()
|
|
|
|
* will be the same only for completely identical lines.
|
|
|
|
*/
|
|
|
|
option_mask32 |= FLAG_no_tie_break;
|
2007-01-01 18:18:04 +00:00
|
|
|
for (i = 1; i < linecount; i++) {
|
2015-10-19 17:13:06 +00:00
|
|
|
if (compare_keys(&lines[j], &lines[i]) == 0)
|
2006-11-26 15:48:03 +00:00
|
|
|
free(lines[i]);
|
|
|
|
else
|
2015-10-19 17:13:06 +00:00
|
|
|
lines[++j] = lines[i];
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2009-12-13 01:09:22 +00:00
|
|
|
if (linecount)
|
2015-10-19 17:13:06 +00:00
|
|
|
linecount = j+1;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2009-12-13 01:09:22 +00:00
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Print it */
|
2009-12-13 01:09:22 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
|
|
|
/* Open output file _after_ we read all input ones */
|
|
|
|
if (option_mask32 & FLAG_o)
|
2010-10-18 00:43:57 +00:00
|
|
|
xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
|
2009-12-13 01:09:22 +00:00
|
|
|
#endif
|
2015-10-19 17:13:06 +00:00
|
|
|
{
|
|
|
|
int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
|
|
|
|
for (i = 0; i < linecount; i++)
|
|
|
|
printf("%s%c", lines[i], ch);
|
|
|
|
}
|
2006-11-26 15:48:03 +00:00
|
|
|
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2000-12-20 20:49:56 +00:00
|
|
|
}
|