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 2011 Nexenta Systems, Inc. All rights reserved. 24 */ 25 26 /* 27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 /* Copyright (c) 1988 AT&T */ 32 /* All Rights Reserved */ 33 34 #if defined(_KERNEL) && !defined(_BOOT) 35 #include <sys/null.h> 36 #include <sys/errno.h> 37 #else /* _KERNEL && !_BOOT */ 38 #if !defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE) 39 #include "lint.h" 40 #endif /* !_BOOT && !_KMDB && !_STANDALONE */ 41 #if defined(_STANDALONE) 42 #include <sys/cdefs.h> 43 #include <stand.h> 44 #include <limits.h> 45 #else 46 #include <errno.h> 47 #include <ctype.h> 48 #include <limits.h> 49 #include <stdlib.h> 50 #endif /* _STANDALONE */ 51 #endif /* _KERNEL && !_BOOT */ 52 #include "strtolctype.h" 53 #include <sys/types.h> 54 55 #if defined(_KERNEL) && !defined(_BOOT) 56 int 57 ddi_strtol(const char *str, char **nptr, int base, long *result) 58 #else /* _KERNEL && !_BOOT */ 59 long 60 strtol(const char *str, char **nptr, int base) 61 #endif /* _KERNEL && !_BOOT */ 62 { 63 long val; 64 int c; 65 int xx; 66 int neg = 0; 67 long multmin; 68 long limit; 69 const char **ptr = (const char **)nptr; 70 const unsigned char *ustr = (const unsigned char *)str; 71 72 if (ptr != NULL) 73 *ptr = (char *)ustr; /* in case no number is formed */ 74 if (base < 0 || base > MBASE || base == 1) { 75 /* base is invalid -- should be a fatal error */ 76 #if defined(_KERNEL) && !defined(_BOOT) 77 return (EINVAL); 78 #else /* _KERNEL && !_BOOT */ 79 errno = EINVAL; 80 return (0); 81 #endif /* _KERNEL && !_BOOT */ 82 } 83 if (!isalnum(c = *ustr)) { 84 while (isspace(c)) 85 c = *++ustr; 86 switch (c) { 87 case '-': 88 neg++; 89 /* FALLTHROUGH */ 90 case '+': 91 c = *++ustr; 92 } 93 } 94 if (base == 0) { 95 if (c != '0') 96 base = 10; 97 else if (ustr[1] == 'x' || ustr[1] == 'X') 98 base = 16; 99 else 100 base = 8; 101 } 102 /* 103 * for any base > 10, the digits incrementally following 104 * 9 are assumed to be "abc...z" or "ABC...Z" 105 */ 106 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) { 107 /* no number formed */ 108 #if defined(_KERNEL) && !defined(_BOOT) 109 return (EINVAL); 110 #else /* _KERNEL && !_BOOT */ 111 errno = EINVAL; 112 return (0); 113 #endif /* _KERNEL && !_BOOT */ 114 } 115 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 116 isxdigit(ustr[2])) 117 c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 118 119 /* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */ 120 if (neg) 121 limit = LONG_MIN; 122 else 123 limit = -LONG_MAX; 124 multmin = limit / (long)base; 125 val = -DIGIT(c); 126 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 127 /* accumulate neg avoids surprises near LONG_MAX */ 128 if (val < multmin) 129 goto overflow; 130 val *= base; 131 if (val < limit + xx) 132 goto overflow; 133 val -= xx; 134 c = *++ustr; 135 } 136 if (ptr != NULL) 137 *ptr = (char *)ustr; 138 #if defined(_KERNEL) && !defined(_BOOT) 139 *result = neg ? val : -val; 140 return (0); 141 #else /* _KERNEL && !_BOOT */ 142 return (neg ? val : -val); 143 #endif /* _KERNEL && !_BOOT */ 144 145 overflow: 146 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 147 ; 148 if (ptr != NULL) 149 *ptr = (char *)ustr; 150 #if defined(_KERNEL) && !defined(_BOOT) 151 return (ERANGE); 152 #else /* _KERNEL && !_BOOT */ 153 errno = ERANGE; 154 return (neg ? LONG_MIN : LONG_MAX); 155 #endif /* _KERNEL && !_BOOT */ 156 } 157