aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/sort.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2022-01-01 13:32:33 -0600
committerRob Landley <rob@landley.net>2022-01-01 13:32:33 -0600
commit7aaf3f36f86dc2c17ec1e86c77b1790d706d41bd (patch)
tree62fb908fcfd68828dd246b5ec5cda48bd28a27bb /toys/posix/sort.c
parent218e3aa7eb15c62a0f37dd07f2b3e2170636b5c5 (diff)
downloadtoybox-7aaf3f36f86dc2c17ec1e86c77b1790d706d41bd.tar.gz
Teach sort -h to handle decimals.
(Didn't want to tangle it into the "isinf/nan" stuff with -g, but forgot to actually add the decimal part.)
Diffstat (limited to 'toys/posix/sort.c')
-rw-r--r--toys/posix/sort.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/toys/posix/sort.c b/toys/posix/sort.c
index 574063ea..cedf9269 100644
--- a/toys/posix/sort.c
+++ b/toys/posix/sort.c
@@ -231,13 +231,16 @@ static int compare_values(int flags, char *x, char *y)
// Full floating point version of -n
if (CFG_SORT_FLOAT) {
char *xx = x, *units = "kmgtpez";
- double dxy[2];
+ double dxy[2], zz = 1;
int i = 0;
do {
dxy[i] = estrtol(xx, &xx, 0);
if (flags & FLAG_h) {
- if (isspace(*xx)) xx++;
+ if (*xx=='.') {
+ xx++;
+ while (isdigit(*xx)) dxy[i] += ((*xx++)-'0')*(zz/=10);
+ }
if (*xx && (xx = strchr(units, tolower(*xx))))
do dxy[i] *= 1024; while (xx-->units);
}
@@ -245,7 +248,7 @@ static int compare_values(int flags, char *x, char *y)
} while (!i++);
return dxy[0]<dxy[1] ? -1 : dxy[0]>dxy[1];
- // Integer version of -n for tiny systems
+ // Integer version of -n for tiny systems. (qsort needs int return not long)
} else return atoi(x)-atoi(y);
// Ascii sort