17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
232ef9abdjv * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
302ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
312ef9abdjv#include <sys/errno.h>
322ef9abdjv#else	/* _KERNEL && !_BOOT */
332ef9abdjv#if	!defined(_BOOT) && !defined(_KMDB)
347257d1braf#include "lint.h"
352ef9abdjv#endif	/* !_BOOT && !_KMDB */
367c478bdstevel@tonic-gate#include <errno.h>
377c478bdstevel@tonic-gate#include <ctype.h>
387c478bdstevel@tonic-gate#include <limits.h>
397c478bdstevel@tonic-gate#include <stdlib.h>
402ef9abdjv#endif	/* _KERNEL && !_BOOT */
412ef9abdjv#include "strtolctype.h"
422ef9abdjv#include <sys/types.h>
437c478bdstevel@tonic-gate
442ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
452ef9abdjvint
462ef9abdjvddi_strtoll(const char *str, char **nptr, int base, longlong_t *result)
472ef9abdjv#else	/* _KERNEL && !_BOOT */
487c478bdstevel@tonic-gatelonglong_t
497c478bdstevel@tonic-gatestrtoll(const char *str, char **nptr, int base)
502ef9abdjv#endif	/* _KERNEL && !_BOOT */
517c478bdstevel@tonic-gate{
527c478bdstevel@tonic-gate	longlong_t val;
537c478bdstevel@tonic-gate	int c;
542ef9abdjv	int xx;
552ef9abdjv	int neg = 0;
562ef9abdjv	longlong_t multmin;
572ef9abdjv	longlong_t limit;
587c478bdstevel@tonic-gate	const char **ptr = (const char **)nptr;
592ef9abdjv	const unsigned char *ustr = (const unsigned char *)str;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	if (ptr != (const char **)0)
627c478bdstevel@tonic-gate		*ptr = (char *)ustr; /* in case no number is formed */
637c478bdstevel@tonic-gate	if (base < 0 || base > MBASE || base == 1) {
642ef9abdjv		/* base is invalid -- should be a fatal error */
652ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
662ef9abdjv		return (EINVAL);
672ef9abdjv#else	/* _KERNEL && !_BOOT */
687c478bdstevel@tonic-gate		errno = EINVAL;
692ef9abdjv		return (0);
702ef9abdjv#endif	/* _KERNEL && !_BOOT */
717c478bdstevel@tonic-gate	}
727c478bdstevel@tonic-gate	if (!isalnum(c = *ustr)) {
737c478bdstevel@tonic-gate		while (isspace(c))
747c478bdstevel@tonic-gate			c = *++ustr;
757c478bdstevel@tonic-gate		switch (c) {
767c478bdstevel@tonic-gate		case '-':
777c478bdstevel@tonic-gate			neg++;
787c478bdstevel@tonic-gate			/* FALLTHROUGH */
797c478bdstevel@tonic-gate		case '+':
807c478bdstevel@tonic-gate			c = *++ustr;
817c478bdstevel@tonic-gate		}
827c478bdstevel@tonic-gate	}
837c478bdstevel@tonic-gate	if (base == 0)
847c478bdstevel@tonic-gate		if (c != '0')
857c478bdstevel@tonic-gate			base = 10;
867c478bdstevel@tonic-gate		else if (ustr[1] == 'x' || ustr[1] == 'X')
877c478bdstevel@tonic-gate			base = 16;
887c478bdstevel@tonic-gate		else
897c478bdstevel@tonic-gate			base = 8;
907c478bdstevel@tonic-gate	/*
917c478bdstevel@tonic-gate	 * for any base > 10, the digits incrementally following
927c478bdstevel@tonic-gate	 *	9 are assumed to be "abc...z" or "ABC...Z"
937c478bdstevel@tonic-gate	 */
942ef9abdjv	if (!lisalnum(c) || (xx = DIGIT(c)) >= base) {
952ef9abdjv		/* no number formed */
962ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
972ef9abdjv		return (EINVAL);
982ef9abdjv#else	/* _KERNEL && !_BOOT */
992ef9abdjv		return (0);
1002ef9abdjv#endif	/* _KERNEL && !_BOOT */
1012ef9abdjv	}
1027c478bdstevel@tonic-gate	if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
1037257d1braf	    isxdigit(ustr[2]))
1047c478bdstevel@tonic-gate		c = *(ustr += 2); /* skip over leading "0x" or "0X" */
1057c478bdstevel@tonic-gate
1062ef9abdjv	/* this code assumes that abs(LLONG_MIN) >= abs(LLONG_MAX) */
1077c478bdstevel@tonic-gate	if (neg)
1082ef9abdjv		limit = LLONG_MIN;
1097c478bdstevel@tonic-gate	else
1102ef9abdjv		limit = -LLONG_MAX;
1112ef9abdjv	multmin = limit / (longlong_t)base;
1127c478bdstevel@tonic-gate	val = -DIGIT(c);
1137c478bdstevel@tonic-gate	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
1142ef9abdjv		/* accumulate neg avoids surprises near LLONG_MAX */
1157c478bdstevel@tonic-gate		if (val < multmin)
1167c478bdstevel@tonic-gate			goto overflow;
1177c478bdstevel@tonic-gate		val *= base;
1187c478bdstevel@tonic-gate		if (val < limit + xx)
1197c478bdstevel@tonic-gate			goto overflow;
1207c478bdstevel@tonic-gate		val -= xx;
1217c478bdstevel@tonic-gate		c = *++ustr;
1227c478bdstevel@tonic-gate	}
1237c478bdstevel@tonic-gate	if (ptr != (const char **)0)
1247c478bdstevel@tonic-gate		*ptr = (char *)ustr;
1252ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
1262ef9abdjv	*result = neg ? val : -val;
1272ef9abdjv	return (0);
1282ef9abdjv#else	/* _KERNEL && !_BOOT */
1297c478bdstevel@tonic-gate	return (neg ? val : -val);
1302ef9abdjv#endif	/* _KERNEL && !_BOOT */
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gateoverflow:
1337c478bdstevel@tonic-gate	for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
1347c478bdstevel@tonic-gate		;
1357c478bdstevel@tonic-gate	if (ptr != (const char **)0)
1367c478bdstevel@tonic-gate		*ptr = (char *)ustr;
1372ef9abdjv#if	defined(_KERNEL) && !defined(_BOOT)
1382ef9abdjv	return (ERANGE);
1392ef9abdjv#else	/* _KERNEL && !_BOOT */
1407c478bdstevel@tonic-gate	errno = ERANGE;
1412ef9abdjv	return (neg ? LLONG_MIN : LLONG_MAX);
1422ef9abdjv#endif	/* _KERNEL && !_BOOT */
1437c478bdstevel@tonic-gate}
144