]>
git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/strtol.c
1 /* strtol - Convert string representation of a number into an integer value.
2 Copyright (C) 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #if !defined (HAVE_STRTOL)
30 # define __set_errno(Val) errno = (Val)
44 /* Nonzero if we are defining `strtoul', operating on unsigned integers. */
49 # define RETTYPE unsigned long
52 /* Determine the name. */
54 # define strtol strtoul
62 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
63 # define ULONG_MIN ((unsigned long) 0 - ULONG_MAX)
67 # define LONG_MAX ((long) (ULONG_MAX >> 1))
68 # define LONG_MIN ((long) (0 - LONG_MAX))
71 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
72 If BASE is 0 the base is determined by the presence of a leading
73 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
74 If BASE is < 2 or > 36, it is reset to 10.
75 If ENDPTR is not NULL, a pointer to the character after the last
76 one converted is stored in *ENDPTR. */
79 strtol (nptr
, endptr
, base
)
85 register unsigned long cutoff
, i
;
86 register unsigned int cutlim
;
87 register const char *s
;
88 register unsigned char c
;
89 const char *save
, *end
;
92 if (base
< 0 || base
== 1 || base
> 36)
97 /* Skip white space. */
103 /* Check for a sign. */
104 if (*s
== '-' || *s
== '+')
106 negative
= (*s
== '-');
112 if (base
== 16 && *s
== '0' && toupper (s
[1]) == 'X')
115 /* If BASE is zero, figure it out ourselves. */
119 if (toupper (s
[1]) == 'X')
130 /* Save the pointer so we can check later if anything happened. */
135 cutoff
= ULONG_MAX
/ (unsigned long int) base
;
136 cutlim
= ULONG_MAX
% (unsigned long int) base
;
140 for (c
= *s
; c
!= '\0'; c
= *++s
)
145 if (c
>= '0' && c
<= '9')
147 else if (isalpha (c
))
148 c
= toupper (c
) - 'A' + 10;
155 /* Check for overflow. */
156 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
160 i
*= (unsigned long int) base
;
165 /* Check if anything actually happened. */
169 /* Store in ENDPTR the address of one character
170 past the last character we converted. */
172 *endptr
= (char *) s
;
175 /* Check for a value that is within the range of
176 `unsigned long int', but outside the range of `long int'. */
179 ? -((unsigned long) (LONG_MIN
+ 1)) + 1
180 : (unsigned long) LONG_MAX
))
186 __set_errno (ERANGE
);
190 return negative
? LONG_MIN
: LONG_MAX
;
194 /* Return the result with the appropriate sign. */
195 return (negative
? -i
: i
);
198 /* We must handle a special case here: the base is 0 or 16 and the
199 first two characters are '0' and 'x', but the rest are no
200 hexadecimal digits. This is no error case. We return 0 and
201 ENDPTR points to the `x'. */
204 if (save
- nptr
>= 2 && toupper (save
[-1]) == 'X' && save
[-2] == '0')
205 *endptr
= (char *) &save
[-1];
207 /* There was no number to convert. */
208 *endptr
= (char *) nptr
;
214 #endif /* !HAVE_STRTOL */