xref: /minix3/lib/libc/stdlib/strsuftoll.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $	*/
22fe8fb19SBen Gras /*-
32fe8fb19SBen Gras  * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
42fe8fb19SBen Gras  * All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
72fe8fb19SBen Gras  * by Luke Mewburn.
82fe8fb19SBen Gras  *
92fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
102fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
112fe8fb19SBen Gras  * are met:
122fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
142fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
152fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
162fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
192fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
202fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
212fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
222fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras /*-
312fe8fb19SBen Gras  * Copyright (c) 1991, 1993, 1994
322fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
332fe8fb19SBen Gras  *
342fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
352fe8fb19SBen Gras  * Keith Muller of the University of California, San Diego and Lance
362fe8fb19SBen Gras  * Visser of Convex Computer Corporation.
372fe8fb19SBen Gras  *
382fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
392fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
402fe8fb19SBen Gras  * are met:
412fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
422fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
432fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
442fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
452fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
462fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
472fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
482fe8fb19SBen Gras  *    without specific prior written permission.
492fe8fb19SBen Gras  *
502fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
512fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
522fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
532fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
542fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
552fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
562fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
572fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
582fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
592fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
602fe8fb19SBen Gras  * SUCH DAMAGE.
612fe8fb19SBen Gras  */
622fe8fb19SBen Gras 
632fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
642fe8fb19SBen Gras #include "nbtool_config.h"
652fe8fb19SBen Gras #endif
662fe8fb19SBen Gras 
672fe8fb19SBen Gras #include <sys/cdefs.h>
682fe8fb19SBen Gras 
692fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
70*f14fb602SLionel Sambuc __RCSID("$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $");
712fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
722fe8fb19SBen Gras 
732fe8fb19SBen Gras #ifdef _LIBC
742fe8fb19SBen Gras #include "namespace.h"
752fe8fb19SBen Gras #endif
762fe8fb19SBen Gras 
772fe8fb19SBen Gras #if !HAVE_STRSUFTOLL
782fe8fb19SBen Gras 
792fe8fb19SBen Gras #include <sys/types.h>
802fe8fb19SBen Gras #include <sys/time.h>
812fe8fb19SBen Gras 
822fe8fb19SBen Gras #include <assert.h>
832fe8fb19SBen Gras #include <ctype.h>
842fe8fb19SBen Gras #include <err.h>
852fe8fb19SBen Gras #include <errno.h>
862fe8fb19SBen Gras #include <limits.h>
872fe8fb19SBen Gras #include <stdio.h>
882fe8fb19SBen Gras #include <stdlib.h>
892fe8fb19SBen Gras #include <string.h>
902fe8fb19SBen Gras 
912fe8fb19SBen Gras #ifdef _LIBC
922fe8fb19SBen Gras # ifdef __weak_alias
__weak_alias(strsuftoll,_strsuftoll)932fe8fb19SBen Gras __weak_alias(strsuftoll, _strsuftoll)
942fe8fb19SBen Gras __weak_alias(strsuftollx, _strsuftollx)
952fe8fb19SBen Gras # endif
962fe8fb19SBen Gras #endif /* LIBC */
972fe8fb19SBen Gras 
982fe8fb19SBen Gras /*
992fe8fb19SBen Gras  * Convert an expression of the following forms to a (u)int64_t.
1002fe8fb19SBen Gras  * 	1) A positive decimal number.
1012fe8fb19SBen Gras  *	2) A positive decimal number followed by a b (mult by 512).
1022fe8fb19SBen Gras  *	3) A positive decimal number followed by a k (mult by 1024).
1032fe8fb19SBen Gras  *	4) A positive decimal number followed by a m (mult by 1048576).
1042fe8fb19SBen Gras  *	5) A positive decimal number followed by a g (mult by 1073741824).
1052fe8fb19SBen Gras  *	6) A positive decimal number followed by a t (mult by 1099511627776).
1062fe8fb19SBen Gras  *	7) A positive decimal number followed by a w (mult by sizeof int)
1072fe8fb19SBen Gras  *	8) Two or more positive decimal numbers (with/without k,b or w).
1082fe8fb19SBen Gras  *	   separated by x (also * for backwards compatibility), specifying
1092fe8fb19SBen Gras  *	   the product of the indicated values.
1102fe8fb19SBen Gras  * Returns the result upon successful conversion, or exits with an
1112fe8fb19SBen Gras  * appropriate error.
1122fe8fb19SBen Gras  *
1132fe8fb19SBen Gras  */
1142fe8fb19SBen Gras /* LONGLONG */
1152fe8fb19SBen Gras long long
1162fe8fb19SBen Gras strsuftoll(const char *desc, const char *val,
1172fe8fb19SBen Gras     long long min, long long max)
1182fe8fb19SBen Gras {
1192fe8fb19SBen Gras 	long long result;
1202fe8fb19SBen Gras 	char	errbuf[100];
1212fe8fb19SBen Gras 
1222fe8fb19SBen Gras 	result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
1232fe8fb19SBen Gras 	if (*errbuf != '\0')
124*f14fb602SLionel Sambuc 		errx(EXIT_FAILURE, "%s", errbuf);
125*f14fb602SLionel Sambuc 	return result;
1262fe8fb19SBen Gras }
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras /*
1292fe8fb19SBen Gras  * As strsuftoll(), but returns the error message into the provided buffer
1302fe8fb19SBen Gras  * rather than exiting with it.
1312fe8fb19SBen Gras  */
1322fe8fb19SBen Gras /* LONGLONG */
133*f14fb602SLionel Sambuc static long long
__strsuftollx(const char * desc,const char * val,long long min,long long max,char * ebuf,size_t ebuflen,size_t depth)134*f14fb602SLionel Sambuc __strsuftollx(const char *desc, const char *val,
135*f14fb602SLionel Sambuc     long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
1362fe8fb19SBen Gras {
1372fe8fb19SBen Gras 	long long num, t;
1382fe8fb19SBen Gras 	char	*expr;
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 	_DIAGASSERT(desc != NULL);
1412fe8fb19SBen Gras 	_DIAGASSERT(val != NULL);
1422fe8fb19SBen Gras 	_DIAGASSERT(ebuf != NULL);
1432fe8fb19SBen Gras 
144*f14fb602SLionel Sambuc 	if (depth > 16) {
145*f14fb602SLionel Sambuc 		snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
146*f14fb602SLionel Sambuc 		return 0;
147*f14fb602SLionel Sambuc 	}
1482fe8fb19SBen Gras 
1492fe8fb19SBen Gras 	while (isspace((unsigned char)*val))	/* Skip leading space */
1502fe8fb19SBen Gras 		val++;
1512fe8fb19SBen Gras 
152*f14fb602SLionel Sambuc 	errno = 0;
1532fe8fb19SBen Gras 	num = strtoll(val, &expr, 10);
1542fe8fb19SBen Gras 	if (errno == ERANGE)
1552fe8fb19SBen Gras 		goto erange;			/* Overflow */
1562fe8fb19SBen Gras 
1572fe8fb19SBen Gras 	if (expr == val)			/* No digits */
1582fe8fb19SBen Gras 		goto badnum;
1592fe8fb19SBen Gras 
1602fe8fb19SBen Gras 	switch (*expr) {
1612fe8fb19SBen Gras 	case 'b':
1622fe8fb19SBen Gras 		t = num;
1632fe8fb19SBen Gras 		num *= 512;			/* 1 block */
1642fe8fb19SBen Gras 		if (t > num)
1652fe8fb19SBen Gras 			goto erange;
1662fe8fb19SBen Gras 		++expr;
1672fe8fb19SBen Gras 		break;
1682fe8fb19SBen Gras 	case 'k':
1692fe8fb19SBen Gras 		t = num;
1702fe8fb19SBen Gras 		num *= 1024;			/* 1 kibibyte */
1712fe8fb19SBen Gras 		if (t > num)
1722fe8fb19SBen Gras 			goto erange;
1732fe8fb19SBen Gras 		++expr;
1742fe8fb19SBen Gras 		break;
1752fe8fb19SBen Gras 	case 'm':
1762fe8fb19SBen Gras 		t = num;
1772fe8fb19SBen Gras 		num *= 1048576;			/* 1 mebibyte */
1782fe8fb19SBen Gras 		if (t > num)
1792fe8fb19SBen Gras 			goto erange;
1802fe8fb19SBen Gras 		++expr;
1812fe8fb19SBen Gras 		break;
1822fe8fb19SBen Gras 	case 'g':
1832fe8fb19SBen Gras 		t = num;
1842fe8fb19SBen Gras 		num *= 1073741824;		/* 1 gibibyte */
1852fe8fb19SBen Gras 		if (t > num)
1862fe8fb19SBen Gras 			goto erange;
1872fe8fb19SBen Gras 		++expr;
1882fe8fb19SBen Gras 		break;
1892fe8fb19SBen Gras 	case 't':
1902fe8fb19SBen Gras 		t = num;
1912fe8fb19SBen Gras 		num *= 1099511627776LL;		/* 1 tebibyte */
1922fe8fb19SBen Gras 		if (t > num)
1932fe8fb19SBen Gras 			goto erange;
1942fe8fb19SBen Gras 		++expr;
1952fe8fb19SBen Gras 		break;
1962fe8fb19SBen Gras 	case 'w':
1972fe8fb19SBen Gras 		t = num;
1982fe8fb19SBen Gras 		num *= sizeof(int);		/* 1 word */
1992fe8fb19SBen Gras 		if (t > num)
2002fe8fb19SBen Gras 			goto erange;
2012fe8fb19SBen Gras 		++expr;
2022fe8fb19SBen Gras 		break;
2032fe8fb19SBen Gras 	}
2042fe8fb19SBen Gras 
2052fe8fb19SBen Gras 	switch (*expr) {
2062fe8fb19SBen Gras 	case '\0':
2072fe8fb19SBen Gras 		break;
2082fe8fb19SBen Gras 	case '*':				/* Backward compatible */
2092fe8fb19SBen Gras 	case 'x':
2102fe8fb19SBen Gras 		t = num;
211*f14fb602SLionel Sambuc 		num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
212*f14fb602SLionel Sambuc 			depth + 1);
2132fe8fb19SBen Gras 		if (*ebuf != '\0')
214*f14fb602SLionel Sambuc 			return 0;
2152fe8fb19SBen Gras 		if (t > num) {
2162fe8fb19SBen Gras  erange:
217*f14fb602SLionel Sambuc 			errno = ERANGE;
218*f14fb602SLionel Sambuc 			snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
219*f14fb602SLionel Sambuc 			return 0;
2202fe8fb19SBen Gras 		}
2212fe8fb19SBen Gras 		break;
2222fe8fb19SBen Gras 	default:
223*f14fb602SLionel Sambuc  badnum:
224*f14fb602SLionel Sambuc 		snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
225*f14fb602SLionel Sambuc 		return 0;
2262fe8fb19SBen Gras 	}
2272fe8fb19SBen Gras 	if (num < min) {
2282fe8fb19SBen Gras 		/* LONGLONG */
2292fe8fb19SBen Gras 		snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
2302fe8fb19SBen Gras 		    desc, (long long)num, (long long)min);
231*f14fb602SLionel Sambuc 		return 0;
2322fe8fb19SBen Gras 	}
2332fe8fb19SBen Gras 	if (num > max) {
2342fe8fb19SBen Gras 		/* LONGLONG */
235*f14fb602SLionel Sambuc 		snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
2362fe8fb19SBen Gras 		    desc, (long long)num, (long long)max);
237*f14fb602SLionel Sambuc 		return 0;
2382fe8fb19SBen Gras 	}
2392fe8fb19SBen Gras 	*ebuf = '\0';
240*f14fb602SLionel Sambuc 	return num;
2412fe8fb19SBen Gras }
2422fe8fb19SBen Gras 
243*f14fb602SLionel Sambuc long long
strsuftollx(const char * desc,const char * val,long long min,long long max,char * ebuf,size_t ebuflen)244*f14fb602SLionel Sambuc strsuftollx(const char *desc, const char *val,
245*f14fb602SLionel Sambuc     long long min, long long max, char *ebuf, size_t ebuflen)
246*f14fb602SLionel Sambuc {
247*f14fb602SLionel Sambuc 	return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
248*f14fb602SLionel Sambuc }
2492fe8fb19SBen Gras #endif /* !HAVE_STRSUFTOLL */
250