198b9484cSchristos /* Implementation of strtod for systems with atof. 2*7e120ff0Schristos Copyright (C) 1991-2024 Free Software Foundation, Inc. 398b9484cSchristos 498b9484cSchristos This file is part of the libiberty library. This library is free 598b9484cSchristos software; you can redistribute it and/or modify it under the 698b9484cSchristos terms of the GNU General Public License as published by the 798b9484cSchristos Free Software Foundation; either version 2, or (at your option) 898b9484cSchristos any later version. 998b9484cSchristos 1098b9484cSchristos This library is distributed in the hope that it will be useful, 1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1398b9484cSchristos GNU General Public License for more details. 1498b9484cSchristos 1598b9484cSchristos You should have received a copy of the GNU General Public License 1698b9484cSchristos along with GNU CC; see the file COPYING. If not, write to 1798b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 1898b9484cSchristos 1998b9484cSchristos As a special exception, if you link this library with files 2098b9484cSchristos compiled with a GNU compiler to produce an executable, this does not cause 2198b9484cSchristos the resulting executable to be covered by the GNU General Public License. 2298b9484cSchristos This exception does not however invalidate any other reasons why 2398b9484cSchristos the executable file might be covered by the GNU General Public License. */ 2498b9484cSchristos 2598b9484cSchristos /* 2698b9484cSchristos 2798b9484cSchristos @deftypefn Supplemental double strtod (const char *@var{string}, @ 2898b9484cSchristos char **@var{endptr}) 2998b9484cSchristos 3098b9484cSchristos This ISO C function converts the initial portion of @var{string} to a 3198b9484cSchristos @code{double}. If @var{endptr} is not @code{NULL}, a pointer to the 3298b9484cSchristos character after the last character used in the conversion is stored in 3398b9484cSchristos the location referenced by @var{endptr}. If no conversion is 3498b9484cSchristos performed, zero is returned and the value of @var{string} is stored in 3598b9484cSchristos the location referenced by @var{endptr}. 3698b9484cSchristos 3798b9484cSchristos @end deftypefn 3898b9484cSchristos 3998b9484cSchristos */ 4098b9484cSchristos 4198b9484cSchristos #include "ansidecl.h" 4298b9484cSchristos #include "safe-ctype.h" 4398b9484cSchristos 4498b9484cSchristos extern double atof (const char *); 4598b9484cSchristos 4698b9484cSchristos /* Disclaimer: this is currently just used by CHILL in GDB and therefore 4798b9484cSchristos has not been tested well. It may have been tested for nothing except 4898b9484cSchristos that it compiles. */ 4998b9484cSchristos 5098b9484cSchristos double 5198b9484cSchristos strtod (char *str, char **ptr) 5298b9484cSchristos { 5398b9484cSchristos char *p; 5498b9484cSchristos 5598b9484cSchristos if (ptr == (char **)0) 5698b9484cSchristos return atof (str); 5798b9484cSchristos 5898b9484cSchristos p = str; 5998b9484cSchristos 6098b9484cSchristos while (ISSPACE (*p)) 6198b9484cSchristos ++p; 6298b9484cSchristos 6398b9484cSchristos if (*p == '+' || *p == '-') 6498b9484cSchristos ++p; 6598b9484cSchristos 6698b9484cSchristos /* INF or INFINITY. */ 6798b9484cSchristos if ((p[0] == 'i' || p[0] == 'I') 6898b9484cSchristos && (p[1] == 'n' || p[1] == 'N') 6998b9484cSchristos && (p[2] == 'f' || p[2] == 'F')) 7098b9484cSchristos { 7198b9484cSchristos if ((p[3] == 'i' || p[3] == 'I') 7298b9484cSchristos && (p[4] == 'n' || p[4] == 'N') 7398b9484cSchristos && (p[5] == 'i' || p[5] == 'I') 7498b9484cSchristos && (p[6] == 't' || p[6] == 'T') 7598b9484cSchristos && (p[7] == 'y' || p[7] == 'Y')) 7698b9484cSchristos { 7798b9484cSchristos *ptr = p + 8; 7898b9484cSchristos return atof (str); 7998b9484cSchristos } 8098b9484cSchristos else 8198b9484cSchristos { 8298b9484cSchristos *ptr = p + 3; 8398b9484cSchristos return atof (str); 8498b9484cSchristos } 8598b9484cSchristos } 8698b9484cSchristos 8798b9484cSchristos /* NAN or NAN(foo). */ 8898b9484cSchristos if ((p[0] == 'n' || p[0] == 'N') 8998b9484cSchristos && (p[1] == 'a' || p[1] == 'A') 9098b9484cSchristos && (p[2] == 'n' || p[2] == 'N')) 9198b9484cSchristos { 9298b9484cSchristos p += 3; 9398b9484cSchristos if (*p == '(') 9498b9484cSchristos { 9598b9484cSchristos ++p; 9698b9484cSchristos while (*p != '\0' && *p != ')') 9798b9484cSchristos ++p; 9898b9484cSchristos if (*p == ')') 9998b9484cSchristos ++p; 10098b9484cSchristos } 10198b9484cSchristos *ptr = p; 10298b9484cSchristos return atof (str); 10398b9484cSchristos } 10498b9484cSchristos 10598b9484cSchristos /* digits, with 0 or 1 periods in it. */ 10698b9484cSchristos if (ISDIGIT (*p) || *p == '.') 10798b9484cSchristos { 10898b9484cSchristos int got_dot = 0; 10998b9484cSchristos while (ISDIGIT (*p) || (!got_dot && *p == '.')) 11098b9484cSchristos { 11198b9484cSchristos if (*p == '.') 11298b9484cSchristos got_dot = 1; 11398b9484cSchristos ++p; 11498b9484cSchristos } 11598b9484cSchristos 11698b9484cSchristos /* Exponent. */ 11798b9484cSchristos if (*p == 'e' || *p == 'E') 11898b9484cSchristos { 11998b9484cSchristos int i; 12098b9484cSchristos i = 1; 12198b9484cSchristos if (p[i] == '+' || p[i] == '-') 12298b9484cSchristos ++i; 12398b9484cSchristos if (ISDIGIT (p[i])) 12498b9484cSchristos { 12598b9484cSchristos while (ISDIGIT (p[i])) 12698b9484cSchristos ++i; 12798b9484cSchristos *ptr = p + i; 12898b9484cSchristos return atof (str); 12998b9484cSchristos } 13098b9484cSchristos } 13198b9484cSchristos *ptr = p; 13298b9484cSchristos return atof (str); 13398b9484cSchristos } 13498b9484cSchristos /* Didn't find any digits. Doesn't look like a number. */ 13598b9484cSchristos *ptr = str; 13698b9484cSchristos return 0.0; 13798b9484cSchristos } 138