sl@0: /* sl@0: FUNCTION sl@0: <>---string to long sl@0: <>---string to unsigned long sl@0: sl@0: INDEX sl@0: strtol sl@0: INDEX sl@0: _strtol_r sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: long strtol(const char *<[s]>, char **<[ptr]>,int <[base]>); sl@0: unsigned long strtoul(const char *<[s]>, char **<[ptr]>, sl@0: int <[base]>); sl@0: sl@0: DESCRIPTION sl@0: The function <> converts the string <<*<[s]>>> to sl@0: a <>. First, it breaks down the string into three parts: sl@0: leading whitespace, which is ignored; a subject string consisting sl@0: of characters resembling an integer in the radix specified by <[base]>; sl@0: and a trailing portion consisting of zero or more unparseable characters, sl@0: and always including the terminating null character. Then, it attempts sl@0: to convert the subject string into a <> and returns the sl@0: result. sl@0: sl@0: If the value of <[base]> is 0, the subject string is expected to look sl@0: like a normal C integer constant: an optional sign, a possible `<<0x>>' sl@0: indicating a hexadecimal base, and a number. If <[base]> is between sl@0: 2 and 36, the expected form of the subject is a sequence of letters sl@0: and digits representing an integer in the radix specified by <[base]>, sl@0: with an optional plus or minus sign. The letters <>--<> (or, sl@0: equivalently, <>--<>) are used to signify values from 10 to 35; sl@0: only letters whose ascribed values are less than <[base]> are sl@0: permitted. If <[base]> is 16, a leading <<0x>> is permitted. sl@0: sl@0: The subject sequence is the longest initial sequence of the input sl@0: string that has the expected form, starting with the first sl@0: non-whitespace character. If the string is empty or consists entirely sl@0: of whitespace, or if the first non-whitespace character is not a sl@0: permissible letter or digit, the subject string is empty. sl@0: sl@0: If the subject string is acceptable, and the value of <[base]> is zero, sl@0: <> attempts to determine the radix from the input string. A sl@0: string with a leading <<0x>> is treated as a hexadecimal value; a string with sl@0: a leading 0 and no <> is treated as octal; all other strings are sl@0: treated as decimal. If <[base]> is between 2 and 36, it is used as the sl@0: conversion radix, as described above. If the subject string begins with sl@0: a minus sign, the value is negated. Finally, a pointer to the first sl@0: character past the converted subject string is stored in <[ptr]>, if sl@0: <[ptr]> is not <>. sl@0: sl@0: If the subject string is empty (or not in acceptable form), no conversion sl@0: is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is sl@0: not <>). sl@0: sl@0: The alternate function <<_strtol_r>> is a reentrant version. The sl@0: extra argument <[reent]> is a pointer to a reentrancy structure. sl@0: sl@0: The function <> is similar but does not permit an optional sign sl@0: and returns an <>. sl@0: sl@0: RETURNS sl@0: <> returns the converted value, if any. If no conversion was sl@0: made, 0 is returned. sl@0: sl@0: <> returns <> or <> if the magnitude of sl@0: the converted value is too large, and sets <> to <>. sl@0: sl@0: <> returns <> if the magnitude of the converted sl@0: value is too large, and sets <> to <>. sl@0: sl@0: PORTABILITY sl@0: <> and <> are both ANSI. sl@0: sl@0: No supporting OS subroutines are required. sl@0: */ sl@0: sl@0: /*- sl@0: * Copyright (c) 1990 The Regents of the University of California. sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * This product includes software developed by the University of sl@0: * California, Berkeley and its contributors. sl@0: * 4. Neither the name of the University nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: */ sl@0: sl@0: sl@0: #include <_ansi.h> sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* sl@0: * Convert a string to a long integer. sl@0: * sl@0: * Ignores `locale' stuff. Assumes that the upper and lower case sl@0: * alphabets and digits are each contiguous. sl@0: */ sl@0: static unsigned long _do_strtoX (const char *nptr, char **endptr, int base, int issigned) sl@0: { sl@0: register const char *s = nptr; sl@0: register unsigned long acc; sl@0: register int c; sl@0: register unsigned long cutoff; sl@0: register int neg = 0, any, cutlim; sl@0: register const unsigned long long_min = (unsigned long)LONG_MIN; sl@0: sl@0: /* sl@0: * Skip white space and pick up leading +/- sign if any. sl@0: * If base is 0, allow 0x for hex and 0 for octal, else sl@0: * assume decimal; if base is already 16, allow 0x. sl@0: */ sl@0: do { sl@0: c = *s++; sl@0: } while (isspace(c)); sl@0: sl@0: if ((c == '-')||(c == '+')) { sl@0: issigned = 1; sl@0: } sl@0: sl@0: if (issigned) { sl@0: if (c == '-') { sl@0: neg = 1; sl@0: c = *s++; sl@0: } else if (c == '+') sl@0: c = *s++; sl@0: } sl@0: if ((base == 0 || base == 16) && sl@0: c == '0' && (*s == 'x' || *s == 'X')) { sl@0: c = s[1]; sl@0: s += 2; sl@0: base = 16; sl@0: } sl@0: if (base == 0) sl@0: base = c == '0' ? 8 : 10; sl@0: sl@0: /* sl@0: * Compute the cutoff value between legal numbers and illegal sl@0: * numbers. That is the largest legal value, divided by the sl@0: * base. An input number that is greater than this value, if sl@0: * followed by a legal input character, is too big. One that sl@0: * is equal to this value may be valid or not; the limit sl@0: * between valid and invalid numbers is then based on the last sl@0: * digit. For instance, if the range for longs is sl@0: * [-2147483648..2147483647] and the input base is 10, sl@0: * cutoff will be set to 214748364 and cutlim to either sl@0: * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated sl@0: * a value > 214748364, or equal but the next digit is > 7 (or 8), sl@0: * the number is too big, and we will return a range error. sl@0: * sl@0: * Set any if any `digits' consumed; make it negative to indicate sl@0: * overflow. sl@0: */ sl@0: if (issigned) sl@0: cutoff = neg ? long_min : LONG_MAX; sl@0: else sl@0: cutoff = ULONG_MAX; sl@0: cutlim = cutoff % (unsigned long)base; sl@0: cutoff = cutoff / (unsigned long)base; sl@0: for (acc = 0, any = 0;; c = *s++) { sl@0: if (isdigit(c)) sl@0: c -= '0'; sl@0: else if (isalpha(c)) sl@0: c -= isupper(c) ? 'A' - 10 : 'a' - 10; sl@0: else sl@0: break; sl@0: if (c >= base) sl@0: break; sl@0: if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) sl@0: any = -1; sl@0: else { sl@0: any = 1; sl@0: acc *= base; sl@0: acc += c; sl@0: } sl@0: } sl@0: if (any < 0) { sl@0: if (issigned) sl@0: acc = neg ? LONG_MIN : LONG_MAX; sl@0: else sl@0: acc = ULONG_MAX; sl@0: errno = ERANGE; sl@0: } else if (neg) sl@0: acc = (unsigned long)(-(long)acc); sl@0: if (endptr != 0) sl@0: *endptr = (char *) (any ? s - 1 : nptr); sl@0: return (acc); sl@0: } sl@0: sl@0: /** sl@0: Convert string to unsigned long integer. sl@0: Parses string interpreting its content as an integer value sl@0: until a character that can not be interpreted is found, sl@0: and returns an unsigned long value. sl@0: @return The converted unsigned long value from the input string. sl@0: If an error occurs or no conversion can be made 0 is returned. sl@0: @param s String representing an integer number. sl@0: @param ptr Address of a pointer. sl@0: This is filled by the function with the address where scan has ended. sl@0: Serves to determine where there is the first non-numerical character in the string. sl@0: @param base Numeral radix in which the number to be interpreted. sl@0: Must be 0 or be between 2 and 36. If it is 0 the radix of the string is determined sl@0: by the initial characters of the string: sl@0: */ sl@0: EXPORT_C unsigned long strtoul (const char *s, char **ptr, int base) sl@0: { sl@0: return _do_strtoX (s, ptr, base, 0); sl@0: } sl@0: sl@0: /** sl@0: Convert string to long integer. sl@0: @return The converted long int value from the input string. sl@0: If an error occurs or no conversion can be made 0 is returned. sl@0: @param s String representing an integer number. sl@0: @param ptr Address of a pointer. sl@0: This is filled by the function with the address where scan has ended. sl@0: @param base Numeral radix in which the number to be interpreted. sl@0: Must be 0 or be between 2 and 36. If it is 0 the radix of the string sl@0: is determined by the initial characters of the string sl@0: */ sl@0: EXPORT_C long strtol (const char *s, char **ptr, int base) sl@0: { sl@0: return (long)_do_strtoX (s, ptr, base, 1); sl@0: } sl@0: sl@0: /** sl@0: Convert string to integer. sl@0: Parses string interpreting its content as a number and returns an int value. sl@0: @return The converted integer value of the input string. sl@0: On overflow the result is undefined. sl@0: If an error occurs 0 is returned. sl@0: @param s String representing an integer number. sl@0: */ sl@0: EXPORT_C int atoi (const char *s) sl@0: { sl@0: return (int) _do_strtoX (s, NULL, 10, 1); sl@0: }