OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdlib/] [strtoul.c] - Blame information for rev 300

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
2
FUNCTION
3
        <<strtoul>>---string to unsigned long
4
 
5
INDEX
6
        strtoul
7
INDEX
8
        _strtoul_r
9
 
10
ANSI_SYNOPSIS
11
        #include <stdlib.h>
12
        unsigned long strtoul(const char *<[s]>, char **<[ptr]>,
13
                              int <[base]>);
14
 
15
        unsigned long _strtoul_r(void *<[reent]>, const char *<[s]>,
16
                              char **<[ptr]>, int <[base]>);
17
 
18
TRAD_SYNOPSIS
19
        #include <stdlib.h>
20
        unsigned long strtoul(<[s]>, <[ptr]>, <[base]>)
21
        char *<[s]>;
22
        char **<[ptr]>;
23
        int <[base]>;
24
 
25
        unsigned long _strtoul_r(<[reent]>, <[s]>, <[ptr]>, <[base]>)
26
        char *<[reent]>;
27
        char *<[s]>;
28
        char **<[ptr]>;
29
        int <[base]>;
30
 
31
DESCRIPTION
32
The function <<strtoul>> converts the string <<*<[s]>>> to
33
an <<unsigned long>>. First, it breaks down the string into three parts:
34
leading whitespace, which is ignored; a subject string consisting
35
of the digits meaningful in the radix specified by <[base]>
36
(for example, <<0>> through <<7>> if the value of <[base]> is 8);
37
and a trailing portion consisting of one or more unparseable characters,
38
which always includes the terminating null character. Then, it attempts
39
to convert the subject string into an unsigned long integer, and returns the
40
result.
41
 
42
If the value of <[base]> is zero, the subject string is expected to look
43
like a normal C integer constant (save that no optional sign is permitted):
44
a possible <<0x>> indicating hexadecimal radix, and a number.
45
If <[base]> is between 2 and 36, the expected form of the subject is a
46
sequence of digits (which may include letters, depending on the
47
base) representing an integer in the radix specified by <[base]>.
48
The letters <<a>>--<<z>> (or <<A>>--<<Z>>) are used as digits valued from
49
10 to 35. If <[base]> is 16, a leading <<0x>> is permitted.
50
 
51
The subject sequence is the longest initial sequence of the input
52
string that has the expected form, starting with the first
53
non-whitespace character.  If the string is empty or consists entirely
54
of whitespace, or if the first non-whitespace character is not a
55
permissible digit, the subject string is empty.
56
 
57
If the subject string is acceptable, and the value of <[base]> is zero,
58
<<strtoul>> attempts to determine the radix from the input string. A
59
string with a leading <<0x>> is treated as a hexadecimal value; a string with
60
a leading <<0>> and no <<x>> is treated as octal; all other strings are
61
treated as decimal. If <[base]> is between 2 and 36, it is used as the
62
conversion radix, as described above. Finally, a pointer to the first
63
character past the converted subject string is stored in <[ptr]>, if
64
<[ptr]> is not <<NULL>>.
65
 
66
If the subject string is empty (that is, if <<*>><[s]> does not start
67
with a substring in acceptable form), no conversion
68
is performed and the value of <[s]> is stored in <[ptr]> (if <[ptr]> is
69
not <<NULL>>).
70
 
71
The alternate function <<_strtoul_r>> is a reentrant version.  The
72
extra argument <[reent]> is a pointer to a reentrancy structure.
73
 
74
 
75
RETURNS
76
<<strtoul>> returns the converted value, if any. If no conversion was
77
made, <<0>> is returned.
78
 
79
<<strtoul>> returns <<ULONG_MAX>> if the magnitude of the converted
80
value is too large, and sets <<errno>> to <<ERANGE>>.
81
 
82
PORTABILITY
83
<<strtoul>> is ANSI.
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
#include <_ansi.h>
122
#include <limits.h>
123
#include <ctype.h>
124
#include <errno.h>
125
#include <stdlib.h>
126
#include <reent.h>
127
 
128
/*
129
 * Convert a string to an unsigned long integer.
130
 *
131
 * Ignores `locale' stuff.  Assumes that the upper and lower case
132
 * alphabets and digits are each contiguous.
133
 */
134
unsigned long
135
_DEFUN (_strtoul_r, (rptr, nptr, endptr, base),
136
        struct _reent *rptr _AND
137
        _CONST char *nptr _AND
138
        char **endptr _AND
139
        int base)
140
{
141
        register const char *s = nptr;
142
        register unsigned long acc;
143
        register int c;
144
        register unsigned long cutoff;
145
        register int neg = 0, any, cutlim;
146
 
147
        /*
148
         * See strtol for comments as to the logic used.
149
         */
150
        do {
151
                c = *s++;
152
        } while (isspace(c));
153
        if (c == '-') {
154
                neg = 1;
155
                c = *s++;
156
        } else if (c == '+')
157
                c = *s++;
158
        if ((base == 0 || base == 16) &&
159
            c == '0' && (*s == 'x' || *s == 'X')) {
160
                c = s[1];
161
                s += 2;
162
                base = 16;
163
        }
164
        if (base == 0)
165
                base = c == '0' ? 8 : 10;
166
        cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
167
        cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
168
        for (acc = 0, any = 0;; c = *s++) {
169
                if (isdigit(c))
170
                        c -= '0';
171
                else if (isalpha(c))
172
                        c -= isupper(c) ? 'A' - 10 : 'a' - 10;
173
                else
174
                        break;
175
                if (c >= base)
176
                        break;
177
               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
178
                        any = -1;
179
                else {
180
                        any = 1;
181
                        acc *= base;
182
                        acc += c;
183
                }
184
        }
185
        if (any < 0) {
186
                acc = ULONG_MAX;
187
                rptr->_errno = ERANGE;
188
        } else if (neg)
189
                acc = -acc;
190
        if (endptr != 0)
191
                *endptr = (char *) (any ? s - 1 : nptr);
192
        return (acc);
193
}
194
 
195
#ifndef _REENT_ONLY
196
 
197
unsigned long
198
_DEFUN (strtoul, (s, ptr, base),
199
        _CONST char *s _AND
200
        char **ptr _AND
201
        int base)
202
{
203
        return _strtoul_r (_REENT, s, ptr, base);
204
}
205
 
206
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.