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