xref: /minix3/lib/libc/gen/dehumanize_number.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dehumanize_number.c,v 1.7 2014/10/01 13:53:04 apb Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
92fe8fb19SBen Gras  * 2005 program.
102fe8fb19SBen Gras  *
112fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
122fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
132fe8fb19SBen Gras  * are met:
142fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
152fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
162fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
172fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
182fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
192fe8fb19SBen Gras  *
202fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
212fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
222fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
232fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
242fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
312fe8fb19SBen Gras  */
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc #ifdef HAVE_NBTOOL_CONFIG_H
34*0a6a1f1dSLionel Sambuc #include "nbtool_config.h"
35*0a6a1f1dSLionel Sambuc #endif /* HAVE_NBTOOL_CONFIG_H */
36*0a6a1f1dSLionel Sambuc 
372fe8fb19SBen Gras #include <sys/cdefs.h>
382fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
39*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: dehumanize_number.c,v 1.7 2014/10/01 13:53:04 apb Exp $");
402fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
412fe8fb19SBen Gras 
422fe8fb19SBen Gras #include "namespace.h"
43f14fb602SLionel Sambuc #include <assert.h>
442fe8fb19SBen Gras #include <inttypes.h>
452fe8fb19SBen Gras #include <ctype.h>
462fe8fb19SBen Gras #include <stdlib.h>
472fe8fb19SBen Gras #include <string.h>
482fe8fb19SBen Gras #include <errno.h>
492fe8fb19SBen Gras #include <limits.h>
502fe8fb19SBen Gras 
512fe8fb19SBen Gras /*
522fe8fb19SBen Gras  * Converts the number given in 'str', which may be given in a humanized
532fe8fb19SBen Gras  * form (as described in humanize_number(3), but with some limitations),
542fe8fb19SBen Gras  * to an int64_t without units.
552fe8fb19SBen Gras  * In case of success, 0 is returned and *size holds the value.
562fe8fb19SBen Gras  * Otherwise, -1 is returned and *size is untouched.
572fe8fb19SBen Gras  *
582fe8fb19SBen Gras  * TODO: Internationalization, SI units.
592fe8fb19SBen Gras  */
602fe8fb19SBen Gras int
dehumanize_number(const char * str,int64_t * size)612fe8fb19SBen Gras dehumanize_number(const char *str, int64_t *size)
622fe8fb19SBen Gras {
632fe8fb19SBen Gras 	char *ep, unit;
642fe8fb19SBen Gras 	const char *delimit;
652fe8fb19SBen Gras 	long multiplier;
662fe8fb19SBen Gras 	long long tmp, tmp2;
672fe8fb19SBen Gras 	size_t len;
682fe8fb19SBen Gras 
692fe8fb19SBen Gras 	len = strlen(str);
702fe8fb19SBen Gras 	if (len == 0) {
712fe8fb19SBen Gras 		errno = EINVAL;
722fe8fb19SBen Gras 		return -1;
732fe8fb19SBen Gras 	}
742fe8fb19SBen Gras 
752fe8fb19SBen Gras 	multiplier = 1;
762fe8fb19SBen Gras 
772fe8fb19SBen Gras 	unit = str[len - 1];
782fe8fb19SBen Gras 	if (isalpha((unsigned char)unit)) {
792fe8fb19SBen Gras 		switch (tolower((unsigned char)unit)) {
802fe8fb19SBen Gras 		case 'b':
812fe8fb19SBen Gras 			multiplier = 1;
822fe8fb19SBen Gras 			break;
832fe8fb19SBen Gras 
842fe8fb19SBen Gras 		case 'k':
852fe8fb19SBen Gras 			multiplier = 1024;
862fe8fb19SBen Gras 			break;
872fe8fb19SBen Gras 
882fe8fb19SBen Gras 		case 'm':
892fe8fb19SBen Gras 			multiplier = 1024 * 1024;
902fe8fb19SBen Gras 			break;
912fe8fb19SBen Gras 
922fe8fb19SBen Gras 		case 'g':
932fe8fb19SBen Gras 			multiplier = 1024 * 1024 * 1024;
942fe8fb19SBen Gras 			break;
952fe8fb19SBen Gras 
962fe8fb19SBen Gras 		default:
972fe8fb19SBen Gras 			errno = EINVAL;
982fe8fb19SBen Gras 			return -1; /* Invalid suffix. */
992fe8fb19SBen Gras 		}
1002fe8fb19SBen Gras 
1012fe8fb19SBen Gras 		delimit = &str[len - 1];
1022fe8fb19SBen Gras 	} else
1032fe8fb19SBen Gras 		delimit = NULL;
1042fe8fb19SBen Gras 
1052fe8fb19SBen Gras 	errno = 0;
1062fe8fb19SBen Gras 	tmp = strtoll(str, &ep, 10);
1072fe8fb19SBen Gras 	if (str[0] == '\0' || (ep != delimit && *ep != '\0'))
1082fe8fb19SBen Gras 		return -1; /* Not a number. */
1092fe8fb19SBen Gras 	else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
1102fe8fb19SBen Gras 		return -1; /* Out of range. */
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras 	tmp2 = tmp * multiplier;
1132fe8fb19SBen Gras 	tmp2 = tmp2 / multiplier;
1142fe8fb19SBen Gras 	if (tmp != tmp2) {
1152fe8fb19SBen Gras 		errno = ERANGE;
1162fe8fb19SBen Gras 		return -1; /* Out of range. */
1172fe8fb19SBen Gras 	}
118f14fb602SLionel Sambuc 	tmp *= multiplier;
119*0a6a1f1dSLionel Sambuc #ifdef _DIAGASSERT
120f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int64_t, tmp));
121*0a6a1f1dSLionel Sambuc #endif
122f14fb602SLionel Sambuc 	*size = (int64_t)tmp;
1232fe8fb19SBen Gras 
1242fe8fb19SBen Gras 	return 0;
1252fe8fb19SBen Gras }
126