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