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