busybox/coreutils/sort.c

567 lines
15 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
1999-12-22 01:30:35 +05:30
/*
* SuS3 compliant sort implementation for busybox
1999-12-22 01:30:35 +05:30
*
* Copyright (C) 2004 by Rob Landley <rob@landley.net>
1999-12-22 01:30:35 +05:30
*
* MAINTAINER: Rob Landley <rob@landley.net>
2006-09-17 21:58:10 +05:30
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
1999-12-22 01:30:35 +05:30
*
* See SuS3 sort standard at:
* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
1999-12-22 01:30:35 +05:30
*/
//config:config SORT
//config: bool "sort (7.4 kb)"
//config: default y
//config: help
//config: sort is used to sort lines of text in specified files.
//config:
//config:config FEATURE_SORT_BIG
//config: bool "Full SuSv3 compliant sort (support -ktcsbdfiozgM)"
//config: default y
//config: depends on SORT
//config: help
//config: Without this, sort only supports -r, -u, and an integer version
//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.
//config:
//config: The SuSv3 sort standard is available at:
//config: http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
//applet:IF_SORT(APPLET_NOEXEC(sort, sort, BB_DIR_USR_BIN, BB_SUID_DROP, sort))
//kbuild:lib-$(CONFIG_SORT) += sort.o
1999-12-22 01:30:35 +05:30
//usage:#define sort_trivial_usage
//usage: "[-nru"
//usage: IF_FEATURE_SORT_BIG("gMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
//usage: "] [FILE]..."
//usage:#define sort_full_usage "\n\n"
//usage: "Sort lines of text\n"
//usage: IF_FEATURE_SORT_BIG(
//usage: "\n -o FILE Output to FILE"
//usage: "\n -c Check whether input is sorted"
//usage: "\n -b Ignore leading blanks"
//usage: "\n -f Ignore case"
//usage: "\n -i Ignore unprintable characters"
//usage: "\n -d Dictionary order (blank or alphanumeric only)"
//usage: "\n -g General numerical sort"
//usage: "\n -M Sort month"
//usage: )
//-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G)
//usage: "\n -n Sort numbers"
//usage: IF_FEATURE_SORT_BIG(
//usage: "\n -t CHAR Field separator"
//usage: "\n -k N[,M] Sort by Nth field"
//usage: )
//usage: "\n -r Reverse sort order"
//usage: IF_FEATURE_SORT_BIG(
//usage: "\n -s Stable (don't sort ties alphabetically)"
//usage: )
//usage: "\n -u Suppress duplicate lines"
//usage: IF_FEATURE_SORT_BIG(
//usage: "\n -z Lines are terminated by NUL, not newline"
///////: "\n -m Ignored for GNU compatibility"
///////: "\n -S BUFSZ Ignored for GNU compatibility"
///////: "\n -T TMPDIR Ignored for GNU compatibility"
//usage: )
//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: ""
#include "libbb.h"
1999-12-22 01:30:35 +05:30
/* These are sort types */
enum {
FLAG_n = 1, /* Numeric sort */
FLAG_g = 2, /* Sort using strtod() */
FLAG_M = 4, /* Sort date */
/* ucsz apply to root level only, not keys. b at root level implies bb */
FLAG_u = 8, /* Unique */
FLAG_c = 0x10, /* Check: no output, exit(!ordered) */
FLAG_s = 0x20, /* Stable sort, no ascii fallback at end */
FLAG_z = 0x40, /* Input and output is NUL terminated, not \n */
/* These can be applied to search keys, the previous four can't */
FLAG_b = 0x80, /* Ignore leading blanks */
FLAG_r = 0x100, /* Reverse */
FLAG_d = 0x200, /* Ignore !(isalnum()|isspace()) */
FLAG_f = 0x400, /* Force uppercase */
FLAG_i = 0x800, /* Ignore !isprint() */
FLAG_m = 0x1000, /* ignored: merge already sorted files; do not sort */
FLAG_S = 0x2000, /* ignored: -S, --buffer-size=SIZE */
FLAG_T = 0x4000, /* ignored: -T, --temporary-directory=DIR */
FLAG_o = 0x8000,
FLAG_k = 0x10000,
FLAG_t = 0x20000,
FLAG_bb = 0x80000000, /* Ignore trailing blanks */
FLAG_no_tie_break = 0x40000000,
};
static const char sort_opt_str[] ALIGN1 = "^"
"ngMucszbrdfimS:T:o:k:*t:"
"\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)
#if ENABLE_FEATURE_SORT_BIG
static char key_separator;
static struct sort_key {
struct sort_key *next_key; /* linked list */
unsigned range[4]; /* start word, start char, end word, end char */
unsigned flags;
} *key_list;
1999-12-22 06:00:29 +05:30
/* This is a NOEXEC applet. Be very careful! */
static char *get_key(char *str, struct sort_key *key, int flags)
1999-12-22 06:00:29 +05:30
{
int start = start; /* for compiler */
int end;
int len, j;
unsigned i;
/* Special case whole string, so we don't have to make a copy */
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;
}
/* Find start of key on first pass, end on second pass */
len = strlen(str);
for (j = 0; j < 2; j++) {
if (!key->range[2*j])
end = len;
/* Loop through fields */
else {
unsigned char ch = 0;
end = 0;
for (i = 1; i < key->range[2*j] + j; i++) {
if (key_separator) {
/* Skip body of key and separator */
while ((ch = str[end]) != '\0') {
end++;
if (ch == key_separator)
break;
}
} else {
/* Skip leading blanks */
while (isspace(str[end]))
end++;
/* Skip body of key */
while (str[end] != '\0') {
if (isspace(str[end]))
break;
end++;
}
}
}
/* Remove last delim: "abc:def:" => "abc:def" */
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]);
end--;
}
}
if (!j) start = end;
}
/* Strip leading whitespace if necessary */
if (flags & FLAG_b)
/* not using skip_whitespace() for speed */
while (isspace(str[start])) start++;
/* Strip trailing whitespace if necessary */
if (flags & FLAG_bb)
while (end > start && isspace(str[end-1])) end--;
/* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */
if (key->range[3]) {
end = key->range[3];
if (end > len) end = len;
}
/* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */
if (key->range[1]) {
start += key->range[1] - 1;
if (start > len) start = len;
}
/* Make the copy */
if (end < start)
end = start;
str = xstrndup(str+start, end-start);
/* Handle -d */
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';
}
/* Handle -i */
if (flags & FLAG_i) {
for (start = end = 0; str[end]; end++)
if (isprint_asciionly(str[end]))
str[start++] = str[end];
str[start] = '\0';
}
/* Handle -f */
if (flags & FLAG_f)
for (i = 0; str[i]; i++)
str[i] = toupper(str[i]);
return str;
1999-12-22 06:00:29 +05:30
}
1999-12-22 01:30:35 +05:30
static struct sort_key *add_key(void)
1999-12-22 01:30:35 +05:30
{
struct sort_key **pkey = &key_list;
while (*pkey)
pkey = &((*pkey)->next_key);
return *pkey = xzalloc(sizeof(struct sort_key));
}
#define GET_LINE(fp) \
((option_mask32 & FLAG_z) \
? bb_get_chunk_from_file(fp, NULL) \
: xmalloc_fgetline(fp))
#else
#define GET_LINE(fp) xmalloc_fgetline(fp)
#endif
2003-03-19 14:43:01 +05:30
/* Iterate through keys list and perform comparisons */
static int compare_keys(const void *xarg, const void *yarg)
{
int flags = option_mask32, retval = 0;
char *x, *y;
2003-03-19 14:43:01 +05:30
#if ENABLE_FEATURE_SORT_BIG
struct sort_key *key;
2006-01-25 05:38:53 +05:30
for (key = key_list; !retval && key; key = key->next_key) {
flags = key->flags ? key->flags : option_mask32;
/* Chop out and modify key chunks, handling -dfib */
x = get_key(*(char **)xarg, key, flags);
y = get_key(*(char **)yarg, key, flags);
#else
/* This curly bracket serves no purpose but to match the nesting
* level of the for () loop we're not using */
{
x = *(char **)xarg;
y = *(char **)yarg;
#endif
/* Perform actual comparison */
switch (flags & (FLAG_n | FLAG_M | FLAG_g)) {
default:
bb_error_msg_and_die("unknown sort type");
break;
/* Ascii sort */
case 0:
#if ENABLE_LOCALE_SUPPORT
retval = strcoll(x, y);
#else
retval = strcmp(x, y);
#endif
break;
#if ENABLE_FEATURE_SORT_BIG
case FLAG_g: {
char *xx, *yy;
2006-11-26 21:18:54 +05:30
double dx = strtod(x, &xx);
double dy = strtod(y, &yy);
/* 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-22 01:30:35 +05:30
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
retval = dx - thyme.tm_mon;
break;
}
/* Full floating point version of -n */
case FLAG_n: {
2006-11-26 21:18:54 +05:30
double dx = atof(x);
double dy = atof(y);
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
break;
}
2006-11-26 21:18:54 +05:30
} /* switch */
/* Free key copies. */
if (x != *(char **)xarg) free(x);
if (y != *(char **)yarg) free(y);
/* if (retval) break; - done by for () anyway */
#else
/* Integer version of -n for tiny systems */
case FLAG_n:
retval = atoi(x) - atoi(y);
break;
2006-11-26 21:18:54 +05:30
} /* switch */
#endif
} /* for */
if (retval == 0) {
/* So far lines are "the same" */
if (option_mask32 & FLAG_s) {
/* "Stable sort": later line is "greater than",
* 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;
/* If x > y, 1, else -1 */
retval = (x32 > y32) * 2 - 1;
} else
if (!(option_mask32 & FLAG_no_tie_break)) {
/* fallback sort */
flags = option_mask32;
retval = strcmp(*(char **)xarg, *(char **)yarg);
}
}
if (flags & FLAG_r)
return -retval;
return retval;
}
#if ENABLE_FEATURE_SORT_BIG
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;
}
#endif
int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2008-07-05 14:48:54 +05:30
int sort_main(int argc UNUSED_PARAM, char **argv)
{
char **lines;
char *str_ignored, *str_o, *str_t;
llist_t *lst_k = NULL;
int i;
int linecount;
unsigned opts;
xfunc_error_retval = 2;
/* Parse command line options */
opts = getopt32(argv,
sort_opt_str,
getopt32: remove opt_complementary function old new delta vgetopt32 1318 1392 +74 runsvdir_main 703 713 +10 bb_make_directory 423 425 +2 collect_cpu 546 545 -1 opt_chars 3 - -3 opt_complementary 4 - -4 tftpd_main 567 562 -5 ntp_init 476 471 -5 zcip_main 1266 1256 -10 xxd_main 428 418 -10 whois_main 140 130 -10 who_main 463 453 -10 which_main 212 202 -10 wget_main 2535 2525 -10 watchdog_main 291 281 -10 watch_main 222 212 -10 vlock_main 399 389 -10 uuencode_main 332 322 -10 uudecode_main 316 306 -10 unlink_main 45 35 -10 udhcpd_main 1482 1472 -10 udhcpc_main 2762 2752 -10 tune2fs_main 290 280 -10 tunctl_main 366 356 -10 truncate_main 218 208 -10 tr_main 518 508 -10 time_main 1134 1124 -10 tftp_main 286 276 -10 telnetd_main 1873 1863 -10 tcpudpsvd_main 1785 1775 -10 taskset_main 521 511 -10 tar_main 1009 999 -10 tail_main 1644 1634 -10 syslogd_main 1967 1957 -10 switch_root_main 368 358 -10 svlogd_main 1454 1444 -10 sv 1296 1286 -10 stat_main 104 94 -10 start_stop_daemon_main 1028 1018 -10 split_main 542 532 -10 sort_main 796 786 -10 slattach_main 624 614 -10 shuf_main 504 494 -10 setsid_main 96 86 -10 setserial_main 1132 1122 -10 setfont_main 388 378 -10 setconsole_main 78 68 -10 sendmail_main 1209 1199 -10 sed_main 677 667 -10 script_main 1077 1067 -10 run_parts_main 325 315 -10 rtcwake_main 454 444 -10 rm_main 175 165 -10 reformime_main 119 109 -10 readlink_main 123 113 -10 rdate_main 246 236 -10 pwdx_main 189 179 -10 pstree_main 317 307 -10 pscan_main 663 653 -10 popmaildir_main 818 808 -10 pmap_main 80 70 -10 nc_main 1042 1032 -10 mv_main 558 548 -10 mountpoint_main 477 467 -10 mount_main 1264 1254 -10 modprobe_main 768 758 -10 modinfo_main 333 323 -10 mktemp_main 200 190 -10 mkswap_main 324 314 -10 mkfs_vfat_main 1489 1479 -10 microcom_main 715 705 -10 md5_sha1_sum_main 521 511 -10 man_main 867 857 -10 makedevs_main 1052 1042 -10 ls_main 563 553 -10 losetup_main 432 422 -10 loadfont_main 89 79 -10 ln_main 524 514 -10 link_main 75 65 -10 ipcalc_main 544 534 -10 iostat_main 2397 2387 -10 install_main 768 758 -10 id_main 480 470 -10 i2cset_main 1239 1229 -10 i2cget_main 380 370 -10 i2cdump_main 1482 1472 -10 i2cdetect_main 682 672 -10 hwclock_main 406 396 -10 httpd_main 741 731 -10 grep_main 837 827 -10 getty_main 1559 1549 -10 fuser_main 297 287 -10 ftpgetput_main 345 335 -10 ftpd_main 2232 2222 -10 fstrim_main 251 241 -10 fsfreeze_main 77 67 -10 fsck_minix_main 2921 2911 -10 flock_main 314 304 -10 flashcp_main 740 730 -10 flash_eraseall_main 833 823 -10 fdformat_main 532 522 -10 expand_main 680 670 -10 eject_main 335 325 -10 dumpleases_main 630 620 -10 du_main 314 304 -10 dos2unix_main 441 431 -10 diff_main 1350 1340 -10 df_main 1064 1054 -10 date_main 1095 1085 -10 cut_main 961 951 -10 cryptpw_main 228 218 -10 crontab_main 575 565 -10 crond_main 1149 1139 -10 cp_main 370 360 -10 common_traceroute_main 3834 3824 -10 common_ping_main 1767 1757 -10 comm_main 239 229 -10 cmp_main 655 645 -10 chrt_main 379 369 -10 chpst_main 704 694 -10 chpasswd_main 308 298 -10 chown_main 171 161 -10 chmod_main 158 148 -10 cat_main 428 418 -10 bzip2_main 120 110 -10 blkdiscard_main 264 254 -10 base64_main 221 211 -10 arping_main 1665 1655 -10 ar_main 556 546 -10 adjtimex_main 406 396 -10 adduser_main 882 872 -10 addgroup_main 411 401 -10 acpid_main 1198 1188 -10 optstring 11 - -11 opt_string 18 - -18 OPT_STR 25 - -25 ubi_tools_main 1288 1258 -30 ls_options 31 - -31 ------------------------------------------------------------------------------ (add/remove: 0/6 grow/shrink: 3/129 up/down: 86/-1383) Total: -1297 bytes text data bss dec hex filename 915428 485 6876 922789 e14a5 busybox_old 914629 485 6872 921986 e1182 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-09 01:25:02 +05:30
&str_ignored, &str_ignored, &str_o, &lst_k, &str_t
);
/* global b strips leading and trailing spaces */
if (opts & FLAG_b)
option_mask32 |= FLAG_bb;
#if ENABLE_FEATURE_SORT_BIG
if (opts & FLAG_t) {
if (!str_t[0] || str_t[1])
bb_error_msg_and_die("bad -t parameter");
key_separator = str_t[0];
}
/* note: below this point we use option_mask32, not opts,
* since that reduces register pressure and makes code smaller */
/* Parse sort key */
while (lst_k) {
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();
char *str_k = llist_pop(&lst_k);
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) {
int flag;
const char *idx;
if (*str_k == ',' && !i++) {
str_k++;
break;
} /* no else needed: fall through to syntax error
because comma isn't in OPT_STR */
idx = strchr(OPT_STR, *str_k);
if (!idx)
bb_error_msg_and_die("unknown key option");
flag = 1 << (idx - OPT_STR);
if (flag & ~FLAG_allowed_for_k)
bb_error_msg_and_die("unknown sort type");
/* b after ',' means strip _trailing_ space */
if (i && flag == FLAG_b)
flag = FLAG_bb;
key->flags |= flag;
str_k++;
}
}
2003-03-19 14:43:01 +05:30
}
#endif
/* Open input files and read data */
argv += optind;
if (!*argv)
*--argv = (char*)"-";
linecount = 0;
lines = NULL;
do {
/* coreutils 6.9 compat: abort on first open error,
* do not continue to next file: */
FILE *fp = xfopen_stdin(*argv);
for (;;) {
char *line = GET_LINE(fp);
if (!line)
break;
lines = xrealloc_vector(lines, 6, linecount);
lines[linecount++] = line;
2003-03-19 14:43:01 +05:30
}
fclose_if_not_stdin(fp);
} while (*++argv);
#if ENABLE_FEATURE_SORT_BIG
/* If no key, perform alphabetic sort */
if (!key_list)
add_key()->range[0] = 1;
/* Handle -c */
if (option_mask32 & FLAG_c) {
int j = (option_mask32 & FLAG_u) ? -1 : 0;
for (i = 1; i < linecount; i++) {
if (compare_keys(&lines[i-1], &lines[i]) > j) {
fprintf(stderr, "Check line %u\n", i);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
#endif
/* 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 */
}
/* Perform the actual sort */
qsort(lines, linecount, sizeof(lines[0]), compare_keys);
/* Handle -u */
if (option_mask32 & FLAG_u) {
int j = 0;
/* 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;
for (i = 1; i < linecount; i++) {
if (compare_keys(&lines[j], &lines[i]) == 0)
free(lines[i]);
else
lines[++j] = lines[i];
}
if (linecount)
linecount = j+1;
}
/* Print it */
#if ENABLE_FEATURE_SORT_BIG
/* Open output file _after_ we read all input ones */
if (option_mask32 & FLAG_o)
xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
#endif
{
int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
for (i = 0; i < linecount; i++)
printf("%s%c", lines[i], ch);
}
2006-10-27 04:51:47 +05:30
fflush_stdout_and_exit(EXIT_SUCCESS);
2000-12-21 02:19:56 +05:30
}