summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2003-09-17 16:33:17 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2003-09-17 16:33:17 +0000
commitdd3498f6b29544bd772ea4e002bb02710a9000f2 (patch)
tree7fcf81a64785ef00ca373a0ab69d2fb1aeb23b27
parent506a044efbd6c756e499c134263a27323ad770b6 (diff)
downloadmonitoring-plugins-dd3498f6b29544bd772ea4e002bb02710a9000f2.tar.gz
Required if strtod not GNU compilant
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@737 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r--lib/strtod.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/lib/strtod.c b/lib/strtod.c
new file mode 100644
index 00000000..10edb5cf
--- /dev/null
+++ b/lib/strtod.c
@@ -0,0 +1,202 @@
1/* Copyright (C) 1991, 1992, 1997, 1999 Free Software Foundation, Inc.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17#if HAVE_CONFIG_H
18# include <config.h>
19#endif
20
21#include <errno.h>
22#ifndef errno
23extern int errno;
24#endif
25
26#include <ctype.h>
27
28#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
29# define IN_CTYPE_DOMAIN(c) 1
30#else
31# define IN_CTYPE_DOMAIN(c) isascii(c)
32#endif
33
34#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
35#define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
36#define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))
37
38#include <math.h>
39
40#if HAVE_FLOAT_H
41# include <float.h>
42#else
43# define DBL_MAX 1.7976931348623159e+308
44# define DBL_MIN 2.2250738585072010e-308
45#endif
46
47#if STDC_HEADERS
48# include <stdlib.h>
49# include <string.h>
50#else
51# define NULL 0
52# ifndef HUGE_VAL
53# define HUGE_VAL HUGE
54# endif
55#endif
56
57/* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the
58 character after the last one used in the number is put in *ENDPTR. */
59double
60strtod (const char *nptr, char **endptr)
61{
62 register const char *s;
63 short int sign;
64
65 /* The number so far. */
66 double num;
67
68 int got_dot; /* Found a decimal point. */
69 int got_digit; /* Seen any digits. */
70
71 /* The exponent of the number. */
72 long int exponent;
73
74 if (nptr == NULL)
75 {
76 errno = EINVAL;
77 goto noconv;
78 }
79
80 s = nptr;
81
82 /* Eat whitespace. */
83 while (ISSPACE (*s))
84 ++s;
85
86 /* Get the sign. */
87 sign = *s == '-' ? -1 : 1;
88 if (*s == '-' || *s == '+')
89 ++s;
90
91 num = 0.0;
92 got_dot = 0;
93 got_digit = 0;
94 exponent = 0;
95 for (;; ++s)
96 {
97 if (ISDIGIT (*s))
98 {
99 got_digit = 1;
100
101 /* Make sure that multiplication by 10 will not overflow. */
102 if (num > DBL_MAX * 0.1)
103 /* The value of the digit doesn't matter, since we have already
104 gotten as many digits as can be represented in a `double'.
105 This doesn't necessarily mean the result will overflow.
106 The exponent may reduce it to within range.
107
108 We just need to record that there was another
109 digit so that we can multiply by 10 later. */
110 ++exponent;
111 else
112 num = (num * 10.0) + (*s - '0');
113
114 /* Keep track of the number of digits after the decimal point.
115 If we just divided by 10 here, we would lose precision. */
116 if (got_dot)
117 --exponent;
118 }
119 else if (!got_dot && *s == '.')
120 /* Record that we have found the decimal point. */
121 got_dot = 1;
122 else
123 /* Any other character terminates the number. */
124 break;
125 }
126
127 if (!got_digit)
128 goto noconv;
129
130 if (TOLOWER (*s) == 'e')
131 {
132 /* Get the exponent specified after the `e' or `E'. */
133 int save = errno;
134 char *end;
135 long int exp;
136
137 errno = 0;
138 ++s;
139 exp = strtol (s, &end, 10);
140 if (errno == ERANGE)
141 {
142 /* The exponent overflowed a `long int'. It is probably a safe
143 assumption that an exponent that cannot be represented by
144 a `long int' exceeds the limits of a `double'. */
145 if (endptr != NULL)
146 *endptr = end;
147 if (exp < 0)
148 goto underflow;
149 else
150 goto overflow;
151 }
152 else if (end == s)
153 /* There was no exponent. Reset END to point to
154 the 'e' or 'E', so *ENDPTR will be set there. */
155 end = (char *) s - 1;
156 errno = save;
157 s = end;
158 exponent += exp;
159 }
160
161 if (endptr != NULL)
162 *endptr = (char *) s;
163
164 if (num == 0.0)
165 return 0.0;
166
167 /* Multiply NUM by 10 to the EXPONENT power,
168 checking for overflow and underflow. */
169
170 if (exponent < 0)
171 {
172 if (num < DBL_MIN * pow (10.0, (double) -exponent))
173 goto underflow;
174 }
175 else if (exponent > 0)
176 {
177 if (num > DBL_MAX * pow (10.0, (double) -exponent))
178 goto overflow;
179 }
180
181 num *= pow (10.0, (double) exponent);
182
183 return num * sign;
184
185overflow:
186 /* Return an overflow error. */
187 errno = ERANGE;
188 return HUGE_VAL * sign;
189
190underflow:
191 /* Return an underflow error. */
192 if (endptr != NULL)
193 *endptr = (char *) nptr;
194 errno = ERANGE;
195 return 0.0;
196
197noconv:
198 /* There was no number. */
199 if (endptr != NULL)
200 *endptr = (char *) nptr;
201 return 0.0;
202}