1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1988 AT&T */ 28 /* All Rights Reserved */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #if !defined(_BOOT) && !defined(_KMDB) 33 #include "lint.h" 34 #endif /* !_BOOT && !_KMDB */ 35 #include <errno.h> 36 #include <ctype.h> 37 #include <limits.h> 38 #include <stdlib.h> 39 #include <sys/types.h> 40 41 #define DIGIT(x) \ 42 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 43 44 #define MBASE ('z' - 'a' + 1 + 10) 45 46 /* 47 * The following macro is a local version of isalnum() which limits 48 * alphabetic characters to the ranges a-z and A-Z; locale dependent 49 * characters will not return 1. The members of a-z and A-Z are 50 * assumed to be in ascending order and contiguous 51 */ 52 #define lisalnum(x) \ 53 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 54 55 unsigned long 56 strtoul(const char *str, char **nptr, int base) 57 { 58 unsigned long val; 59 int c; 60 int xx; 61 unsigned long multmax; 62 int neg = 0; 63 const char **ptr = (const char **)nptr; 64 const unsigned char *ustr = (const unsigned char *)str; 65 66 if (ptr != (const char **)0) 67 *ptr = (char *)ustr; /* in case no number is formed */ 68 if (base < 0 || base > MBASE || base == 1) { 69 errno = EINVAL; 70 return (0); /* base is invalid -- should be a fatal error */ 71 } 72 if (!isalnum(c = *ustr)) { 73 while (isspace(c)) 74 c = *++ustr; 75 switch (c) { 76 case '-': 77 neg++; 78 /* FALLTHROUGH */ 79 case '+': 80 c = *++ustr; 81 } 82 } 83 if (base == 0) 84 if (c != '0') 85 base = 10; 86 else if (ustr[1] == 'x' || ustr[1] == 'X') 87 base = 16; 88 else 89 base = 8; 90 /* 91 * for any base > 10, the digits incrementally following 92 * 9 are assumed to be "abc...z" or "ABC...Z" 93 */ 94 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) 95 return (0); /* no number formed */ 96 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 97 isxdigit(ustr[2])) 98 c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 99 100 multmax = ULONG_MAX / (unsigned long)base; 101 val = DIGIT(c); 102 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 103 if (val > multmax) 104 goto overflow; 105 val *= base; 106 if (ULONG_MAX - val < xx) 107 goto overflow; 108 val += xx; 109 c = *++ustr; 110 } 111 if (ptr != (const char **)0) 112 *ptr = (char *)ustr; 113 return (neg ? -val : val); 114 115 overflow: 116 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 117 ; 118 if (ptr != (const char **)0) 119 *ptr = (char *)ustr; 120 errno = ERANGE; 121 return (ULONG_MAX); 122 } 123