1 |
27 |
unneback |
//===========================================================================
|
2 |
|
|
//
|
3 |
|
|
// strtol.cxx
|
4 |
|
|
//
|
5 |
|
|
// ISO standard string to long int conversion function defined in
|
6 |
|
|
// section 7.10.1.5 of the standard
|
7 |
|
|
//
|
8 |
|
|
//===========================================================================
|
9 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
10 |
|
|
// -------------------------------------------
|
11 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
12 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
13 |
|
|
//
|
14 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
15 |
|
|
// the terms of the GNU General Public License as published by the Free
|
16 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
19 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License along
|
24 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
28 |
|
|
// or inline functions from this file, or you compile this file and link it
|
29 |
|
|
// with other works to produce a work based on this file, this file does not
|
30 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
31 |
|
|
// License. However the source code for this file must still be made available
|
32 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
33 |
|
|
//
|
34 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
35 |
|
|
// this file might be covered by the GNU General Public License.
|
36 |
|
|
//
|
37 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
38 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
39 |
|
|
// -------------------------------------------
|
40 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
41 |
|
|
//===========================================================================
|
42 |
|
|
//#####DESCRIPTIONBEGIN####
|
43 |
|
|
//
|
44 |
|
|
// Author(s): jlarmour
|
45 |
|
|
// Contributors:
|
46 |
|
|
// Date: 2000-04-30
|
47 |
|
|
// Purpose:
|
48 |
|
|
// Description:
|
49 |
|
|
// Usage:
|
50 |
|
|
//
|
51 |
|
|
//####DESCRIPTIONEND####
|
52 |
|
|
//
|
53 |
|
|
//===========================================================================
|
54 |
|
|
//
|
55 |
|
|
// This code is based on original code with the following copyright:
|
56 |
|
|
//
|
57 |
|
|
/*-
|
58 |
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
59 |
|
|
* All rights reserved.
|
60 |
|
|
*
|
61 |
|
|
* Redistribution and use in source and binary forms, with or without
|
62 |
|
|
* modification, are permitted provided that the following conditions
|
63 |
|
|
* are met:
|
64 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
65 |
|
|
* notice, this list of conditions and the following disclaimer.
|
66 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
67 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
68 |
|
|
* documentation and/or other materials provided with the distribution.
|
69 |
|
|
* 3. Neither the name of the University nor the names of its contributors
|
70 |
|
|
* may be used to endorse or promote products derived from this software
|
71 |
|
|
* without specific prior written permission.
|
72 |
|
|
*
|
73 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
74 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
75 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
76 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
77 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
78 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
79 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
80 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
81 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
82 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
83 |
|
|
* SUCH DAMAGE.
|
84 |
|
|
*/
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
// CONFIGURATION
|
88 |
|
|
|
89 |
|
|
#include <pkgconf/libc_stdlib.h> // Configuration header
|
90 |
|
|
|
91 |
|
|
// INCLUDES
|
92 |
|
|
|
93 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions and support
|
94 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
95 |
|
|
#include <cyg/infra/cyg_ass.h> // Assertion support
|
96 |
|
|
#include <limits.h> // Definition of LONG_MIN and LONG_MAX
|
97 |
|
|
#include <ctype.h> // Definition of many ctype functions
|
98 |
|
|
#include <errno.h> // Error code definitions
|
99 |
|
|
#include <stdlib.h> // Header for all stdlib functions
|
100 |
|
|
// (like this one)
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
// FUNCTIONS
|
104 |
|
|
|
105 |
|
|
//
|
106 |
|
|
// Convert a string to a long integer.
|
107 |
|
|
//
|
108 |
|
|
// Ignores `locale' stuff. Assumes that the upper and lower case
|
109 |
|
|
// alphabets and digits are each contiguous.
|
110 |
|
|
//
|
111 |
|
|
|
112 |
|
|
long
|
113 |
|
|
strtol( const char *nptr, char **endptr, int base )
|
114 |
|
|
{
|
115 |
|
|
const char *s = nptr;
|
116 |
|
|
unsigned long acc;
|
117 |
|
|
int c;
|
118 |
|
|
unsigned long cutoff;
|
119 |
|
|
int neg = 0, any, cutlim;
|
120 |
|
|
|
121 |
|
|
CYG_REPORT_FUNCNAMETYPE( "strtol", "returning long %d" );
|
122 |
|
|
CYG_REPORT_FUNCARG3( "nptr=%08x, endptr=%08x, base=%d",
|
123 |
|
|
nptr, endptr, base );
|
124 |
|
|
CYG_CHECK_DATA_PTR( nptr, "nptr is not a valid pointer!" );
|
125 |
|
|
|
126 |
|
|
if (endptr != NULL)
|
127 |
|
|
CYG_CHECK_DATA_PTR( endptr, "endptr is not a valid pointer!" );
|
128 |
|
|
|
129 |
|
|
//
|
130 |
|
|
// Skip white space and pick up leading +/- sign if any.
|
131 |
|
|
// If base is 0, allow 0x for hex and 0 for octal, else
|
132 |
|
|
// assume decimal; if base is already 16, allow 0x.
|
133 |
|
|
//
|
134 |
|
|
|
135 |
|
|
do {
|
136 |
|
|
c = *s++;
|
137 |
|
|
} while (isspace(c));
|
138 |
|
|
if (c == '-') {
|
139 |
|
|
neg = 1;
|
140 |
|
|
c = *s++;
|
141 |
|
|
} else if (c == '+')
|
142 |
|
|
c = *s++;
|
143 |
|
|
if ((base == 0 || base == 16) &&
|
144 |
|
|
c == '0' && (*s == 'x' || *s == 'X')) {
|
145 |
|
|
c = s[1];
|
146 |
|
|
s += 2;
|
147 |
|
|
base = 16;
|
148 |
|
|
}
|
149 |
|
|
if (base == 0)
|
150 |
|
|
base = c == '0' ? 8 : 10;
|
151 |
|
|
|
152 |
|
|
//
|
153 |
|
|
// Compute the cutoff value between legal numbers and illegal
|
154 |
|
|
// numbers. That is the largest legal value, divided by the
|
155 |
|
|
// base. An input number that is greater than this value, if
|
156 |
|
|
// followed by a legal input character, is too big. One that
|
157 |
|
|
// is equal to this value may be valid or not; the limit
|
158 |
|
|
// between valid and invalid numbers is then based on the last
|
159 |
|
|
// digit. For instance, if the range for longs is
|
160 |
|
|
// [-2147483648..2147483647] and the input base is 10,
|
161 |
|
|
// cutoff will be set to 214748364 and cutlim to either
|
162 |
|
|
// 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
163 |
|
|
// a value > 214748364, or equal but the next digit is > 7 (or 8),
|
164 |
|
|
// the number is too big, and we will return a range error.
|
165 |
|
|
//
|
166 |
|
|
// Set any if any `digits' consumed; make it negative to indicate
|
167 |
|
|
// overflow.
|
168 |
|
|
//
|
169 |
|
|
|
170 |
|
|
cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
|
171 |
|
|
cutlim = cutoff % (unsigned long)base;
|
172 |
|
|
cutoff /= (unsigned long)base;
|
173 |
|
|
for (acc = 0, any = 0;; c = *s++) {
|
174 |
|
|
if (isdigit(c))
|
175 |
|
|
c -= '0';
|
176 |
|
|
else if (isalpha(c))
|
177 |
|
|
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
|
178 |
|
|
else
|
179 |
|
|
break;
|
180 |
|
|
if (c >= base)
|
181 |
|
|
break;
|
182 |
|
|
if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
|
183 |
|
|
any = -1;
|
184 |
|
|
else {
|
185 |
|
|
any = 1;
|
186 |
|
|
acc *= base;
|
187 |
|
|
acc += c;
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
if (any < 0) {
|
191 |
|
|
acc = neg ? LONG_MIN : LONG_MAX;
|
192 |
|
|
errno = ERANGE;
|
193 |
|
|
} else if (neg)
|
194 |
|
|
acc = -acc;
|
195 |
|
|
if (endptr != 0)
|
196 |
|
|
*endptr = (char *) (any ? s - 1 : nptr);
|
197 |
|
|
|
198 |
|
|
CYG_REPORT_RETVAL ( acc );
|
199 |
|
|
|
200 |
|
|
return acc;
|
201 |
|
|
} // strtol()
|
202 |
|
|
|
203 |
|
|
// EOF strtol.cxx
|