blob: 10ddc6ec2fcacdc4ba405c6d9440f0dd528953d3 [file] [log] [blame]
Guido van Rossumbe0e9421993-12-24 10:32:00 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumbe0e9421993-12-24 10:32:00 +00004
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
Guido van Rossumb6775db1994-08-01 11:34:53 +000025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29/* strtol and strtoul, renamed to avoid conflicts */
30
Guido van Rossumbe0e9421993-12-24 10:32:00 +000031/*
32** strtoul
33** This is a general purpose routine for converting
34** an ascii string to an integer in an arbitrary base.
35** Leading white space is ignored. If 'base' is zero
36** it looks for a leading 0, 0x or 0X to tell which
37** base. If these are absent it defaults to 10.
38** Base must be 0 or between 2 and 36 (inclusive).
39** If 'ptr' is non-NULL it will contain a pointer to
40** the end of the scan.
41** Errors due to bad pointers will probably result in
42** exceptions - we don't check for them.
43*/
44
45#include <ctype.h>
46#include <errno.h>
47
48unsigned long
Guido van Rossumb6775db1994-08-01 11:34:53 +000049mystrtoul(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +000050register char * str;
51char ** ptr;
52int base;
53{
54 register unsigned long result; /* return value of the function */
55 register int c; /* current input character */
56 register unsigned long temp; /* used in overflow testing */
57 int ovf; /* true if overflow occurred */
58
59 result = 0;
60 ovf = 0;
61
62/* catch silly bases */
63 if (base != 0 && (base < 2 || base > 36))
64 {
65 if (ptr)
66 *ptr = str;
67 return 0;
68 }
69
70/* skip leading white space */
71 while (*str && isspace(*str))
72 str++;
73
74/* check for leading 0 or 0x for auto-base or base 16 */
75 switch (base)
76 {
77 case 0: /* look for leading 0, 0x or 0X */
78 if (*str == '0')
79 {
80 str++;
81 if (*str == 'x' || *str == 'X')
82 {
83 str++;
84 base = 16;
85 }
86 else
87 base = 8;
88 }
89 else
90 base = 10;
91 break;
92
93 case 16: /* skip leading 0x or 0X */
94 if (*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
95 str += 2;
96 break;
97 }
98
99/* do the conversion */
100 while (c = *str)
101 {
102 if (isdigit(c) && c - '0' < base)
103 c -= '0';
104 else
105 {
106 if (isupper(c))
107 c = tolower(c);
108 if (c >= 'a' && c <= 'z')
109 c -= 'a' - 10;
110 else /* non-"digit" character */
111 break;
112 if (c >= base) /* non-"digit" character */
113 break;
114 }
115 temp = result;
116 result = result * base + c;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117#ifndef MPW
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000118 if ((result - c) / base != temp) /* overflow */
119 ovf = 1;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120#endif
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000121 str++;
122 }
123
124/* set pointer to point to the last character scanned */
125 if (ptr)
126 *ptr = str;
127 if (ovf)
128 {
129 result = ~0;
130 errno = ERANGE;
131 }
132 return result;
133}
134
135long
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136mystrtol(str, ptr, base)
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000137char * str;
138char ** ptr;
139int base;
140{
141 long result;
142 char sign;
143
144 while (*str && isspace(*str))
145 str++;
146
147 sign = *str;
148 if (sign == '+' || sign == '-')
149 str++;
150
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151 result = (long) mystrtoul(str, ptr, base);
Guido van Rossumbe0e9421993-12-24 10:32:00 +0000152
153 /* Signal overflow if the result appears negative,
154 except for the largest negative integer */
155 if (result < 0 && !(sign == '-' && result == -result)) {
156 errno = ERANGE;
157 result = 0x7fffffff;
158 }
159
160 if (sign == '-')
161 result = -result;
162
163 return result;
164}