From 4f5717dd140e8769f770191bcd1e6ee1f4bbc6d8 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 19 Dec 2018 17:50:16 -0600 Subject: First stab at sort -V --- toys/posix/sort.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'toys') diff --git a/toys/posix/sort.c b/toys/posix/sort.c index 4c9452eb..6f5467e1 100644 --- a/toys/posix/sort.c +++ b/toys/posix/sort.c @@ -7,7 +7,7 @@ * Deviations from POSIX: Lots. * We invented -x -USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")"S:T:m" "o:k*t:xbMcszdfirun", TOYFLAG_USR|TOYFLAG_BIN)) +USE_SORT(NEWTOY(sort, USE_SORT_FLOAT("g")"S:T:m" "o:k*t:" "xVbMcszdfirun", TOYFLAG_USR|TOYFLAG_BIN)) config SORT bool "sort" @@ -32,6 +32,7 @@ config SORT -k Sort by "key" (see below) -t Use a key separator other than whitespace -o Output to FILE instead of stdout + -V Version numbers (name-1.234-rc6.5b.tgz) Sorting by key looks at a subset of the words on each line. -k2 uses the second word to the end of the line, -k2,2 looks at only @@ -168,12 +169,7 @@ static struct sort_key *add_key(void) // Perform actual comparison static int compare_values(int flags, char *x, char *y) { - int ff = flags & (FLAG_n|FLAG_g|FLAG_M|FLAG_x); - - // Ascii sort - if (!ff) return ((flags&FLAG_f) ? strcasecmp : strcmp)(x, y); - - if (CFG_SORT_FLOAT && ff == FLAG_g) { + if (CFG_SORT_FLOAT && (flags & FLAG_g)) { char *xx,*yy; double dx = strtod(x,&xx), dy = strtod(y,&yy); int xinf, yinf; @@ -197,7 +193,7 @@ static int compare_values(int flags, char *x, char *y) if (yinf) return dy<0 ? 1 : -1; return dx>dy ? 1 : (dxyy) return 1; + } else { + char xx = *x ? *x : x[-1], yy = *y ? *y : y[-1]; + + // -rc/-pre hack so abc-123 > abc-123-rc1 (other way already - < 0-9) + if (xx != yy) { + if (xxdy ? 1 : (dx