19c5cbc30SBill Fenner /*- 29c5cbc30SBill Fenner * Copyright (c) 1992, 1993 39c5cbc30SBill Fenner * The Regents of the University of California. All rights reserved. 49c5cbc30SBill Fenner * 59c5cbc30SBill Fenner * Redistribution and use in source and binary forms, with or without 69c5cbc30SBill Fenner * modification, are permitted provided that the following conditions 79c5cbc30SBill Fenner * are met: 89c5cbc30SBill Fenner * 1. Redistributions of source code must retain the above copyright 99c5cbc30SBill Fenner * notice, this list of conditions and the following disclaimer. 109c5cbc30SBill Fenner * 2. Redistributions in binary form must reproduce the above copyright 119c5cbc30SBill Fenner * notice, this list of conditions and the following disclaimer in the 129c5cbc30SBill Fenner * documentation and/or other materials provided with the distribution. 139c5cbc30SBill Fenner * 3. All advertising materials mentioning features or use of this software 149c5cbc30SBill Fenner * must display the following acknowledgement: 159c5cbc30SBill Fenner * This product includes software developed by the University of 169c5cbc30SBill Fenner * California, Berkeley and its contributors. 179c5cbc30SBill Fenner * 4. Neither the name of the University nor the names of its contributors 189c5cbc30SBill Fenner * may be used to endorse or promote products derived from this software 199c5cbc30SBill Fenner * without specific prior written permission. 209c5cbc30SBill Fenner * 219c5cbc30SBill Fenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229c5cbc30SBill Fenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239c5cbc30SBill Fenner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249c5cbc30SBill Fenner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259c5cbc30SBill Fenner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269c5cbc30SBill Fenner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279c5cbc30SBill Fenner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289c5cbc30SBill Fenner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299c5cbc30SBill Fenner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309c5cbc30SBill Fenner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319c5cbc30SBill Fenner * SUCH DAMAGE. 329c5cbc30SBill Fenner * 339c5cbc30SBill Fenner * $FreeBSD$ 349c5cbc30SBill Fenner */ 359c5cbc30SBill Fenner 369c5cbc30SBill Fenner #if defined(LIBC_SCCS) && !defined(lint) 379c5cbc30SBill Fenner static char sccsid[] = "from @(#)strtoul.c 8.1 (Berkeley) 6/4/93"; 389c5cbc30SBill Fenner #endif /* LIBC_SCCS and not lint */ 399c5cbc30SBill Fenner 409c5cbc30SBill Fenner #include <ctype.h> 419c5cbc30SBill Fenner #include <errno.h> 429c5cbc30SBill Fenner #include <stdlib.h> 439c5cbc30SBill Fenner #include <inttypes.h> 449c5cbc30SBill Fenner 459c5cbc30SBill Fenner /* 469c5cbc30SBill Fenner * Convert a string to a uintmax_t integer. 479c5cbc30SBill Fenner * 489c5cbc30SBill Fenner * Assumes that the upper and lower case 499c5cbc30SBill Fenner * alphabets and digits are each contiguous. 509c5cbc30SBill Fenner */ 519c5cbc30SBill Fenner uintmax_t 529c5cbc30SBill Fenner strtoumax(nptr, endptr, base) 539c5cbc30SBill Fenner const char *nptr; 549c5cbc30SBill Fenner char **endptr; 559c5cbc30SBill Fenner int base; 569c5cbc30SBill Fenner { 579c5cbc30SBill Fenner const char *s; 589c5cbc30SBill Fenner uintmax_t acc; 599c5cbc30SBill Fenner unsigned char c; 609c5cbc30SBill Fenner uintmax_t cutoff; 619c5cbc30SBill Fenner int neg, any, cutlim; 629c5cbc30SBill Fenner 639c5cbc30SBill Fenner /* 649c5cbc30SBill Fenner * See strtoimax for comments as to the logic used. 659c5cbc30SBill Fenner */ 669c5cbc30SBill Fenner s = nptr; 679c5cbc30SBill Fenner do { 689c5cbc30SBill Fenner c = *s++; 699c5cbc30SBill Fenner } while (isspace(c)); 709c5cbc30SBill Fenner if (c == '-') { 719c5cbc30SBill Fenner neg = 1; 729c5cbc30SBill Fenner c = *s++; 739c5cbc30SBill Fenner } else { 749c5cbc30SBill Fenner neg = 0; 759c5cbc30SBill Fenner if (c == '+') 769c5cbc30SBill Fenner c = *s++; 779c5cbc30SBill Fenner } 789c5cbc30SBill Fenner if ((base == 0 || base == 16) && 799c5cbc30SBill Fenner c == '0' && (*s == 'x' || *s == 'X')) { 809c5cbc30SBill Fenner c = s[1]; 819c5cbc30SBill Fenner s += 2; 829c5cbc30SBill Fenner base = 16; 839c5cbc30SBill Fenner } 849c5cbc30SBill Fenner if (base == 0) 859c5cbc30SBill Fenner base = c == '0' ? 8 : 10; 869c5cbc30SBill Fenner acc = any = 0; 879c5cbc30SBill Fenner if (base < 2 || base > 36) 889c5cbc30SBill Fenner goto noconv; 899c5cbc30SBill Fenner 909c5cbc30SBill Fenner cutoff = UINTMAX_MAX / base; 919c5cbc30SBill Fenner cutlim = UINTMAX_MAX % base; 929c5cbc30SBill Fenner for ( ; ; c = *s++) { 939c5cbc30SBill Fenner if (isxdigit(c)) 949c5cbc30SBill Fenner c = digittoint(c); 959c5cbc30SBill Fenner else if (isascii(c) && isalpha(c)) 969c5cbc30SBill Fenner c -= isupper(c) ? 'A' - 10 : 'a' - 10; 979c5cbc30SBill Fenner else 989c5cbc30SBill Fenner break; 999c5cbc30SBill Fenner if (c >= base) 1009c5cbc30SBill Fenner break; 1019c5cbc30SBill Fenner if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 1029c5cbc30SBill Fenner any = -1; 1039c5cbc30SBill Fenner else { 1049c5cbc30SBill Fenner any = 1; 1059c5cbc30SBill Fenner acc *= base; 1069c5cbc30SBill Fenner acc += c; 1079c5cbc30SBill Fenner } 1089c5cbc30SBill Fenner } 1099c5cbc30SBill Fenner if (any < 0) { 1109c5cbc30SBill Fenner acc = UINTMAX_MAX; 1119c5cbc30SBill Fenner errno = ERANGE; 1129c5cbc30SBill Fenner } else if (!any) { 1139c5cbc30SBill Fenner noconv: 1149c5cbc30SBill Fenner errno = EINVAL; 1159c5cbc30SBill Fenner } else if (neg) 1169c5cbc30SBill Fenner acc = -acc; 1179c5cbc30SBill Fenner if (endptr != NULL) 1189c5cbc30SBill Fenner *endptr = (char *)(any ? s - 1 : nptr); 1199c5cbc30SBill Fenner return (acc); 1209c5cbc30SBill Fenner } 121