2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-22 01:30:35 +05:30
|
|
|
/*
|
2005-01-24 12:30:02 +05:30
|
|
|
* SuS3 compliant sort implementation for busybox
|
1999-12-22 01:30:35 +05:30
|
|
|
*
|
2005-01-24 12:30:02 +05:30
|
|
|
* Copyright (C) 2004 by Rob Landley <rob@landley.net>
|
1999-12-22 01:30:35 +05:30
|
|
|
*
|
2006-02-24 08:00:39 +05:30
|
|
|
* MAINTAINER: Rob Landley <rob@landley.net>
|
2006-09-17 21:58:10 +05:30
|
|
|
*
|
2006-02-24 08:00:39 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-12-22 01:30:35 +05:30
|
|
|
*
|
2005-01-24 12:30:02 +05:30
|
|
|
* See SuS3 sort standard at:
|
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
|
1999-12-22 01:30:35 +05:30
|
|
|
*/
|
|
|
|
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
1999-12-22 01:30:35 +05:30
|
|
|
|
2005-04-17 01:09:00 +05:30
|
|
|
static int global_flags;
|
2005-01-24 12:30:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
|
|
|
|
sort -c [-bdfinru][-t char][-k keydef][file]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* These are sort types */
|
|
|
|
#define FLAG_n 1 /* Numeric sort */
|
|
|
|
#define FLAG_g 2 /* Sort using strtod() */
|
|
|
|
#define FLAG_M 4 /* Sort date */
|
|
|
|
/* ucsz apply to root level only, not keys. b at root level implies bb */
|
|
|
|
#define FLAG_u 8 /* Unique */
|
|
|
|
#define FLAG_c 16 /* Check: no output, exit(!ordered) */
|
|
|
|
#define FLAG_s 32 /* Stable sort, no ascii fallback at end */
|
|
|
|
#define FLAG_z 64 /* Input is null terminated, not \n */
|
|
|
|
/* These can be applied to search keys, the previous four can't */
|
|
|
|
#define FLAG_b 128 /* Ignore leading blanks */
|
|
|
|
#define FLAG_r 256 /* Reverse */
|
|
|
|
#define FLAG_d 512 /* Ignore !(isalnum()|isspace()) */
|
|
|
|
#define FLAG_f 1024 /* Force uppercase */
|
|
|
|
#define FLAG_i 2048 /* Ignore !isprint() */
|
|
|
|
#define FLAG_bb 32768 /* Ignore trailing blanks */
|
|
|
|
|
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2005-04-17 01:09:00 +05:30
|
|
|
static char key_separator;
|
2005-01-24 12:30:02 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
static struct sort_key {
|
2005-01-24 12:30:02 +05:30
|
|
|
struct sort_key *next_key; /* linked list */
|
|
|
|
unsigned short range[4]; /* start word, start char, end word, end char */
|
|
|
|
int flags;
|
|
|
|
} *key_list;
|
1999-12-22 06:00:29 +05:30
|
|
|
|
2005-01-24 12:30:02 +05:30
|
|
|
static char *get_key(char *str, struct sort_key *key, int flags)
|
1999-12-22 06:00:29 +05:30
|
|
|
{
|
2006-11-17 16:58:00 +05:30
|
|
|
int start = 0, end = 0, len, i, j;
|
2005-01-24 12:30:02 +05:30
|
|
|
|
|
|
|
/* Special case whole string, so we don't have to make a copy */
|
2006-11-26 21:18:03 +05:30
|
|
|
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 12:30:02 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
/* 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;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Loop through fields */
|
|
|
|
else {
|
2006-11-26 21:18:03 +05:30
|
|
|
end = 0;
|
|
|
|
for (i = 1; i < key->range[2*j] + j; i++) {
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Skip leading blanks or first separator */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (str[end]) {
|
2006-11-26 21:18:54 +05:30
|
|
|
if (!key_separator)
|
2006-11-26 21:18:03 +05:30
|
|
|
while (isspace(str[end])) end++;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Skip body of key */
|
2006-11-26 21:18:03 +05:30
|
|
|
for (; str[end]; end++) {
|
|
|
|
if (key_separator) {
|
|
|
|
if (str[end] == key_separator)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (isspace(str[end]))
|
|
|
|
break;
|
|
|
|
}
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-26 21:18:03 +05:30
|
|
|
if (!j) start = end;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Key with explicit separator starts after separator */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (key_separator && str[start] == key_separator)
|
|
|
|
start++;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Strip leading whitespace if necessary */
|
2006-11-17 16:58:00 +05:30
|
|
|
//XXX: skip_whitespace()
|
2006-11-26 21:18:03 +05:30
|
|
|
if (flags & FLAG_b)
|
|
|
|
while (isspace(str[start])) start++;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Strip trailing whitespace if necessary */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (flags & FLAG_bb)
|
|
|
|
while (end > start && isspace(str[end-1])) end--;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Handle offsets on start and end */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (key->range[3]) {
|
|
|
|
end += key->range[3] - 1;
|
|
|
|
if (end > len) end = len;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
2006-11-26 21:18:03 +05:30
|
|
|
if (key->range[1]) {
|
|
|
|
start += key->range[1] - 1;
|
|
|
|
if (start > len) start = len;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Make the copy */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (end < start) end = start;
|
|
|
|
str = xstrndup(str+start, end-start);
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Handle -d */
|
2006-11-26 21:18:03 +05:30
|
|
|
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 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Handle -i */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (flags & FLAG_i) {
|
|
|
|
for (start = end = 0; str[end]; end++)
|
|
|
|
if (isprint(str[end]))
|
|
|
|
str[start++] = str[end];
|
|
|
|
str[start] = '\0';
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Handle -f */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (flags & FLAG_f)
|
|
|
|
for (i = 0; str[i]; i++)
|
|
|
|
str[i] = toupper(str[i]);
|
2005-01-24 12:30:02 +05:30
|
|
|
|
|
|
|
return str;
|
1999-12-22 06:00:29 +05:30
|
|
|
}
|
1999-12-22 01:30:35 +05:30
|
|
|
|
2005-01-24 12:30:02 +05:30
|
|
|
static struct sort_key *add_key(void)
|
1999-12-22 01:30:35 +05:30
|
|
|
{
|
2006-11-26 21:18:03 +05:30
|
|
|
struct sort_key **pkey = &key_list;
|
|
|
|
while (*pkey)
|
|
|
|
pkey = &((*pkey)->next_key);
|
2006-08-04 01:37:35 +05:30
|
|
|
return *pkey = xzalloc(sizeof(struct sort_key));
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
#define GET_LINE(fp) \
|
|
|
|
((global_flags & FLAG_z) \
|
|
|
|
? bb_get_chunk_from_file(fp, NULL) \
|
|
|
|
: xmalloc_getline(fp))
|
2005-01-24 12:30:02 +05:30
|
|
|
#else
|
2006-11-26 21:18:03 +05:30
|
|
|
#define GET_LINE(fp) xmalloc_getline(fp)
|
2005-01-24 12:30:02 +05:30
|
|
|
#endif
|
2003-03-19 14:43:01 +05:30
|
|
|
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Iterate through keys list and perform comparisons */
|
|
|
|
static int compare_keys(const void *xarg, const void *yarg)
|
|
|
|
{
|
2006-11-26 21:18:03 +05:30
|
|
|
int flags = global_flags, retval = 0;
|
|
|
|
char *x, *y;
|
2003-03-19 14:43:01 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2005-01-24 12:30:02 +05:30
|
|
|
struct sort_key *key;
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
for (key = key_list; !retval && key; key = key->next_key) {
|
2006-11-26 21:18:54 +05:30
|
|
|
flags = key->flags ? key->flags : global_flags;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Chop out and modify key chunks, handling -dfib */
|
2006-11-26 21:18:03 +05:30
|
|
|
x = get_key(*(char **)xarg, key, flags);
|
|
|
|
y = get_key(*(char **)yarg, key, flags);
|
2005-01-24 12:30:02 +05:30
|
|
|
#else
|
|
|
|
/* This curly bracket serves no purpose but to match the nesting
|
|
|
|
level of the for() loop we're not using */
|
|
|
|
{
|
2006-11-26 21:18:03 +05:30
|
|
|
x = *(char **)xarg;
|
|
|
|
y = *(char **)yarg;
|
2005-01-24 12:30:02 +05:30
|
|
|
#endif
|
|
|
|
/* Perform actual comparison */
|
2006-11-26 21:18:03 +05:30
|
|
|
switch (flags & 7) {
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unknown sort type");
|
|
|
|
break;
|
|
|
|
/* Ascii sort */
|
|
|
|
case 0:
|
|
|
|
retval = strcmp(x, y);
|
|
|
|
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);
|
2006-11-26 21:18:03 +05:30
|
|
|
/* 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
|
|
|
|
2006-11-26 21:18:03 +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
|
2006-11-26 21:18:54 +05:30
|
|
|
retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
|
2006-11-26 21:18:03 +05:30
|
|
|
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);
|
2006-11-26 21:18:03 +05:30
|
|
|
retval = (dx > dy) ? 1 : ((dx < dy) ? -1 : 0);
|
|
|
|
break;
|
|
|
|
}
|
2006-11-26 21:18:54 +05:30
|
|
|
} /* switch */
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Free key copies. */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (x != *(char **)xarg) free(x);
|
|
|
|
if (y != *(char **)yarg) free(y);
|
2006-11-26 21:18:54 +05:30
|
|
|
/* if (retval) break; - done by for() anyway */
|
2005-01-24 12:30:02 +05:30
|
|
|
#else
|
2006-11-26 21:18:03 +05:30
|
|
|
/* 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 */
|
2005-01-24 12:30:02 +05:30
|
|
|
#endif
|
2003-03-19 14:43:01 +05:30
|
|
|
}
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Perform fallback sort if necessary */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (!retval && !(global_flags & FLAG_s))
|
|
|
|
retval = strcmp(*(char **)xarg, *(char **)yarg);
|
|
|
|
|
|
|
|
if (flags & FLAG_r) return -retval;
|
|
|
|
return retval;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
2000-09-28 23:19:59 +05:30
|
|
|
|
2005-01-24 12:30:02 +05:30
|
|
|
int sort_main(int argc, char **argv)
|
|
|
|
{
|
2006-11-26 21:18:03 +05:30
|
|
|
FILE *fp, *outfile = NULL;
|
|
|
|
int linecount = 0, i, flag;
|
|
|
|
char *line, **lines = NULL, *optlist = "ngMucszbrdfimS:T:o:k:t:";
|
2005-07-21 00:37:27 +05:30
|
|
|
int c;
|
2005-01-24 12:30:02 +05:30
|
|
|
|
2006-10-04 01:58:06 +05:30
|
|
|
xfunc_error_retval = 2;
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Parse command line options */
|
2006-11-26 21:18:03 +05:30
|
|
|
while ((c = getopt(argc, argv, optlist)) > 0) {
|
|
|
|
line = strchr(optlist, c);
|
|
|
|
if (!line) bb_show_usage();
|
|
|
|
switch (*line) {
|
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
|
|
|
case 'o':
|
|
|
|
if (outfile) bb_error_msg_and_die("too many -o");
|
|
|
|
outfile = xfopen(optarg, "w");
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (key_separator || optarg[1])
|
|
|
|
bb_error_msg_and_die("too many -t");
|
|
|
|
key_separator = *optarg;
|
|
|
|
break;
|
|
|
|
/* parse sort key */
|
|
|
|
case 'k': {
|
|
|
|
struct sort_key *key = add_key();
|
|
|
|
char *temp, *temp2;
|
2005-01-24 12:30:02 +05:30
|
|
|
|
2006-11-26 21:18:03 +05:30
|
|
|
temp = optarg;
|
|
|
|
for (i = 0; *temp;) {
|
|
|
|
/* Start of range */
|
|
|
|
key->range[2*i] = (unsigned short)strtol(temp, &temp, 10);
|
|
|
|
if (*temp == '.')
|
|
|
|
key->range[(2*i)+1] = (unsigned short)strtol(temp+1, &temp, 10);
|
|
|
|
for (; *temp; temp++) {
|
|
|
|
if (*temp == ',' && !i++) {
|
|
|
|
temp++;
|
|
|
|
break;
|
|
|
|
} /* no else needed: fall through to syntax error
|
|
|
|
because comma isn't in optlist */
|
|
|
|
temp2 = strchr(optlist, *temp);
|
|
|
|
flag = (1 << (temp2 - optlist));
|
|
|
|
if (!temp2 || (flag > FLAG_M && flag < FLAG_b))
|
|
|
|
bb_error_msg_and_die("unknown key option");
|
|
|
|
/* b after ',' means strip _trailing_ space */
|
|
|
|
if (i && flag == FLAG_b) flag = FLAG_bb;
|
|
|
|
key->flags |= flag;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
}
|
2006-11-26 21:18:03 +05:30
|
|
|
break;
|
|
|
|
}
|
2005-01-24 12:30:02 +05:30
|
|
|
#endif
|
2006-11-26 21:18:03 +05:30
|
|
|
default:
|
|
|
|
global_flags |= (1 << (line - optlist));
|
|
|
|
/* global b strips leading and trailing spaces */
|
|
|
|
if (global_flags & FLAG_b) global_flags |= FLAG_bb;
|
|
|
|
break;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
}
|
2005-01-24 12:30:02 +05:30
|
|
|
/* Open input files and read data */
|
2006-11-26 21:18:03 +05:30
|
|
|
for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
|
|
|
|
fp = stdin;
|
2006-12-17 05:19:13 +05:30
|
|
|
if (i >= optind && NOT_LONE_DASH(argv[i]))
|
2006-11-26 21:18:03 +05:30
|
|
|
fp = xfopen(argv[i], "r");
|
|
|
|
for (;;) {
|
|
|
|
line = GET_LINE(fp);
|
|
|
|
if (!line) break;
|
|
|
|
if (!(linecount & 63))
|
|
|
|
lines = xrealloc(lines, sizeof(char *) * (linecount + 64));
|
|
|
|
lines[linecount++] = line;
|
2003-03-19 14:43:01 +05:30
|
|
|
}
|
2005-01-24 12:30:02 +05:30
|
|
|
fclose(fp);
|
2003-03-19 14:43:01 +05:30
|
|
|
}
|
2006-11-26 21:18:03 +05:30
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2005-01-24 12:30:02 +05:30
|
|
|
/* if no key, perform alphabetic sort */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (!key_list)
|
|
|
|
add_key()->range[0] = 1;
|
2006-01-25 05:38:53 +05:30
|
|
|
/* handle -c */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (global_flags & FLAG_c) {
|
|
|
|
int j = (global_flags & FLAG_u) ? -1 : 0;
|
|
|
|
for (i = 1; i < linecount; i++)
|
|
|
|
if (compare_keys(&lines[i-1], &lines[i]) > j) {
|
|
|
|
fprintf(stderr, "Check line %d\n", i);
|
2005-01-24 12:30:02 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Perform the actual sort */
|
2006-11-26 21:18:03 +05:30
|
|
|
qsort(lines, linecount, sizeof(char *), compare_keys);
|
2005-01-24 12:30:02 +05:30
|
|
|
/* handle -u */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (global_flags & FLAG_u) {
|
|
|
|
for (flag = 0, i = 1; i < linecount; i++) {
|
|
|
|
if (!compare_keys(&lines[flag], &lines[i]))
|
|
|
|
free(lines[i]);
|
|
|
|
else
|
|
|
|
lines[++flag] = lines[i];
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
2006-11-26 21:18:03 +05:30
|
|
|
if (linecount) linecount = flag+1;
|
2005-01-24 12:30:02 +05:30
|
|
|
}
|
|
|
|
/* Print it */
|
2006-11-26 21:18:03 +05:30
|
|
|
if (!outfile) outfile = stdout;
|
|
|
|
for (i = 0; i < linecount; i++)
|
|
|
|
fprintf(outfile, "%s\n", lines[i]);
|
|
|
|
|
2006-10-27 04:51:47 +05:30
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2000-12-21 02:19:56 +05:30
|
|
|
}
|