2 * strtol : convert a string to long.
4 * Andy Wilson, 2-Oct-89.
16 #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
23 const char *s; char **ptr; int base;
25 unsigned long total = 0;
31 unsigned long maxdiv, maxrem;
47 if (base==0 || base==16) /* the 'base==16' is for handling 0x */
52 * try to infer base from the string
55 tmp = 10; /* doesn't start with 0 - assume decimal */
56 else if (s[1] == 'X' || s[1] == 'x')
57 tmp = 16, s += 2; /* starts with 0x or 0X - hence hex */
59 tmp = 8; /* starts with 0 - hence octal */
64 maxdiv = ULONG_MAX / base;
65 maxrem = ULONG_MAX % base;
67 while ((digit = *s) != '\0')
69 if (digit >= '0' && digit < ('0'+base))
74 if (digit >= 'a' && digit < ('a'+(base-10)))
75 digit = digit - 'a' + 10;
76 else if (digit >= 'A' && digit < ('A'+(base-10)))
77 digit = digit - 'A' + 10;
85 || (total == maxdiv && digit > maxrem))
87 total = (total * base) + digit;
98 *ptr = (char *) ((did_conversion) ? (char *)s : (char *)start);
99 return negate ? -total : total;