1*04ac863bSchristos /* $NetBSD: strtol.c,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $ */
2*04ac863bSchristos
3*04ac863bSchristos /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
4*04ac863bSchristos Free Software Foundation, Inc.
5*04ac863bSchristos Written by James Clark (jjc@jclark.com)
6*04ac863bSchristos
7*04ac863bSchristos This file is part of groff.
8*04ac863bSchristos
9*04ac863bSchristos groff is free software; you can redistribute it and/or modify it under
10*04ac863bSchristos the terms of the GNU General Public License as published by the Free
11*04ac863bSchristos Software Foundation; either version 2, or (at your option) any later
12*04ac863bSchristos version.
13*04ac863bSchristos
14*04ac863bSchristos groff is distributed in the hope that it will be useful, but WITHOUT ANY
15*04ac863bSchristos WARRANTY; without even the implied warranty of MERCHANTABILITY or
16*04ac863bSchristos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17*04ac863bSchristos for more details.
18*04ac863bSchristos
19*04ac863bSchristos You should have received a copy of the GNU General Public License along
20*04ac863bSchristos with groff; see the file COPYING. If not, write to the Free Software
21*04ac863bSchristos Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
22*04ac863bSchristos
23*04ac863bSchristos #ifdef HAVE_CONFIG_H
24*04ac863bSchristos #include <config.h>
25*04ac863bSchristos #endif
26*04ac863bSchristos
27*04ac863bSchristos #include <string.h>
28*04ac863bSchristos #include <ctype.h>
29*04ac863bSchristos #include <errno.h>
30*04ac863bSchristos
31*04ac863bSchristos #ifdef HAVE_LIMITS_H
32*04ac863bSchristos #include <limits.h>
33*04ac863bSchristos #endif
34*04ac863bSchristos
35*04ac863bSchristos #ifndef LONG_MAX
36*04ac863bSchristos #define LONG_MAX 2147483647
37*04ac863bSchristos #endif
38*04ac863bSchristos
39*04ac863bSchristos #ifndef LONG_MIN
40*04ac863bSchristos #define LONG_MIN (-LONG_MAX-1)
41*04ac863bSchristos #endif
42*04ac863bSchristos
43*04ac863bSchristos #ifdef isascii
44*04ac863bSchristos #define ISASCII(c) isascii(c)
45*04ac863bSchristos #else
46*04ac863bSchristos #define ISASCII(c) (1)
47*04ac863bSchristos #endif
48*04ac863bSchristos
strtol(str,ptr,base)49*04ac863bSchristos long strtol(str, ptr, base)
50*04ac863bSchristos char *str, **ptr;
51*04ac863bSchristos int base;
52*04ac863bSchristos {
53*04ac863bSchristos char *start = str;
54*04ac863bSchristos int neg = 0;
55*04ac863bSchristos long val;
56*04ac863bSchristos char *p;
57*04ac863bSchristos static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
58*04ac863bSchristos
59*04ac863bSchristos while (ISASCII((unsigned char)*str) && isspace((unsigned char)*str))
60*04ac863bSchristos str++;
61*04ac863bSchristos
62*04ac863bSchristos if (*str == '-') {
63*04ac863bSchristos neg = 1;
64*04ac863bSchristos str++;
65*04ac863bSchristos }
66*04ac863bSchristos if (base == 0) {
67*04ac863bSchristos if (*str == '0') {
68*04ac863bSchristos if (str[1] == 'x' || str[1] == 'X') {
69*04ac863bSchristos str += 2;
70*04ac863bSchristos base = 16;
71*04ac863bSchristos }
72*04ac863bSchristos else
73*04ac863bSchristos base = 8;
74*04ac863bSchristos }
75*04ac863bSchristos else
76*04ac863bSchristos base = 10;
77*04ac863bSchristos }
78*04ac863bSchristos if (base < 2 || base > 36)
79*04ac863bSchristos base = 10;
80*04ac863bSchristos else if (base == 16 && *str == '0' && (str[1] == 'x' || str[1] == 'X'))
81*04ac863bSchristos str += 2;
82*04ac863bSchristos
83*04ac863bSchristos p = strchr(digits, (ISASCII((unsigned char)*str)
84*04ac863bSchristos && isupper((unsigned char)*str)
85*04ac863bSchristos ? tolower((unsigned char)*str)
86*04ac863bSchristos : *str));
87*04ac863bSchristos if (p == 0 || (val = (p - digits)) >= base) {
88*04ac863bSchristos if (base == 16 && str > start && (str[-1] == 'x' || str[-1] == 'X')) {
89*04ac863bSchristos if (ptr)
90*04ac863bSchristos *ptr = str - 1;
91*04ac863bSchristos }
92*04ac863bSchristos else {
93*04ac863bSchristos if (ptr)
94*04ac863bSchristos *ptr = start;
95*04ac863bSchristos errno = ERANGE;
96*04ac863bSchristos }
97*04ac863bSchristos return 0;
98*04ac863bSchristos }
99*04ac863bSchristos if (neg)
100*04ac863bSchristos val = -val;
101*04ac863bSchristos
102*04ac863bSchristos while (*++str != '\0') {
103*04ac863bSchristos int n;
104*04ac863bSchristos
105*04ac863bSchristos p = strchr(digits, (ISASCII((unsigned char)*str)
106*04ac863bSchristos && isupper((unsigned char)*str)
107*04ac863bSchristos ? tolower((unsigned char)*str) : *str));
108*04ac863bSchristos if (p == 0)
109*04ac863bSchristos break;
110*04ac863bSchristos n = p - digits;
111*04ac863bSchristos if (n >= base)
112*04ac863bSchristos break;
113*04ac863bSchristos if (neg) {
114*04ac863bSchristos if (-(unsigned long)val > (-(unsigned long)LONG_MIN - n)/base) {
115*04ac863bSchristos val = LONG_MIN;
116*04ac863bSchristos errno = ERANGE;
117*04ac863bSchristos }
118*04ac863bSchristos else
119*04ac863bSchristos val = val*base - n;
120*04ac863bSchristos }
121*04ac863bSchristos else {
122*04ac863bSchristos if (val > (LONG_MAX - n)/base) {
123*04ac863bSchristos val = LONG_MAX;
124*04ac863bSchristos errno = ERANGE;
125*04ac863bSchristos }
126*04ac863bSchristos else
127*04ac863bSchristos val = val*base + n;
128*04ac863bSchristos }
129*04ac863bSchristos }
130*04ac863bSchristos
131*04ac863bSchristos if (ptr)
132*04ac863bSchristos *ptr = str;
133*04ac863bSchristos
134*04ac863bSchristos return val;
135*04ac863bSchristos }
136