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
|
|
|
*
|
2006-02-24 02:30:39 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
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
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
1999-12-21 20:00:35 +00:00
|
|
|
|
2007-04-10 15:43:37 +00:00
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/*
|
|
|
|
sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
|
|
|
|
sort -c [-bdfinru][-t char][-k keydef][file]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* These are sort types */
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
|
2007-01-01 18:18:04 +00:00
|
|
|
enum {
|
|
|
|
FLAG_n = 1, /* Numeric sort */
|
|
|
|
FLAG_g = 2, /* Sort using strtod() */
|
|
|
|
FLAG_M = 4, /* Sort date */
|
2005-01-24 07:00:02 +00:00
|
|
|
/* ucsz apply to root level only, not keys. b at root level implies bb */
|
2007-01-01 18:18:04 +00:00
|
|
|
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 is null terminated, not \n */
|
2005-01-24 07:00:02 +00:00
|
|
|
/* These can be applied to search keys, the previous four can't */
|
2007-01-01 18:18:04 +00:00
|
|
|
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 */
|
|
|
|
};
|
2005-01-24 07:00:02 +00:00
|
|
|
|
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 {
|
2005-01-24 07:00:02 +00:00
|
|
|
struct sort_key *next_key; /* linked list */
|
2007-01-01 18:18:04 +00:00
|
|
|
unsigned range[4]; /* start word, start char, end word, end char */
|
|
|
|
unsigned flags;
|
2005-01-24 07:00:02 +00:00
|
|
|
} *key_list;
|
1999-12-22 00:30:29 +00:00
|
|
|
|
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
|
|
|
{
|
2006-11-17 11:28:00 +00:00
|
|
|
int start = 0, end = 0, len, i, j;
|
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 {
|
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 */
|
2007-01-01 18:18:04 +00:00
|
|
|
while (str[end]) {
|
2007-01-07 00:21:41 +00:00
|
|
|
if (str[end++] == 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 */
|
|
|
|
while (str[end]) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-17 11:28:00 +00:00
|
|
|
//XXX: skip_whitespace()
|
2006-11-26 15:48:03 +00:00
|
|
|
if (flags & FLAG_b)
|
|
|
|
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--;
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Handle offsets on start and end */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (key->range[3]) {
|
|
|
|
end += key->range[3] - 1;
|
|
|
|
if (end > len) end = len;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
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 */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (end < start) end = start;
|
|
|
|
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++)
|
|
|
|
if (isprint(str[end]))
|
|
|
|
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) \
|
|
|
|
: xmalloc_getline(fp))
|
2005-01-24 07:00:02 +00:00
|
|
|
#else
|
2006-11-26 15:48:03 +00:00
|
|
|
#define GET_LINE(fp) xmalloc_getline(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
|
2006-12-26 10:42:51 +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 */
|
2006-11-26 15:48:03 +00:00
|
|
|
switch (flags & 7) {
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unknown sort type");
|
|
|
|
break;
|
|
|
|
/* 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
|
2006-11-26 15:48:54 +00:00
|
|
|
retval = (dx == thyme.tm_mon) ? 0 : 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 */
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Perform fallback sort if necessary */
|
2007-01-01 18:18:04 +00:00
|
|
|
if (!retval && !(option_mask32 & FLAG_s))
|
2006-11-26 15:48:03 +00:00
|
|
|
retval = strcmp(*(char **)xarg, *(char **)yarg);
|
|
|
|
|
|
|
|
if (flags & FLAG_r) return -retval;
|
|
|
|
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;
|
2005-01-24 07:00:02 +00:00
|
|
|
int sort_main(int argc, char **argv)
|
|
|
|
{
|
2007-01-01 18:18:04 +00:00
|
|
|
FILE *fp, *outfile = stdout;
|
|
|
|
char *line, **lines = NULL;
|
2007-02-17 18:11:45 +00:00
|
|
|
char *str_ignored, *str_o, *str_t;
|
|
|
|
llist_t *lst_k = NULL;
|
2007-01-01 18:18:04 +00:00
|
|
|
int i, flag;
|
|
|
|
int linecount = 0;
|
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 */
|
2007-01-01 18:18:04 +00:00
|
|
|
/* -o and -t can be given at most once */
|
2007-07-21 13:27:44 +00:00
|
|
|
opt_complementary = "o--o:t--t:" /* -t, -o: maximum one of each */
|
2007-02-17 18:11:45 +00:00
|
|
|
"k::"; /* -k takes list */
|
2007-08-18 15:32:12 +00:00
|
|
|
getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
|
2006-11-26 15:48:03 +00:00
|
|
|
#if ENABLE_FEATURE_SORT_BIG
|
2007-01-01 18:18:04 +00:00
|
|
|
if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w");
|
|
|
|
if (option_mask32 & FLAG_t) {
|
|
|
|
if (!str_t[0] || str_t[1])
|
|
|
|
bb_error_msg_and_die("bad -t parameter");
|
|
|
|
key_separator = str_t[0];
|
|
|
|
}
|
|
|
|
/* 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();
|
2007-02-17 18:11:45 +00:00
|
|
|
char *str_k = lst_k->data;
|
2007-01-01 18:18:04 +00:00
|
|
|
const char *temp2;
|
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) {
|
|
|
|
if (*str_k == ',' && !i++) {
|
|
|
|
str_k++;
|
|
|
|
break;
|
|
|
|
} /* no else needed: fall through to syntax error
|
|
|
|
because comma isn't in OPT_STR */
|
|
|
|
temp2 = strchr(OPT_STR, *str_k);
|
|
|
|
if (!temp2)
|
|
|
|
bb_error_msg_and_die("unknown key option");
|
|
|
|
flag = 1 << (temp2 - 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++;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
2007-02-17 18:11:45 +00:00
|
|
|
/* leaking lst_k... */
|
|
|
|
lst_k = lst_k->link;
|
2003-03-19 09:13:01 +00:00
|
|
|
}
|
2007-01-01 18:18:04 +00:00
|
|
|
#endif
|
|
|
|
/* global b strips leading and trailing spaces */
|
|
|
|
if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb;
|
|
|
|
|
2005-01-24 07:00:02 +00:00
|
|
|
/* Open input files and read data */
|
2006-11-26 15:48:03 +00:00
|
|
|
for (i = argv[optind] ? optind : optind-1; argv[i]; i++) {
|
|
|
|
fp = stdin;
|
2006-12-16 23:49:13 +00:00
|
|
|
if (i >= optind && NOT_LONE_DASH(argv[i]))
|
2006-11-26 15:48:03 +00:00
|
|
|
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 09:13:01 +00:00
|
|
|
}
|
2005-01-24 07:00:02 +00:00
|
|
|
fclose(fp);
|
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
|
|
|
/* if no key, perform alphabetic sort */
|
2006-11-26 15:48:03 +00:00
|
|
|
if (!key_list)
|
|
|
|
add_key()->range[0] = 1;
|
2006-01-25 00:08:53 +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;
|
2006-11-26 15:48:03 +00:00
|
|
|
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 07:00:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Perform the actual sort */
|
2006-11-26 15:48:03 +00:00
|
|
|
qsort(lines, linecount, sizeof(char *), compare_keys);
|
2005-01-24 07:00:02 +00:00
|
|
|
/* handle -u */
|
2007-01-01 18:18:04 +00:00
|
|
|
if (option_mask32 & FLAG_u) {
|
|
|
|
flag = 0;
|
|
|
|
/* coreutils 6.3 drop lines for which only key is the same */
|
|
|
|
/* -- disabling last-resort compare... */
|
|
|
|
option_mask32 |= FLAG_s;
|
|
|
|
for (i = 1; i < linecount; i++) {
|
2006-11-26 15:48:03 +00:00
|
|
|
if (!compare_keys(&lines[flag], &lines[i]))
|
|
|
|
free(lines[i]);
|
|
|
|
else
|
|
|
|
lines[++flag] = lines[i];
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
2006-11-26 15:48:03 +00:00
|
|
|
if (linecount) linecount = flag+1;
|
2005-01-24 07:00:02 +00:00
|
|
|
}
|
|
|
|
/* Print it */
|
2006-11-26 15:48:03 +00:00
|
|
|
for (i = 0; i < linecount; i++)
|
|
|
|
fprintf(outfile, "%s\n", lines[i]);
|
|
|
|
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2000-12-20 20:49:56 +00:00
|
|
|
}
|