513
|
1 #ifdef HAVE_CONFIG_H |
|
2 #include <config.h> |
|
3 #endif |
|
4 |
|
5 #ifdef STDC_HEADERS |
|
6 #include <stdlib.h> |
|
7 #else |
|
8 double strtod (); |
|
9 #endif |
|
10 |
|
11 #include <errno.h> |
|
12 #include <stdio.h> |
|
13 #include <limits.h> |
|
14 #include <ctype.h> |
|
15 #include "xstrtod.h" |
|
16 |
|
17 int |
|
18 xstrtod (str, ptr, result) |
|
19 const char *str; |
|
20 const char **ptr; |
|
21 double *result; |
|
22 { |
|
23 double val; |
|
24 char *terminator; |
|
25 int fail; |
|
26 |
|
27 fail = 0; |
|
28 errno = 0; |
|
29 val = strtod (str, &terminator); |
|
30 |
|
31 /* Having a non-zero terminator is an error only when PTR is NULL. */ |
|
32 if (terminator == str || (ptr == NULL && *terminator != '\0')) |
|
33 fail = 1; |
|
34 else |
|
35 { |
|
36 /* Allow underflow (in which case strtod returns zero), |
|
37 but flag overflow as an error. */ |
|
38 if (val != 0.0 && errno == ERANGE) |
|
39 fail = 1; |
|
40 } |
|
41 |
|
42 if (ptr != NULL) |
|
43 *ptr = terminator; |
|
44 |
|
45 *result = val; |
|
46 return fail; |
|
47 } |
|
48 |