xref: /illumos-gate/usr/src/common/util/strtol.c (revision 6ffde572)
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 
22ae2ada9cSYakov Zaytsev /*
23ae2ada9cSYakov Zaytsev  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24ae2ada9cSYakov Zaytsev  */
25ae2ada9cSYakov Zaytsev 
267c478bd9Sstevel@tonic-gate /*
272ef9abdcSjv  * 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	*/
3228de4f3cSToomas Soome /*	  All Rights Reserved	*/
337257d1b4Sraf 
34*6ffde572SToomas Soome #if	defined(_KERNEL)
3528de4f3cSToomas Soome #include <sys/null.h>
36*6ffde572SToomas Soome #endif	/* _KERNEL */
37*6ffde572SToomas Soome 
38*6ffde572SToomas Soome #if	defined(_KERNEL) && !defined(_BOOT)
392ef9abdcSjv #include <sys/errno.h>
402ef9abdcSjv #else	/* _KERNEL && !_BOOT */
4128de4f3cSToomas Soome #if	!defined(_BOOT) && !defined(_KMDB) && !defined(_STANDALONE)
427257d1b4Sraf #include "lint.h"
4328de4f3cSToomas Soome #endif	/* !_BOOT && !_KMDB && !_STANDALONE */
4428de4f3cSToomas Soome #if	defined(_STANDALONE)
4528de4f3cSToomas Soome #include <sys/cdefs.h>
4628de4f3cSToomas Soome #include <stand.h>
4728de4f3cSToomas Soome #include <limits.h>
4828de4f3cSToomas Soome #else
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
5328de4f3cSToomas Soome #endif	/* _STANDALONE */
542ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
552ef9abdcSjv #include "strtolctype.h"
562ef9abdcSjv #include <sys/types.h>
577c478bd9Sstevel@tonic-gate 
582ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
592ef9abdcSjv int
ddi_strtol(const char * str,char ** nptr,int base,long * result)602ef9abdcSjv ddi_strtol(const char *str, char **nptr, int base, long *result)
612ef9abdcSjv #else	/* _KERNEL && !_BOOT */
627c478bd9Sstevel@tonic-gate long
637c478bd9Sstevel@tonic-gate strtol(const char *str, char **nptr, int base)
642ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	long val;
677c478bd9Sstevel@tonic-gate 	int c;
682ef9abdcSjv 	int xx;
692ef9abdcSjv 	int neg = 0;
702ef9abdcSjv 	long multmin;
712ef9abdcSjv 	long limit;
727c478bd9Sstevel@tonic-gate 	const char **ptr = (const char **)nptr;
732ef9abdcSjv 	const unsigned char *ustr = (const unsigned char *)str;
747c478bd9Sstevel@tonic-gate 
7528de4f3cSToomas Soome 	if (ptr != NULL)
767c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr; /* in case no number is formed */
777c478bd9Sstevel@tonic-gate 	if (base < 0 || base > MBASE || base == 1) {
782ef9abdcSjv 		/* base is invalid -- should be a fatal error */
792ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
802ef9abdcSjv 		return (EINVAL);
812ef9abdcSjv #else	/* _KERNEL && !_BOOT */
827c478bd9Sstevel@tonic-gate 		errno = EINVAL;
832ef9abdcSjv 		return (0);
842ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	if (!isalnum(c = *ustr)) {
877c478bd9Sstevel@tonic-gate 		while (isspace(c))
887c478bd9Sstevel@tonic-gate 			c = *++ustr;
897c478bd9Sstevel@tonic-gate 		switch (c) {
907c478bd9Sstevel@tonic-gate 		case '-':
917c478bd9Sstevel@tonic-gate 			neg++;
927c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
937c478bd9Sstevel@tonic-gate 		case '+':
947c478bd9Sstevel@tonic-gate 			c = *++ustr;
957c478bd9Sstevel@tonic-gate 		}
967c478bd9Sstevel@tonic-gate 	}
9728de4f3cSToomas Soome 	if (base == 0) {
987c478bd9Sstevel@tonic-gate 		if (c != '0')
997c478bd9Sstevel@tonic-gate 			base = 10;
1007c478bd9Sstevel@tonic-gate 		else if (ustr[1] == 'x' || ustr[1] == 'X')
1017c478bd9Sstevel@tonic-gate 			base = 16;
1027c478bd9Sstevel@tonic-gate 		else
1037c478bd9Sstevel@tonic-gate 			base = 8;
10428de4f3cSToomas Soome 	}
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * for any base > 10, the digits incrementally following
1077c478bd9Sstevel@tonic-gate 	 *	9 are assumed to be "abc...z" or "ABC...Z"
1087c478bd9Sstevel@tonic-gate 	 */
1092ef9abdcSjv 	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
1102ef9abdcSjv 		/* no number formed */
1112ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1122ef9abdcSjv 		return (EINVAL);
1132ef9abdcSjv #else	/* _KERNEL && !_BOOT */
114ae2ada9cSYakov Zaytsev 		errno = EINVAL;
1152ef9abdcSjv 		return (0);
1162ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1172ef9abdcSjv 	}
1187c478bd9Sstevel@tonic-gate 	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
1197257d1b4Sraf 	    isxdigit(ustr[2]))
1207c478bd9Sstevel@tonic-gate 		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* this code assumes that abs(LONG_MIN) >= abs(LONG_MAX) */
1237c478bd9Sstevel@tonic-gate 	if (neg)
1247c478bd9Sstevel@tonic-gate 		limit = LONG_MIN;
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		limit = -LONG_MAX;
1277c478bd9Sstevel@tonic-gate 	multmin = limit / (long)base;
1287c478bd9Sstevel@tonic-gate 	val = -DIGIT(c);
1297c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
1302ef9abdcSjv 		/* accumulate neg avoids surprises near LONG_MAX */
1317c478bd9Sstevel@tonic-gate 		if (val < multmin)
1327c478bd9Sstevel@tonic-gate 			goto overflow;
1337c478bd9Sstevel@tonic-gate 		val *= base;
1347c478bd9Sstevel@tonic-gate 		if (val < limit + xx)
1357c478bd9Sstevel@tonic-gate 			goto overflow;
1367c478bd9Sstevel@tonic-gate 		val -= xx;
1377c478bd9Sstevel@tonic-gate 		c = *++ustr;
1387c478bd9Sstevel@tonic-gate 	}
13928de4f3cSToomas Soome 	if (ptr != NULL)
1407c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
1412ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1422ef9abdcSjv 	*result = neg ? val : -val;
1432ef9abdcSjv 	return (0);
1442ef9abdcSjv #else	/* _KERNEL && !_BOOT */
1457c478bd9Sstevel@tonic-gate 	return (neg ? val : -val);
1462ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate overflow:
1497c478bd9Sstevel@tonic-gate 	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
1507c478bd9Sstevel@tonic-gate 		;
15128de4f3cSToomas Soome 	if (ptr != NULL)
1527c478bd9Sstevel@tonic-gate 		*ptr = (char *)ustr;
1532ef9abdcSjv #if	defined(_KERNEL) && !defined(_BOOT)
1542ef9abdcSjv 	return (ERANGE);
1552ef9abdcSjv #else	/* _KERNEL && !_BOOT */
1567c478bd9Sstevel@tonic-gate 	errno = ERANGE;
1577c478bd9Sstevel@tonic-gate 	return (neg ? LONG_MIN : LONG_MAX);
1582ef9abdcSjv #endif	/* _KERNEL && !_BOOT */
1597c478bd9Sstevel@tonic-gate }
160