blob: 965421be36b4e8f63ac186e7498e495a4dfb2b95 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001/***********************************************************
2Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/*
26** strtoul
27** This is a general purpose routine for converting
28** an ascii string to an integer in an arbitrary base.
29** Leading white space is ignored. If 'base' is zero
30** it looks for a leading 0, 0x or 0X to tell which
31** base. If these are absent it defaults to 10.
32** Base must be 0 or between 2 and 36 (inclusive).
33** If 'ptr' is non-NULL it will contain a pointer to
34** the end of the scan.
35** Errors due to bad pointers will probably result in
36** exceptions - we don't check for them.
37*/
38
39#include <ctype.h>
40#include <errno.h>
41
42unsigned long
43strtoul(str, ptr, base)
44register char * str;
45char ** ptr;
46int base;
47{
48 register unsigned long result; /* return value of the function */
49 register int c; /* current input character */
50 register unsigned long temp; /* used in overflow testing */
51 int ovf; /* true if overflow occurred */
52
53 result = 0;
54 ovf = 0;
55
56/* catch silly bases */
57 if (base != 0 && (base < 2 || base > 36))
58 {
59 if (ptr)
60 *ptr = str;
61 return 0;
62 }
63
64/* skip leading white space */
65 while (*str && isspace(*str))
66 str++;
67
68/* check for leading 0 or 0x for auto-base or base 16 */
69 switch (base)
70 {
71 case 0: /* look for leading 0, 0x or 0X */
72 if (*str == '0')
73 {
74 str++;
75 if (*str == 'x' || *str == 'X')
76 {
77 str++;
78 base = 16;
79 }
80 else
81 base = 8;
82 }
83 else
84 base = 10;
85 break;
86
87 case 16: /* skip leading 0x or 0X */
88 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
89 str += 2;
90 break;
91 }
92
93/* do the conversion */
94 while (c = *str)
95 {
96 if (isdigit(c) && c - '0' < base)
97 c -= '0';
98 else
99 {
100 if (isupper(c))
101 c = tolower(c);
102 if (c >= 'a' && c <= 'z')
103 c -= 'a' - 10;
104 else /* non-"digit" character */
105 break;
106 if (c >= base) /* non-"digit" character */
107 break;
108 }
109 temp = result;
110 result = result * base + c;
111 if ((result - c) / base != temp) /* overflow */
112 ovf = 1;
113 str++;
114 }
115
116/* set pointer to point to the last character scanned */
117 if (ptr)
118 *ptr = str;
119 if (ovf)
120 {
121 result = ~0;
122 errno = ERANGE;
123 }
124 return result;
125}
126
127long
128strtol(str, ptr, base)
129char * str;
130char ** ptr;
131int base;
132{
133 long result;
134 char sign;
135
136 while (*str && isspace(*str))
137 str++;
138
139 sign = *str;
140 if (sign == '+' || sign == '-')
141 str++;
142
143 result = (long) strtoul(str, ptr, base);
144
145 /* Signal overflow if the result appears negative,
146 except for the largest negative integer */
147 if (result < 0 && !(sign == '-' && result == -result)) {
148 errno = ERANGE;
149 result = 0x7fffffff;
150 }
151
152 if (sign == '-')
153 result = -result;
154
155 return result;
156}