source: trunk/libs/newlib/src/newlib/libc/stdlib/strtoul.c @ 471

Last change on this file since 471 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 7.2 KB
Line 
1/*
2FUNCTION
3        <<strtoul>>, <<strtoul_l>>---string to unsigned long
4
5INDEX
6        strtoul
7
8INDEX
9        strtoul_l
10
11INDEX
12        _strtoul_r
13
14SYNOPSIS
15        #include <stdlib.h>
16        unsigned long strtoul(const char *restrict <[s]>,
17                              char **restrict <[ptr]>, int <[base]>);
18
19        #include <stdlib.h>
20        unsigned long strtoul_l(const char *restrict <[s]>,
21                                char **restrict <[ptr]>, int <[base]>,
22                                locale_t <[locale]>);
23
24        unsigned long _strtoul_r(void *<[reent]>, const char *restrict <[s]>,
25                                 char **restrict <[ptr]>, int <[base]>);
26
27DESCRIPTION
28The function <<strtoul>> converts the string <<*<[s]>>> to
29an <<unsigned long>>. First, it breaks down the string into three parts:
30leading whitespace, which is ignored; a subject string consisting
31of the digits meaningful in the radix specified by <[base]>
32(for example, <<0>> through <<7>> if the value of <[base]> is 8);
33and a trailing portion consisting of one or more unparseable characters,
34which always includes the terminating null character. Then, it attempts
35to convert the subject string into an unsigned long integer, and returns the
36result.
37
38If the value of <[base]> is zero, the subject string is expected to look
39like a normal C integer constant (save that no optional sign is permitted):
40a possible <<0x>> indicating hexadecimal radix, and a number.
41If <[base]> is between 2 and 36, the expected form of the subject is a
42sequence of digits (which may include letters, depending on the
43base) representing an integer in the radix specified by <[base]>.
44The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
4510 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
46
47The subject sequence is the longest initial sequence of the input
48string that has the expected form, starting with the first
49non-whitespace character.  If the string is empty or consists entirely
50of whitespace, or if the first non-whitespace character is not a
51permissible digit, the subject string is empty.
52
53If the subject string is acceptable, and the value of <[base]> is zero,
54<<strtoul>> attempts to determine the radix from the input string. A
55string with a leading <<0x>> is treated as a hexadecimal value; a string with
56a leading <<0>> and no <<x>> is treated as octal; all other strings are
57treated as decimal. If <[base]> is between 2 and 36, it is used as the
58conversion radix, as described above. Finally, a pointer to the first
59character past the converted subject string is stored in <[ptr]>, if
60<[ptr]> is not <<NULL>>.
61
62If the subject string is empty (that is, if <<*>><[s]> does not start
63with a substring in acceptable form), no conversion
64is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
65not <<NULL>>).
66
67<<strtoul_l>> is like <<strtoul>> but performs the conversion based on the
68locale specified by the locale object locale.  If <[locale]> is
69LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
70
71The alternate function <<_strtoul_r>> is a reentrant version.  The
72extra argument <[reent]> is a pointer to a reentrancy structure.
73
74RETURNS
75<<strtoul>>, <<strtoul_l>> return the converted value, if any. If no
76conversion was made, <<0>> is returned.
77
78<<strtoul>>, <<strtoul_l>> return <<ULONG_MAX>> if the magnitude of the
79converted value is too large, and sets <<errno>> to <<ERANGE>>.
80
81PORTABILITY
82<<strtoul>> is ANSI.
83<<strtoul_l>> is a GNU extension.
84
85<<strtoul>> requires no supporting OS subroutines.
86*/
87
88/*
89 * Copyright (c) 1990 Regents of the University of California.
90 * All rights reserved.
91 *
92 * Redistribution and use in source and binary forms, with or without
93 * modification, are permitted provided that the following conditions
94 * are met:
95 * 1. Redistributions of source code must retain the above copyright
96 *    notice, this list of conditions and the following disclaimer.
97 * 2. Redistributions in binary form must reproduce the above copyright
98 *    notice, this list of conditions and the following disclaimer in the
99 *    documentation and/or other materials provided with the distribution.
100 * 3. All advertising materials mentioning features or use of this software
101 *    must display the following acknowledgement:
102 *      This product includes software developed by the University of
103 *      California, Berkeley and its contributors.
104 * 4. Neither the name of the University nor the names of its contributors
105 *    may be used to endorse or promote products derived from this software
106 *    without specific prior written permission.
107 *
108 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
109 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
110 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
111 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
112 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
113 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
114 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
116 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
117 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
118 * SUCH DAMAGE.
119 */
120
121#define _GNU_SOURCE
122#include <_ansi.h>
123#include <limits.h>
124#include <ctype.h>
125#include <errno.h>
126#include <stdlib.h>
127#include <reent.h>
128#include "../locale/setlocale.h"
129
130/*
131 * Convert a string to an unsigned long integer.
132 */
133static unsigned long
134_strtoul_l (struct _reent *rptr, const char *__restrict nptr,
135            char **__restrict endptr, int base, locale_t loc)
136{
137        register const unsigned char *s = (const unsigned char *)nptr;
138        register unsigned long acc;
139        register int c;
140        register unsigned long cutoff;
141        register int neg = 0, any, cutlim;
142
143        /*
144         * See strtol for comments as to the logic used.
145         */
146        do {
147                c = *s++;
148        } while (isspace_l(c, loc));
149        if (c == '-') {
150                neg = 1;
151                c = *s++;
152        } else if (c == '+')
153                c = *s++;
154        if ((base == 0 || base == 16) &&
155            c == '0' && (*s == 'x' || *s == 'X')) {
156                c = s[1];
157                s += 2;
158                base = 16;
159        }
160        if (base == 0)
161                base = c == '0' ? 8 : 10;
162        cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
163        cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
164        for (acc = 0, any = 0;; c = *s++) {
165                if (c >= '0' && c <= '9')
166                        c -= '0';
167                else if (c >= 'A' && c <= 'Z')
168                        c -= 'A' - 10;
169                else if (c >= 'a' && c <= 'z')
170                        c -= 'a' - 10;
171                else
172                        break;
173                if (c >= base)
174                        break;
175               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
176                        any = -1;
177                else {
178                        any = 1;
179                        acc *= base;
180                        acc += c;
181                }
182        }
183        if (any < 0) {
184                acc = ULONG_MAX;
185                rptr->_errno = ERANGE;
186        } else if (neg)
187                acc = -acc;
188        if (endptr != 0)
189                *endptr = (char *) (any ? (char *)s - 1 : nptr);
190        return (acc);
191}
192
193unsigned long
194_strtoul_r (struct _reent *rptr,
195        const char *__restrict nptr,
196        char **__restrict endptr,
197        int base)
198{
199  return _strtoul_l (rptr, nptr, endptr, base, __get_current_locale ());
200}
201
202#ifndef _REENT_ONLY
203
204unsigned long
205strtoul_l (const char *__restrict s, char **__restrict ptr, int base,
206           locale_t loc)
207{
208        return _strtoul_l (_REENT, s, ptr, base, loc);
209}
210
211unsigned long
212strtoul (const char *__restrict s,
213        char **__restrict ptr,
214        int base)
215{
216        return _strtoul_l (_REENT, s, ptr, base, __get_current_locale ());
217}
218
219#endif
Note: See TracBrowser for help on using the repository browser.