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