17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* 23*ae2ada9cSYakov Zaytsev * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*ae2ada9cSYakov Zaytsev */ 25*ae2ada9cSYakov Zaytsev 26*ae2ada9cSYakov Zaytsev /* 272ef9abdcSjv227347 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 287c478bd9Sstevel@tonic-gate * Use is subject to license terms. 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317257d1b4Sraf /* Copyright (c) 1988 AT&T */ 327257d1b4Sraf /* All Rights Reserved */ 337257d1b4Sraf 342ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 352ef9abdcSjv227347 #include <sys/errno.h> 362ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 377c478bd9Sstevel@tonic-gate #if !defined(_BOOT) && !defined(_KMDB) 387257d1b4Sraf #include "lint.h" 397c478bd9Sstevel@tonic-gate #endif /* !_BOOT && !_KMDB */ 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate #include <ctype.h> 427c478bd9Sstevel@tonic-gate #include <limits.h> 437c478bd9Sstevel@tonic-gate #include <stdlib.h> 442ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 452ef9abdcSjv227347 #include "strtolctype.h" 462ef9abdcSjv227347 #include <sys/types.h> 477c478bd9Sstevel@tonic-gate 482ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 492ef9abdcSjv227347 int 502ef9abdcSjv227347 ddi_strtol(const char *str, char **nptr, int base, long *result) 512ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 527c478bd9Sstevel@tonic-gate long 537c478bd9Sstevel@tonic-gate strtol(const char *str, char **nptr, int base) 542ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 557c478bd9Sstevel@tonic-gate { 567c478bd9Sstevel@tonic-gate long val; 577c478bd9Sstevel@tonic-gate int c; 582ef9abdcSjv227347 int xx; 592ef9abdcSjv227347 int neg = 0; 607c478bd9Sstevel@tonic-gate long multmin; 617c478bd9Sstevel@tonic-gate long limit; 627c478bd9Sstevel@tonic-gate const char **ptr = (const char **)nptr; 637c478bd9Sstevel@tonic-gate const unsigned char *ustr = (const unsigned char *)str; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 667c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; /* in case no number is formed */ 677c478bd9Sstevel@tonic-gate if (base < 0 || base > MBASE || base == 1) { 682ef9abdcSjv227347 /* base is invalid -- should be a fatal error */ 692ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 702ef9abdcSjv227347 return (EINVAL); 712ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 727c478bd9Sstevel@tonic-gate errno = EINVAL; 732ef9abdcSjv227347 return (0); 742ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate if (!isalnum(c = *ustr)) { 777c478bd9Sstevel@tonic-gate while (isspace(c)) 787c478bd9Sstevel@tonic-gate c = *++ustr; 797c478bd9Sstevel@tonic-gate switch (c) { 807c478bd9Sstevel@tonic-gate case '-': 817c478bd9Sstevel@tonic-gate neg++; 827c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 837c478bd9Sstevel@tonic-gate case '+': 847c478bd9Sstevel@tonic-gate c = *++ustr; 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate if (base == 0) 887c478bd9Sstevel@tonic-gate if (c != '0') 897c478bd9Sstevel@tonic-gate base = 10; 907c478bd9Sstevel@tonic-gate else if (ustr[1] == 'x' || ustr[1] == 'X') 917c478bd9Sstevel@tonic-gate base = 16; 927c478bd9Sstevel@tonic-gate else 937c478bd9Sstevel@tonic-gate base = 8; 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * for any base > 10, the digits incrementally following 967c478bd9Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 977c478bd9Sstevel@tonic-gate */ 982ef9abdcSjv227347 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) { 992ef9abdcSjv227347 /* no number formed */ 1002ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1012ef9abdcSjv227347 return (EINVAL); 1022ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 103*ae2ada9cSYakov Zaytsev errno = EINVAL; 1042ef9abdcSjv227347 return (0); 1052ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1062ef9abdcSjv227347 } 1077c478bd9Sstevel@tonic-gate if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 1087c478bd9Sstevel@tonic-gate isxdigit(ustr[2])) 1097c478bd9Sstevel@tonic-gate c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */ 1127c478bd9Sstevel@tonic-gate if (neg) 1137c478bd9Sstevel@tonic-gate limit = LONG_MIN; 1147c478bd9Sstevel@tonic-gate else 1157c478bd9Sstevel@tonic-gate limit = -LONG_MAX; 1167c478bd9Sstevel@tonic-gate multmin = limit / (long)base; 1177c478bd9Sstevel@tonic-gate val = -DIGIT(c); 1187c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 1192ef9abdcSjv227347 /* accumulate neg avoids surprises near LONG_MAX */ 1207c478bd9Sstevel@tonic-gate if (val < multmin) 1217c478bd9Sstevel@tonic-gate goto overflow; 1227c478bd9Sstevel@tonic-gate val *= base; 1237c478bd9Sstevel@tonic-gate if (val < limit + xx) 1247c478bd9Sstevel@tonic-gate goto overflow; 1257c478bd9Sstevel@tonic-gate val -= xx; 1267c478bd9Sstevel@tonic-gate c = *++ustr; 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1297c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 1302ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1312ef9abdcSjv227347 *result = neg ? val : -val; 1322ef9abdcSjv227347 return (0); 1332ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1347c478bd9Sstevel@tonic-gate return (neg ? val : -val); 1352ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate overflow: 1387c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 1397c478bd9Sstevel@tonic-gate ; 1407c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1417c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 1422ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 1432ef9abdcSjv227347 return (ERANGE); 1442ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1457c478bd9Sstevel@tonic-gate errno = ERANGE; 1467c478bd9Sstevel@tonic-gate return (neg ? LONG_MIN : LONG_MAX); 1472ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1487c478bd9Sstevel@tonic-gate } 149