Fix rpm.unregister() Lua extension
[platform/upstream/rpm.git] / misc / strtol.c
1 /* Copyright (C) 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
2
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB.  If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
20 #ifdef _LIBC
21 # define USE_NUMBER_GROUPING
22 # define STDC_HEADERS
23 # define HAVE_LIMITS_H
24 #else
25 #include "system.h"
26 #endif
27
28 #ifndef errno
29 extern int errno;
30 #endif
31
32 #ifdef STDC_HEADERS
33 # include <stddef.h>
34 # include <stdlib.h>
35 #else
36 # ifndef NULL
37 #  define NULL 0
38 # endif
39 #endif
40
41 #ifdef USE_NUMBER_GROUPING
42 # include "../locale/localeinfo.h"
43 #endif
44
45 /* Nonzero if we are defining `strtoul' or `strtouq', operating on
46    unsigned integers.  */
47 #ifndef UNSIGNED
48 # define UNSIGNED 0
49 # define INT LONG int
50 #else
51 # define strtol strtoul
52 # define INT unsigned LONG int
53 #endif
54
55 /* If QUAD is defined, we are defining `strtoq' or `strtouq',
56    operating on `long long int's.  */
57 #ifdef QUAD
58 # if UNSIGNED
59 #  define strtoul strtouq
60 # else
61 #  define strtol strtoq
62 # endif
63 # define LONG long long
64 # undef LONG_MIN
65 # define LONG_MIN LONG_LONG_MIN
66 # undef LONG_MAX
67 # define LONG_MAX LONG_LONG_MAX
68 # undef ULONG_MAX
69 # define ULONG_MAX ULONG_LONG_MAX
70 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
71    /* Work around gcc bug with using this constant.  */
72    static const unsigned long long int maxquad = ULONG_LONG_MAX;
73 #  undef ULONG_MAX
74 #  define ULONG_MAX maxquad
75 # endif
76 #else
77 # define LONG long
78 #endif
79
80 #ifdef __STDC__
81 # define INTERNAL(x) INTERNAL1(x)
82 # define INTERNAL1(x) __##x##_internal
83 #else
84 # define INTERNAL(x) __/**/x/**/_internal
85 #endif
86
87 #ifdef USE_NUMBER_GROUPING
88 /* This file defines a function to check for correct grouping.  */
89 # include "grouping.h"
90 #endif
91
92
93 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
94    If BASE is 0 the base is determined by the presence of a leading
95    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
96    If BASE is < 2 or > 36, it is reset to 10.
97    If ENDPTR is not NULL, a pointer to the character after the last
98    one converted is stored in *ENDPTR.  */
99
100 INT
101 INTERNAL (strtol) (nptr, endptr, base, group)
102      const char *nptr;
103      char **endptr;
104      int base;
105      int group;
106 {
107   int negative;
108   register unsigned LONG int cutoff;
109   register unsigned int cutlim;
110   register unsigned LONG int i;
111   register const char *s;
112   register unsigned char c;
113   const char *save, *end;
114   int overflow;
115
116 #ifdef USE_NUMBER_GROUPING
117   /* The thousands character of the current locale.  */
118   wchar_t thousands;
119   /* The numeric grouping specification of the current locale,
120      in the format described in <locale.h>.  */
121   const char *grouping;
122
123   if (group)
124     {
125       grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
126       if (*grouping <= 0 || *grouping == CHAR_MAX)
127         grouping = NULL;
128       else
129         {
130           /* Figure out the thousands separator character.  */
131           if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
132                       strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
133             thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
134           if (thousands == L'\0')
135             grouping = NULL;
136         }
137     }
138   else
139     grouping = NULL;
140 #endif
141
142   if (base < 0 || base == 1 || base > 36)
143     base = 10;
144
145   save = s = nptr;
146
147   /* Skip white space.  */
148   while (isspace (*s))
149     ++s;
150   if (*s == '\0')
151     goto noconv;
152
153   /* Check for a sign.  */
154   if (*s == '-')
155     {
156       negative = 1;
157       ++s;
158     }
159   else if (*s == '+')
160     {
161       negative = 0;
162       ++s;
163     }
164   else
165     negative = 0;
166
167   if (base == 16 && s[0] == '0' && toupper (s[1]) == 'X')
168     s += 2;
169
170   /* If BASE is zero, figure it out ourselves.  */
171   if (base == 0)
172     if (*s == '0')
173       {
174         if (toupper (s[1]) == 'X')
175           {
176             s += 2;
177             base = 16;
178           }
179         else
180           base = 8;
181       }
182     else
183       base = 10;
184
185   /* Save the pointer so we can check later if anything happened.  */
186   save = s;
187
188 #ifdef USE_NUMBER_GROUPING
189   if (group)
190     {
191       /* Find the end of the digit string and check its grouping.  */
192       end = s;
193       for (c = *end; c != '\0'; c = *++end)
194         if (c != thousands && !isdigit (c) &&
195             (!isalpha (c) || toupper (c) - 'A' + 10 >= base))
196           break;
197       if (*s == thousands)
198         end = s;
199       else
200         end = correctly_grouped_prefix (s, end, thousands, grouping);
201     }
202   else
203 #endif
204     end = NULL;
205
206   cutoff = ULONG_MAX / (unsigned LONG int) base;
207   cutlim = ULONG_MAX % (unsigned LONG int) base;
208
209   overflow = 0;
210   i = 0;
211   for (c = *s; c != '\0'; c = *++s)
212     {
213       if (s == end)
214         break;
215       if (isdigit (c))
216         c -= '0';
217       else if (isalpha (c))
218         c = toupper (c) - 'A' + 10;
219       else
220         break;
221       if (c >= base)
222         break;
223       /* Check for overflow.  */
224       if (i > cutoff || (i == cutoff && c > cutlim))
225         overflow = 1;
226       else
227         {
228           i *= (unsigned LONG int) base;
229           i += c;
230         }
231     }
232
233   /* Check if anything actually happened.  */
234   if (s == save)
235     goto noconv;
236
237   /* Store in ENDPTR the address of one character
238      past the last character we converted.  */
239   if (endptr != NULL)
240     *endptr = (char *) s;
241
242 #if !UNSIGNED
243   /* Check for a value that is within the range of
244      `unsigned LONG int', but outside the range of `LONG int'.  */
245   if (i > (negative ?
246            -(unsigned LONG int) LONG_MIN : (unsigned LONG int) LONG_MAX))
247     overflow = 1;
248 #endif
249
250   if (overflow)
251     {
252       errno = ERANGE;
253 #if UNSIGNED
254       return ULONG_MAX;
255 #else
256       return negative ? LONG_MIN : LONG_MAX;
257 #endif
258     }
259
260   /* Return the result of the appropriate sign.  */
261   return (negative ? -i : i);
262
263 noconv:
264   /* We must handle a special case here: the base is 0 or 16 and the
265      first two characters and '0' and 'x', but the rest are no
266      hexadecimal digits.  This is no error case.  We return 0 and
267      ENDPTR points to the `x`.  */
268   if (endptr != NULL)
269     if (save - nptr >= 2 && tolower (save[-1]) == 'x' && save[-2] == '0')
270       *endptr = (char *) &save[-1];
271     else
272       /*  There was no number to convert.  */
273       *endptr = (char *) nptr;
274
275   return 0L;
276 }
277
278 /* External user entry point.  */
279
280 INT
281 #ifdef __strtol
282 __strtol
283 #else
284 strtol
285 #endif
286         (nptr, endptr, base)
287      const char *nptr;
288      char **endptr;
289      int base;
290 {
291   return INTERNAL (strtol) (nptr, endptr, base, 0);
292 }
293
294 #ifdef weak_symbol
295 weak_symbol (strtol)
296 #endif