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