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