new copyright; att/bsd/shared
[unix-history] / usr / src / lib / libc / stdlib / strtol.c
CommitLineData
40dadc22
KB
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
6c1d546f
KB
3 * All rights reserved.
4 *
40dadc22 5 * %sccs.include.redist.c%
6c1d546f
KB
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
f0a345ab 9static char sccsid[] = "@(#)strtol.c 5.4 (Berkeley) %G%";
6c1d546f
KB
10#endif /* LIBC_SCCS and not lint */
11
40dadc22 12#include <limits.h>
6c1d546f 13#include <ctype.h>
40dadc22 14#include <errno.h>
dd967b02
KB
15#include <stdlib.h>
16
6c1d546f 17
40dadc22
KB
18/*
19 * Convert a string to a long integer.
20 *
21 * Ignores `locale' stuff. Assumes that the upper and lower case
22 * alphabets and digits are each contiguous.
23 */
6c1d546f 24long
40dadc22 25strtol(nptr, endptr, base)
f0a345ab
DS
26 const char *nptr;
27 char **endptr;
6c1d546f
KB
28 register int base;
29{
f0a345ab 30 register const char *s = nptr;
40dadc22 31 register unsigned long acc;
6c1d546f 32 register int c;
40dadc22
KB
33 register unsigned long cutoff;
34 register int neg = 0, any, cutlim;
6c1d546f 35
40dadc22
KB
36 /*
37 * Skip white space and pick up leading +/- sign if any.
38 * If base is 0, allow 0x for hex and 0 for octal, else
39 * assume decimal; if base is already 16, allow 0x.
40 */
41 do {
42 c = *s++;
43 } while (isspace(c));
44 if (c == '-') {
45 neg = 1;
46 c = *s++;
47 } else if (c == '+')
48 c = *s++;
49 if ((base == 0 || base == 16) &&
50 c == '0' && (*s == 'x' || *s == 'X')) {
51 c = s[1];
52 s += 2;
53 base = 16;
6c1d546f 54 }
40dadc22
KB
55 if (base == 0)
56 base = c == '0' ? 8 : 10;
6c1d546f
KB
57
58 /*
40dadc22
KB
59 * Compute the cutoff value between legal numbers and illegal
60 * numbers. That is the largest legal value, divided by the
61 * base. An input number that is greater than this value, if
62 * followed by a legal input character, is too big. One that
63 * is equal to this value may be valid or not; the limit
64 * between valid and invalid numbers is then based on the last
65 * digit. For instance, if the range for longs is
66 * [-2147483648..2147483647] and the input base is 10,
67 * cutoff will be set to 214748364 and cutlim to either
68 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
69 * a value > 214748364, or equal but the next digit is > 7 (or 8),
70 * the number is too big, and we will return a range error.
71 *
72 * Set any if any `digits' consumed; make it negative to indicate
73 * overflow.
6c1d546f 74 */
40dadc22
KB
75 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
76 cutlim = cutoff % (unsigned long)base;
77 cutoff /= (unsigned long)base;
78 for (acc = 0, any = 0;; c = *s++) {
6c1d546f
KB
79 if (isdigit(c))
80 c -= '0';
40dadc22
KB
81 else if (isalpha(c))
82 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
83 else
84 break;
6c1d546f
KB
85 if (c >= base)
86 break;
40dadc22
KB
87 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
88 any = -1;
89 else {
90 any = 1;
91 acc *= base;
92 acc += c;
93 }
6c1d546f 94 }
40dadc22
KB
95 if (any < 0) {
96 acc = neg ? LONG_MIN : LONG_MAX;
97 errno = ERANGE;
98 } else if (neg)
99 acc = -acc;
100 if (endptr != 0)
f0a345ab 101 *endptr = any ? s - 1 : (char *)nptr;
40dadc22 102 return (acc);
6c1d546f 103}