diff options
Diffstat (limited to 'openbsd-compat/strtoul.c')
-rw-r--r-- | openbsd-compat/strtoul.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/openbsd-compat/strtoul.c b/openbsd-compat/strtoul.c new file mode 100644 index 000000000..877e6a01f --- /dev/null +++ b/openbsd-compat/strtoul.c | |||
@@ -0,0 +1,112 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #include "includes.h" | ||
31 | #ifndef HAVE_STRTOUL | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strtoul.c,v 1.5 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <ctype.h> | ||
38 | #include <errno.h> | ||
39 | #include <limits.h> | ||
40 | #include <stdlib.h> | ||
41 | |||
42 | /* | ||
43 | * Convert a string to an unsigned long integer. | ||
44 | * | ||
45 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
46 | * alphabets and digits are each contiguous. | ||
47 | */ | ||
48 | unsigned long | ||
49 | strtoul(nptr, endptr, base) | ||
50 | const char *nptr; | ||
51 | char **endptr; | ||
52 | register int base; | ||
53 | { | ||
54 | register const char *s; | ||
55 | register unsigned long acc, cutoff; | ||
56 | register int c; | ||
57 | register int neg, any, cutlim; | ||
58 | |||
59 | /* | ||
60 | * See strtol for comments as to the logic used. | ||
61 | */ | ||
62 | s = nptr; | ||
63 | do { | ||
64 | c = (unsigned char) *s++; | ||
65 | } while (isspace(c)); | ||
66 | if (c == '-') { | ||
67 | neg = 1; | ||
68 | c = *s++; | ||
69 | } else { | ||
70 | neg = 0; | ||
71 | if (c == '+') | ||
72 | c = *s++; | ||
73 | } | ||
74 | if ((base == 0 || base == 16) && | ||
75 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
76 | c = s[1]; | ||
77 | s += 2; | ||
78 | base = 16; | ||
79 | } | ||
80 | if (base == 0) | ||
81 | base = c == '0' ? 8 : 10; | ||
82 | |||
83 | cutoff = ULONG_MAX / (unsigned long)base; | ||
84 | cutlim = ULONG_MAX % (unsigned long)base; | ||
85 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
86 | if (isdigit(c)) | ||
87 | c -= '0'; | ||
88 | else if (isalpha(c)) | ||
89 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
90 | else | ||
91 | break; | ||
92 | if (c >= base) | ||
93 | break; | ||
94 | if (any < 0) | ||
95 | continue; | ||
96 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
97 | any = -1; | ||
98 | acc = ULONG_MAX; | ||
99 | errno = ERANGE; | ||
100 | } else { | ||
101 | any = 1; | ||
102 | acc *= (unsigned long)base; | ||
103 | acc += c; | ||
104 | } | ||
105 | } | ||
106 | if (neg && any > 0) | ||
107 | acc = -acc; | ||
108 | if (endptr != 0) | ||
109 | *endptr = (char *) (any ? s - 1 : nptr); | ||
110 | return (acc); | ||
111 | } | ||
112 | #endif /* !HAVE_STRTOUL */ | ||