No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / lwres / strtoul.c
blob6fa6f474706ad6a72f3d11959b1deb40f79375be
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Copyright (c) 1990, 1993
22 * The Regents of the University of California. All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
53 /*! \file */
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
56 #endif /* LIBC_SCCS and not lint */
58 /* Id: strtoul.c,v 1.4 2007/06/19 23:47:22 tbox Exp */
60 #include <config.h>
62 #include <limits.h>
63 #include <ctype.h>
64 #include <errno.h>
66 #include <lwres/stdlib.h>
68 #define DE_CONST(konst, var) \
69 do { \
70 union { const void *k; void *v; } _u; \
71 _u.k = konst; \
72 var = _u.v; \
73 } while (0)
75 /*!
76 * Convert a string to an unsigned long integer.
78 * Ignores `locale' stuff. Assumes that the upper and lower case
79 * alphabets and digits are each contiguous.
81 unsigned long
82 lwres_strtoul(const char *nptr, char **endptr, int base) {
83 const char *s = nptr;
84 unsigned long acc;
85 unsigned char c;
86 unsigned long cutoff;
87 int neg = 0, any, cutlim;
90 * See strtol for comments as to the logic used.
92 do {
93 c = *s++;
94 } while (isspace(c));
95 if (c == '-') {
96 neg = 1;
97 c = *s++;
98 } else if (c == '+')
99 c = *s++;
100 if ((base == 0 || base == 16) &&
101 c == '0' && (*s == 'x' || *s == 'X')) {
102 c = s[1];
103 s += 2;
104 base = 16;
106 if (base == 0)
107 base = c == '0' ? 8 : 10;
108 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
109 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
110 for (acc = 0, any = 0;; c = *s++) {
111 if (!isascii(c))
112 break;
113 if (isdigit(c))
114 c -= '0';
115 else if (isalpha(c))
116 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
117 else
118 break;
119 if (c >= base)
120 break;
121 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
122 any = -1;
123 else {
124 any = 1;
125 acc *= base;
126 acc += c;
129 if (any < 0) {
130 acc = ULONG_MAX;
131 errno = ERANGE;
132 } else if (neg)
133 acc = -acc;
134 if (endptr != 0)
135 DE_CONST(any ? s - 1 : nptr, *endptr);
136 return (acc);