diff options
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/Makefile.in | 4 | ||||
-rw-r--r-- | openbsd-compat/strtoul.c | 112 |
2 files changed, 114 insertions, 2 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index c48593f7b..de9856eea 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in | |||
@@ -1,4 +1,4 @@ | |||
1 | # $Id: Makefile.in,v 1.28 2003/07/24 06:52:14 mouring Exp $ | 1 | # $Id: Makefile.in,v 1.29 2003/10/07 07:49:57 dtucker Exp $ |
2 | 2 | ||
3 | sysconfdir=@sysconfdir@ | 3 | sysconfdir=@sysconfdir@ |
4 | piddir=@piddir@ | 4 | piddir=@piddir@ |
@@ -16,7 +16,7 @@ RANLIB=@RANLIB@ | |||
16 | INSTALL=@INSTALL@ | 16 | INSTALL=@INSTALL@ |
17 | LDFLAGS=-L. @LDFLAGS@ | 17 | LDFLAGS=-L. @LDFLAGS@ |
18 | 18 | ||
19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o vis.o | 19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtoul.o vis.o |
20 | 20 | ||
21 | COMPAT=bsd-arc4random.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o | 21 | COMPAT=bsd-arc4random.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o |
22 | 22 | ||
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 */ | ||