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*2ef9abdcSjv227347 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277257d1b4Sraf /* Copyright (c) 1988 AT&T */ 287257d1b4Sraf /* All Rights Reserved */ 297257d1b4Sraf 30*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 31*2ef9abdcSjv227347 #include <sys/errno.h> 32*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 337c478bd9Sstevel@tonic-gate #if !defined(_BOOT) && !defined(_KMDB) 347257d1b4Sraf #include "lint.h" 357c478bd9Sstevel@tonic-gate #endif /* !_BOOT && !_KMDB */ 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <ctype.h> 387c478bd9Sstevel@tonic-gate #include <limits.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 41*2ef9abdcSjv227347 #include "strtolctype.h" 42*2ef9abdcSjv227347 #include <sys/types.h> 437c478bd9Sstevel@tonic-gate 44*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 45*2ef9abdcSjv227347 int 46*2ef9abdcSjv227347 ddi_strtol(const char *str, char **nptr, int base, long *result) 47*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 487c478bd9Sstevel@tonic-gate long 497c478bd9Sstevel@tonic-gate strtol(const char *str, char **nptr, int base) 50*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate long val; 537c478bd9Sstevel@tonic-gate int c; 54*2ef9abdcSjv227347 int xx; 55*2ef9abdcSjv227347 int neg = 0; 567c478bd9Sstevel@tonic-gate long multmin; 577c478bd9Sstevel@tonic-gate long limit; 587c478bd9Sstevel@tonic-gate const char **ptr = (const char **)nptr; 597c478bd9Sstevel@tonic-gate const unsigned char *ustr = (const unsigned char *)str; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 627c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; /* in case no number is formed */ 637c478bd9Sstevel@tonic-gate if (base < 0 || base > MBASE || base == 1) { 64*2ef9abdcSjv227347 /* base is invalid -- should be a fatal error */ 65*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 66*2ef9abdcSjv227347 return (EINVAL); 67*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 687c478bd9Sstevel@tonic-gate errno = EINVAL; 69*2ef9abdcSjv227347 return (0); 70*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate if (!isalnum(c = *ustr)) { 737c478bd9Sstevel@tonic-gate while (isspace(c)) 747c478bd9Sstevel@tonic-gate c = *++ustr; 757c478bd9Sstevel@tonic-gate switch (c) { 767c478bd9Sstevel@tonic-gate case '-': 777c478bd9Sstevel@tonic-gate neg++; 787c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 797c478bd9Sstevel@tonic-gate case '+': 807c478bd9Sstevel@tonic-gate c = *++ustr; 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate if (base == 0) 847c478bd9Sstevel@tonic-gate if (c != '0') 857c478bd9Sstevel@tonic-gate base = 10; 867c478bd9Sstevel@tonic-gate else if (ustr[1] == 'x' || ustr[1] == 'X') 877c478bd9Sstevel@tonic-gate base = 16; 887c478bd9Sstevel@tonic-gate else 897c478bd9Sstevel@tonic-gate base = 8; 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * for any base > 10, the digits incrementally following 927c478bd9Sstevel@tonic-gate * 9 are assumed to be "abc...z" or "ABC...Z" 937c478bd9Sstevel@tonic-gate */ 94*2ef9abdcSjv227347 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) { 95*2ef9abdcSjv227347 /* no number formed */ 96*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 97*2ef9abdcSjv227347 return (EINVAL); 98*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 99*2ef9abdcSjv227347 return (0); 100*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 101*2ef9abdcSjv227347 } 1027c478bd9Sstevel@tonic-gate if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 1037c478bd9Sstevel@tonic-gate isxdigit(ustr[2])) 1047c478bd9Sstevel@tonic-gate c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */ 1077c478bd9Sstevel@tonic-gate if (neg) 1087c478bd9Sstevel@tonic-gate limit = LONG_MIN; 1097c478bd9Sstevel@tonic-gate else 1107c478bd9Sstevel@tonic-gate limit = -LONG_MAX; 1117c478bd9Sstevel@tonic-gate multmin = limit / (long)base; 1127c478bd9Sstevel@tonic-gate val = -DIGIT(c); 1137c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 114*2ef9abdcSjv227347 /* accumulate neg avoids surprises near LONG_MAX */ 1157c478bd9Sstevel@tonic-gate if (val < multmin) 1167c478bd9Sstevel@tonic-gate goto overflow; 1177c478bd9Sstevel@tonic-gate val *= base; 1187c478bd9Sstevel@tonic-gate if (val < limit + xx) 1197c478bd9Sstevel@tonic-gate goto overflow; 1207c478bd9Sstevel@tonic-gate val -= xx; 1217c478bd9Sstevel@tonic-gate c = *++ustr; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1247c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 125*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 126*2ef9abdcSjv227347 *result = neg ? val : -val; 127*2ef9abdcSjv227347 return (0); 128*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1297c478bd9Sstevel@tonic-gate return (neg ? val : -val); 130*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate overflow: 1337c478bd9Sstevel@tonic-gate for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 1347c478bd9Sstevel@tonic-gate ; 1357c478bd9Sstevel@tonic-gate if (ptr != (const char **)0) 1367c478bd9Sstevel@tonic-gate *ptr = (char *)ustr; 137*2ef9abdcSjv227347 #if defined(_KERNEL) && !defined(_BOOT) 138*2ef9abdcSjv227347 return (ERANGE); 139*2ef9abdcSjv227347 #else /* _KERNEL && !_BOOT */ 1407c478bd9Sstevel@tonic-gate errno = ERANGE; 1417c478bd9Sstevel@tonic-gate return (neg ? LONG_MIN : LONG_MAX); 142*2ef9abdcSjv227347 #endif /* _KERNEL && !_BOOT */ 1437c478bd9Sstevel@tonic-gate } 144