aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toys/posix/sort.c59
1 files changed, 23 insertions, 36 deletions
diff --git a/toys/posix/sort.c b/toys/posix/sort.c
index a2784cdb..d4a0f4fd 100644
--- a/toys/posix/sort.c
+++ b/toys/posix/sort.c
@@ -7,27 +7,19 @@
* Deviations from POSIX: Lots.
* We invented -x
-USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")USE_SORT_BIG("S:T:m" "o:k*t:xbMcszdfi") "run", TOYFLAG_USR|TOYFLAG_BIN))
+USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")"S:T:m" "o:k*t:xbMcszdfirun", TOYFLAG_USR|TOYFLAG_BIN))
config SORT
bool "sort"
default y
help
- usage: sort [-run] [FILE...]
+ usage: sort [-runbcdfiMsz] [FILE...] [-k#[,#[x]] [-t X]] [-o FILE]
Sort all lines of text from input files (or stdin) to stdout.
-r reverse
-u unique lines only
-n numeric order (instead of alphabetical)
-
-config SORT_BIG
- bool "SuSv3 options (Support -ktcsbdfiozM)"
- default y
- depends on SORT
- help
- usage: sort [-bcdfiMsz] [-k#[,#[x]] [-t X]] [-o FILE]
-
-b ignore leading blanks (or trailing blanks in second part of key)
-c check whether input is sorted
-d dictionary order (use alphanumeric and whitespace chars only)
@@ -51,7 +43,7 @@ config SORT_BIG
config SORT_FLOAT
bool
default y
- depends on SORT_BIG && TOYBOX_FLOAT
+ depends on TOYBOX_FLOAT
help
usage: sort [-g]
@@ -205,7 +197,7 @@ static int compare_values(int flags, char *x, char *y)
if (yinf) return dy<0 ? 1 : -1;
return dx>dy ? 1 : (dx<dy ? -1 : 0);
- } else if (CFG_SORT_BIG && ff == FLAG_M) {
+ } else if (ff == FLAG_M) {
struct tm thyme;
int dx;
char *xx,*yy;
@@ -217,7 +209,7 @@ static int compare_values(int flags, char *x, char *y)
else if (!yy) return 1;
else return dx==thyme.tm_mon ? 0 : dx-thyme.tm_mon;
- } else if (CFG_SORT_BIG && ff == FLAG_x) {
+ } else if (ff == FLAG_x) {
return strtol(x, NULL, 16)-strtol(y, NULL, 16);
// This has to be ff == FLAG_n
} else {
@@ -238,31 +230,27 @@ static int compare_keys(const void *xarg, const void *yarg)
char *x, *y, *xx = *(char **)xarg, *yy = *(char **)yarg;
struct sort_key *key;
- if (CFG_SORT_BIG) {
- for (key=(struct sort_key *)TT.key_list; !retval && key;
- key = key->next_key)
- {
- flags = key->flags ? key->flags : toys.optflags;
+ for (key=(struct sort_key *)TT.key_list; !retval && key; key = key->next_key){
+ flags = key->flags ? key->flags : toys.optflags;
- // Chop out and modify key chunks, handling -dfib
+ // Chop out and modify key chunks, handling -dfib
- x = get_key_data(xx, key, flags);
- y = get_key_data(yy, key, flags);
+ x = get_key_data(xx, key, flags);
+ y = get_key_data(yy, key, flags);
- retval = compare_values(flags, x, y);
+ retval = compare_values(flags, x, y);
- // Free the copies get_key_data() made.
+ // Free the copies get_key_data() made.
- if (x != xx) free(x);
- if (y != yy) free(y);
+ if (x != xx) free(x);
+ if (y != yy) free(y);
- if (retval) break;
- }
- } else retval = compare_values(flags, xx, yy);
+ if (retval) break;
+ }
// Perform fallback sort if necessary (always case insensitive, no -f,
// the point is to get a stable order even for -f sorts)
- if (!retval && !(CFG_SORT_BIG && (toys.optflags&FLAG_s))) {
+ if (!retval && !(toys.optflags&FLAG_s)) {
flags = toys.optflags;
retval = strcmp(xx, yy);
}
@@ -276,13 +264,13 @@ static void sort_read(int fd, char *name)
// Read each line from file, appending to a big array.
for (;;) {
- char * line = (CFG_SORT_BIG && (toys.optflags&FLAG_z))
+ char * line = (toys.optflags&FLAG_z)
? get_rawline(fd, NULL, 0) : get_line(fd);
if (!line) break;
// handle -c here so we don't allocate more memory than necessary.
- if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) {
+ if (toys.optflags&FLAG_c) {
int j = (toys.optflags&FLAG_u) ? -1 : 0;
if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
@@ -303,11 +291,10 @@ void sort_main(void)
int idx, fd = 1;
// Open output file if necessary.
- if (CFG_SORT_BIG && TT.o)
- fd = xcreate(TT.o, O_CREAT|O_TRUNC|O_WRONLY, 0666);
+ if (TT.o) fd = xcreate(TT.o, O_CREAT|O_TRUNC|O_WRONLY, 0666);
// Parse -k sort keys.
- if (CFG_SORT_BIG && TT.k) {
+ if (TT.k) {
struct arg_list *arg;
for (arg = TT.k; arg; arg = arg->next) {
@@ -359,14 +346,14 @@ void sort_main(void)
if (toys.optflags&FLAG_b) toys.optflags |= FLAG_bb;
// If no keys, perform alphabetic sort over the whole line.
- if (CFG_SORT_BIG && !TT.key_list) add_key()->range[0] = 1;
+ if (!TT.key_list) add_key()->range[0] = 1;
// Open input files and read data, populating TT.lines[TT.linecount]
loopfiles(toys.optargs, sort_read);
// The compare (-c) logic was handled in sort_read(),
// so if we got here, we're done.
- if (CFG_SORT_BIG && (toys.optflags&FLAG_c)) goto exit_now;
+ if (toys.optflags&FLAG_c) goto exit_now;
// Perform the actual sort
qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys);