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