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