2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
<<strtol>>, <<strtol_l>>---string to long
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
strtol
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
strtol_l
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
INDEX
|
|
|
|
_strtol_r
|
|
|
|
|
2017-11-30 16:17:18 +08:00
|
|
|
SYNOPSIS
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <stdlib.h>
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
long strtol(const char *restrict <[s]>, char **restrict <[ptr]>,
|
|
|
|
int <[base]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
long strtol_l(const char *restrict <[s]>, char **restrict <[ptr]>,
|
|
|
|
int <[base]>, locale_t <[locale]>);
|
|
|
|
|
|
|
|
long _strtol_r(void *<[reent]>, const char *restrict <[s]>,
|
|
|
|
char **restrict <[ptr]>,int <[base]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
The function <<strtol>> converts the string <<*<[s]>>> to
|
|
|
|
a <<long>>. First, it breaks down the string into three parts:
|
|
|
|
leading whitespace, which is ignored; a subject string consisting
|
|
|
|
of characters resembling an integer in the radix specified by <[base]>;
|
|
|
|
and a trailing portion consisting of zero or more unparseable characters,
|
|
|
|
and always including the terminating null character. Then, it attempts
|
|
|
|
to convert the subject string into a <<long>> and returns the
|
|
|
|
result.
|
|
|
|
|
|
|
|
If the value of <[base]> is 0, the subject string is expected to look
|
|
|
|
like a normal C integer constant: an optional sign, a possible `<<0x>>'
|
|
|
|
indicating a hexadecimal base, and a number. If <[base]> is between
|
|
|
|
2 and 36, the expected form of the subject is a sequence of letters
|
|
|
|
and digits representing an integer in the radix specified by <[base]>,
|
|
|
|
with an optional plus or minus sign. The letters <<a>>--<<z>> (or,
|
|
|
|
equivalently, <<A>>--<<Z>>) are used to signify values from 10 to 35;
|
|
|
|
only letters whose ascribed values are less than <[base]> are
|
|
|
|
permitted. If <[base]> is 16, a leading <<0x>> is permitted.
|
|
|
|
|
|
|
|
The subject sequence is the longest initial sequence of the input
|
|
|
|
string that has the expected form, starting with the first
|
|
|
|
non-whitespace character. If the string is empty or consists entirely
|
|
|
|
of whitespace, or if the first non-whitespace character is not a
|
|
|
|
permissible letter or digit, the subject string is empty.
|
|
|
|
|
|
|
|
If the subject string is acceptable, and the value of <[base]> is zero,
|
|
|
|
<<strtol>> attempts to determine the radix from the input string. A
|
|
|
|
string with a leading <<0x>> is treated as a hexadecimal value; a string with
|
|
|
|
a leading 0 and no <<x>> is treated as octal; all other strings are
|
|
|
|
treated as decimal. If <[base]> is between 2 and 36, it is used as the
|
|
|
|
conversion radix, as described above. If the subject string begins with
|
|
|
|
a minus sign, the value is negated. Finally, a pointer to the first
|
|
|
|
character past the converted subject string is stored in <[ptr]>, if
|
|
|
|
<[ptr]> is not <<NULL>>.
|
|
|
|
|
|
|
|
If the subject string is empty (or not in acceptable form), no conversion
|
|
|
|
is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
|
|
|
|
not <<NULL>>).
|
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
<<strtol_l>> is like <<strtol>> but performs the conversion based on the
|
|
|
|
locale specified by the locale object locale. If <[locale]> is
|
|
|
|
LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
The alternate function <<_strtol_r>> is a reentrant version. The
|
|
|
|
extra argument <[reent]> is a pointer to a reentrancy structure.
|
|
|
|
|
|
|
|
RETURNS
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
<<strtol>>, <<strtol_l>> return the converted value, if any. If no
|
|
|
|
conversion was made, 0 is returned.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
<<strtol>>, <<strtol_l>> return <<LONG_MAX>> or <<LONG_MIN>> if the
|
|
|
|
magnitude of the converted value is too large, and sets <<errno>>
|
|
|
|
to <<ERANGE>>.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<strtol>> is ANSI.
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
<<strtol_l>> is a GNU extension.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
No supporting OS subroutines are required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2020-01-29 02:54:53 +08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2000-02-18 03:39:52 +08:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
#define _GNU_SOURCE
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <_ansi.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <reent.h>
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
#include "../locale/setlocale.h"
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a string to a long integer.
|
|
|
|
*/
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
static long
|
|
|
|
_strtol_l (struct _reent *rptr, const char *__restrict nptr,
|
|
|
|
char **__restrict endptr, int base, locale_t loc)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2010-10-28 03:06:10 +08:00
|
|
|
register const unsigned char *s = (const unsigned char *)nptr;
|
2000-02-18 03:39:52 +08:00
|
|
|
register unsigned long acc;
|
|
|
|
register int c;
|
|
|
|
register unsigned long cutoff;
|
|
|
|
register int neg = 0, any, cutlim;
|
|
|
|
|
2019-12-17 05:55:30 +08:00
|
|
|
if (base < 0 || base == 1 || base > 36) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
* Skip white space and pick up leading +/- sign if any.
|
|
|
|
* If base is 0, allow 0x for hex and 0 for octal, else
|
|
|
|
* assume decimal; if base is already 16, allow 0x.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
c = *s++;
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
} while (isspace_l(c, loc));
|
2000-02-18 03:39:52 +08:00
|
|
|
if (c == '-') {
|
|
|
|
neg = 1;
|
|
|
|
c = *s++;
|
|
|
|
} else if (c == '+')
|
|
|
|
c = *s++;
|
|
|
|
if ((base == 0 || base == 16) &&
|
|
|
|
c == '0' && (*s == 'x' || *s == 'X')) {
|
|
|
|
c = s[1];
|
|
|
|
s += 2;
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
if (base == 0)
|
|
|
|
base = c == '0' ? 8 : 10;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the cutoff value between legal numbers and illegal
|
|
|
|
* numbers. That is the largest legal value, divided by the
|
|
|
|
* base. An input number that is greater than this value, if
|
|
|
|
* followed by a legal input character, is too big. One that
|
|
|
|
* is equal to this value may be valid or not; the limit
|
|
|
|
* between valid and invalid numbers is then based on the last
|
|
|
|
* digit. For instance, if the range for longs is
|
|
|
|
* [-2147483648..2147483647] and the input base is 10,
|
|
|
|
* cutoff will be set to 214748364 and cutlim to either
|
|
|
|
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
|
|
|
* a value > 214748364, or equal but the next digit is > 7 (or 8),
|
|
|
|
* the number is too big, and we will return a range error.
|
|
|
|
*
|
|
|
|
* Set any if any `digits' consumed; make it negative to indicate
|
|
|
|
* overflow.
|
|
|
|
*/
|
|
|
|
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
|
|
|
cutlim = cutoff % (unsigned long)base;
|
|
|
|
cutoff /= (unsigned long)base;
|
|
|
|
for (acc = 0, any = 0;; c = *s++) {
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
if (c >= '0' && c <= '9')
|
2000-02-18 03:39:52 +08:00
|
|
|
c -= '0';
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
else if (c >= 'A' && c <= 'Z')
|
|
|
|
c -= 'A' - 10;
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
c -= 'a' - 10;
|
2000-02-18 03:39:52 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (c >= base)
|
|
|
|
break;
|
2019-12-17 05:55:30 +08:00
|
|
|
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
|
2000-02-18 03:39:52 +08:00
|
|
|
any = -1;
|
2019-12-17 05:55:30 +08:00
|
|
|
} else {
|
2000-02-18 03:39:52 +08:00
|
|
|
any = 1;
|
|
|
|
acc *= base;
|
|
|
|
acc += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (any < 0) {
|
|
|
|
acc = neg ? LONG_MIN : LONG_MAX;
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(rptr) = ERANGE;
|
2000-02-18 03:39:52 +08:00
|
|
|
} else if (neg)
|
|
|
|
acc = -acc;
|
|
|
|
if (endptr != 0)
|
2010-10-28 03:06:10 +08:00
|
|
|
*endptr = (char *) (any ? (char *)s - 1 : nptr);
|
2000-02-18 03:39:52 +08:00
|
|
|
return (acc);
|
|
|
|
}
|
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
long
|
2017-12-04 11:43:30 +08:00
|
|
|
_strtol_r (struct _reent *rptr,
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *__restrict nptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char **__restrict endptr,
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
int base)
|
|
|
|
{
|
|
|
|
return _strtol_l (rptr, nptr, endptr, base, __get_current_locale ());
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
long
|
|
|
|
strtol_l (const char *__restrict s, char **__restrict ptr, int base,
|
|
|
|
locale_t loc)
|
|
|
|
{
|
|
|
|
return _strtol_l (_REENT, s, ptr, base, loc);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
long
|
2017-12-04 11:43:30 +08:00
|
|
|
strtol (const char *__restrict s,
|
2017-12-04 09:31:41 +08:00
|
|
|
char **__restrict ptr,
|
2000-02-18 03:39:52 +08:00
|
|
|
int base)
|
|
|
|
{
|
Implement strto[dflu]_l/wcsto[dflu]_l
Implement GNU extensions strtod_l, strtof_l, strtol_l, strtold_l, strtoll_l,
strtoul_l, strtoull_l, wcstod_l, wcstof_l, wcstol_l, wcstold_l, wcstoll_l,
wcstoul_l, wcstoull_l.
Export from Cygwin, fix posix.xml.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2016-08-10 22:30:46 +08:00
|
|
|
return _strtol_l (_REENT, s, ptr, base, __get_current_locale ());
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|