summaryrefslogtreecommitdiff
path: root/src/sort.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-05-22 02:47:45 +0000
committerJim Meyering <jim@meyering.net>1999-05-22 02:47:45 +0000
commit7bac5533d78cad33650ea275a37cf06d7295f3c9 (patch)
tree55ac519dd17f3694289be425b97ed49ea3a3fa5f /src/sort.c
parent28c635adb2d06818e66d5848e91d3a115259d168 (diff)
downloadcoreutils-7bac5533d78cad33650ea275a37cf06d7295f3c9.tar.xz
(strtod): Declare if STDC_HEADERS is not defined.
(general_numcompare): Use strtod, not xstrtod. Do not consider partial conversions to be errors. Put -infinity at the start, and +infinity at the end; follow +infinity with NaNs (sorted by bit pattern), and finally by conversion errors.
Diffstat (limited to 'src/sort.c')
-rw-r--r--src/sort.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/src/sort.c b/src/sort.c
index a9ea5c867..7dd0ac6ef 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -30,7 +30,6 @@
#include "system.h"
#include "long-options.h"
#include "error.h"
-#include "xstrtod.h"
#include "xalloc.h"
/* The official name of this program (e.g., no `g' prefix). */
@@ -48,6 +47,10 @@
# define NAME_MAX_IN_DIR(Dir) 255
#endif
+#ifndef STDC_HEADERS
+double strtod ();
+#endif
+
char *xstrdup ();
/* Undefine, to avoid warning about redefinition on some systems. */
@@ -1064,19 +1067,30 @@ numcompare (register const char *a, register const char *b)
static int
general_numcompare (const char *sa, const char *sb)
{
- double a, b;
/* FIXME: add option to warn about failed conversions. */
/* FIXME: maybe add option to try expensive FP conversion
only if A and B can't be compared more cheaply/accurately. */
- if (xstrtod (sa, NULL, &a))
- {
- a = 0;
- }
- if (xstrtod (sb, NULL, &b))
- {
- b = 0;
- }
- return a == b ? 0 : a < b ? -1 : 1;
+
+ char *ea;
+ char *eb;
+ double a = strtod (sa, &ea);
+ double b = strtod (sb, &eb);
+
+ /* Put conversion errors at the end of the collating sequence. */
+ if (sa == ea)
+ return eb - sb;
+ if (sb == eb)
+ return -1;
+
+ /* Put NaNs after numbers but before conversion errors; sort them by
+ internal bit-pattern, for lack of a more portable alternative.
+ Don't test specially for the case where a is a NaN but b is not,
+ since the conditional at the end of this function does the right
+ thing for that case. */
+ if (b != b)
+ return a == a ? -1 : memcmp ((char *) &a, (char *) &b, sizeof a);
+
+ return a < b ? -1 : a != b;
}
/* Return an integer in 1..12 of the month name S with length LEN.