|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // strtoul.cxx |
|
|
4 // |
|
|
5 // ANSI standard string to long int conversion function defined in |
|
|
6 // section 7.10.1.6 of the standard |
|
|
7 // |
|
|
8 //=========================================================================== |
|
|
9 //####COPYRIGHTBEGIN#### |
|
|
10 // |
|
|
11 // ------------------------------------------- |
|
|
12 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
13 // Version 1.0 (the "License"); you may not use this file except in |
|
|
14 // compliance with the License. You may obtain a copy of the License at |
|
|
15 // http://sourceware.cygnus.com/ecos |
|
|
16 // |
|
|
17 // Software distributed under the License is distributed on an "AS IS" |
|
|
18 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
19 // License for the specific language governing rights and limitations under |
|
|
20 // the License. |
|
|
21 // |
|
|
22 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
23 // September 30, 1998. |
|
|
24 // |
|
|
25 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
26 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
27 // ------------------------------------------- |
|
|
28 // |
|
|
29 //####COPYRIGHTEND#### |
|
|
30 //=========================================================================== |
|
|
31 //#####DESCRIPTIONBEGIN#### |
|
|
32 // |
|
|
33 // Author(s): jlarmour |
|
|
34 // Contributors: jlarmour@cygnus.co.uk |
|
|
35 // Date: 1998-02-13 |
|
|
36 // Purpose: |
|
|
37 // Description: |
|
|
38 // Usage: |
|
|
39 // |
|
|
40 //####DESCRIPTIONEND#### |
|
|
41 // |
|
|
42 //=========================================================================== |
|
|
43 // |
|
|
44 // This code is based on original code with the following copyright: |
|
|
45 // |
|
|
46 /* |
|
|
47 * Copyright (c) 1990 Regents of the University of California. |
|
|
48 * All rights reserved. |
|
|
49 * |
|
|
50 * Redistribution and use in source and binary forms, with or without |
|
|
51 * modification, are permitted provided that the following conditions |
|
|
52 * are met: |
|
|
53 * 1. Redistributions of source code must retain the above copyright |
|
|
54 * notice, this list of conditions and the following disclaimer. |
|
|
55 * 2. Redistributions in binary form must reproduce the above copyright |
|
|
56 * notice, this list of conditions and the following disclaimer in the |
|
|
57 * documentation and/or other materials provided with the distribution. |
|
|
58 * 3. All advertising materials mentioning features or use of this software |
|
|
59 * must display the following acknowledgement: |
|
|
60 * This product includes software developed by the University of |
|
|
61 * California, Berkeley and its contributors. |
|
|
62 * 4. Neither the name of the University nor the names of its contributors |
|
|
63 * may be used to endorse or promote products derived from this software |
|
|
64 * without specific prior written permission. |
|
|
65 * |
|
|
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
|
|
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|
|
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
|
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
|
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
76 * SUCH DAMAGE. |
|
|
77 */ |
|
|
78 |
|
|
79 |
|
|
80 // CONFIGURATION |
|
|
81 |
|
|
82 #include <pkgconf/libc.h> // Configuration header |
|
|
83 |
|
|
84 // Include the C library? |
|
|
85 #ifdef CYGPKG_LIBC |
|
|
86 |
|
|
87 // INCLUDES |
|
|
88 |
|
|
89 #include <cyg/infra/cyg_type.h> // Common type definitions and support |
|
|
90 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
91 #include <cyg/infra/cyg_ass.h> // Assertion support |
|
|
92 #include <limits.h> // Definition of ULONG_MAX |
|
|
93 #include <ctype.h> // Definition of many ctype functions |
|
|
94 #include <errno.h> // Error code definitions |
|
|
95 #include <stdlib.h> // Header for all stdlib functions |
|
|
96 // (like this one) |
|
|
97 #include "clibincl/stdlibsupp.hxx" // Support for stdlib functions |
|
|
98 |
|
|
99 |
|
|
100 // EXPORTED SYMBOLS |
|
|
101 |
|
|
102 externC unsigned long |
|
|
103 strtoul( const char *nptr, char **endptr, int base ) \ |
|
|
104 CYGPRI_LIBC_WEAK_ALIAS("_strtoul"); |
|
|
105 |
|
|
106 // FUNCTIONS |
|
|
107 |
|
|
108 // |
|
|
109 // Convert a string to an unsigned long integer. |
|
|
110 // |
|
|
111 // Ignores `locale' stuff. Assumes that the upper and lower case |
|
|
112 // alphabets and digits are each contiguous. |
|
|
113 // |
|
|
114 |
|
|
115 unsigned long |
|
|
116 _strtoul( const char *nptr, char **endptr, int base ) |
|
|
117 { |
|
|
118 const char *s = nptr; |
|
|
119 unsigned long acc; |
|
|
120 int c; |
|
|
121 unsigned long cutoff; |
|
|
122 int neg = 0, any, cutlim; |
|
|
123 |
|
|
124 CYG_REPORT_FUNCNAMETYPE( "_strtoul", "returning long %d" ); |
|
|
125 CYG_REPORT_FUNCARG3( "nptr=%08x, endptr=%08x, base=%d", |
|
|
126 nptr, endptr, base ); |
|
|
127 CYG_CHECK_DATA_PTR( nptr, "nptr is not a valid pointer!" ); |
|
|
128 |
|
|
129 if (endptr != NULL) |
|
|
130 CYG_CHECK_DATA_PTR( endptr, "endptr is not a valid pointer!" ); |
|
|
131 // |
|
|
132 // See strtol for comments as to the logic used. |
|
|
133 // |
|
|
134 do { |
|
|
135 c = *s++; |
|
|
136 } while (isspace(c)); |
|
|
137 if (c == '-') { |
|
|
138 neg = 1; |
|
|
139 c = *s++; |
|
|
140 } else if (c == '+') |
|
|
141 c = *s++; |
|
|
142 if ((base == 0 || base == 16) && |
|
|
143 c == '0' && (*s == 'x' || *s == 'X')) { |
|
|
144 c = s[1]; |
|
|
145 s += 2; |
|
|
146 base = 16; |
|
|
147 } |
|
|
148 if (base == 0) |
|
|
149 base = c == '0' ? 8 : 10; |
|
|
150 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; |
|
|
151 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; |
|
|
152 for (acc = 0, any = 0;; c = *s++) { |
|
|
153 if (isdigit(c)) |
|
|
154 c -= '0'; |
|
|
155 else if (isalpha(c)) |
|
|
156 c -= isupper(c) ? 'A' - 10 : 'a' - 10; |
|
|
157 else |
|
|
158 break; |
|
|
159 if (c >= base) |
|
|
160 break; |
|
|
161 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) |
|
|
162 any = -1; |
|
|
163 else { |
|
|
164 any = 1; |
|
|
165 acc *= base; |
|
|
166 acc += c; |
|
|
167 } |
|
|
168 } |
|
|
169 if (any < 0) { |
|
|
170 acc = ULONG_MAX; |
|
|
171 errno = ERANGE; |
|
|
172 } else if (neg) |
|
|
173 acc = -acc; |
|
|
174 if (endptr != 0) |
|
|
175 *endptr = (char *) (any ? s - 1 : nptr); |
|
|
176 |
|
|
177 CYG_REPORT_RETVAL( acc ); |
|
|
178 |
|
|
179 return acc; |
|
|
180 } // _strtoul() |
|
|
181 |
|
|
182 #endif // ifdef CYGPKG_LIBC |
|
|
183 |
|
|
184 // EOF strtoul.cxx |