xref: /netbsd-src/lib/libc/gen/dehumanize_number.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: dehumanize_number.c,v 1.2 2007/12/14 17:32:47 xtraeme Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: dehumanize_number.c,v 1.2 2007/12/14 17:32:47 xtraeme Exp $");
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 #include <inttypes.h>
46 #include <ctype.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <limits.h>
51 
52 /*
53  * Converts the number given in 'str', which may be given in a humanized
54  * form (as described in humanize_number(3), but with some limitations),
55  * to an int64_t without units.
56  * In case of success, 0 is returned and *size holds the value.
57  * Otherwise, -1 is returned and *size is untouched.
58  *
59  * TODO: Internationalization, SI units.
60  */
61 int
62 dehumanize_number(const char *str, int64_t *size)
63 {
64 	char *ep, unit;
65 	const char *delimit;
66 	long multiplier;
67 	long long tmp, tmp2;
68 	size_t len;
69 
70 	len = strlen(str);
71 	if (len == 0) {
72 		errno = EINVAL;
73 		return -1;
74 	}
75 
76 	multiplier = 1;
77 
78 	unit = str[len - 1];
79 	if (isalpha((unsigned char)unit)) {
80 		switch (tolower((unsigned char)unit)) {
81 		case 'b':
82 			multiplier = 1;
83 			break;
84 
85 		case 'k':
86 			multiplier = 1024;
87 			break;
88 
89 		case 'm':
90 			multiplier = 1024 * 1024;
91 			break;
92 
93 		case 'g':
94 			multiplier = 1024 * 1024 * 1024;
95 			break;
96 
97 		default:
98 			errno = EINVAL;
99 			return -1; /* Invalid suffix. */
100 		}
101 
102 		delimit = &str[len - 1];
103 	} else
104 		delimit = NULL;
105 
106 	errno = 0;
107 	tmp = strtoll(str, &ep, 10);
108 	if (str[0] == '\0' || (ep != delimit && *ep != '\0'))
109 		return -1; /* Not a number. */
110 	else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
111 		return -1; /* Out of range. */
112 
113 	tmp2 = tmp * multiplier;
114 	tmp2 = tmp2 / multiplier;
115 	if (tmp != tmp2) {
116 		errno = ERANGE;
117 		return -1; /* Out of range. */
118 	}
119 	*size = tmp * multiplier;
120 
121 	return 0;
122 }
123