expand_number.c revision 180347
1172029Spjd/*-
2172029Spjd * Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
3172029Spjd * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4172029Spjd * All rights reserved.
5172029Spjd *
6172029Spjd * Redistribution and use in source and binary forms, with or without
7172029Spjd * modification, are permitted provided that the following conditions
8172029Spjd * are met:
9172029Spjd * 1. Redistributions of source code must retain the above copyright
10172029Spjd *    notice, this list of conditions and the following disclaimer.
11172029Spjd * 2. Redistributions in binary form must reproduce the above copyright
12172029Spjd *    notice, this list of conditions and the following disclaimer in the
13172029Spjd *    documentation and/or other materials provided with the distribution.
14172029Spjd *
15172029Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16172029Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17172029Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18172029Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19172029Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20172029Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21172029Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22172029Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23172029Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24172029Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25172029Spjd * SUCH DAMAGE.
26172029Spjd */
27172029Spjd
28172029Spjd#include <sys/cdefs.h>
29172029Spjd__FBSDID("$FreeBSD: head/lib/libutil/expand_number.c 180347 2008-07-07 12:20:34Z kib $");
30172029Spjd
31172029Spjd#include <sys/types.h>
32172029Spjd#include <ctype.h>
33172029Spjd#include <errno.h>
34180347Skib#include <inttypes.h>
35172029Spjd#include <libutil.h>
36172029Spjd#include <stdint.h>
37172029Spjd
38172029Spjd/*
39172029Spjd * Convert an expression of the following forms to a int64_t.
40172029Spjd * 	1) A positive decimal number.
41172029Spjd *	2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
42172029Spjd *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
43172029Spjd *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
44172029Spjd *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
45172029Spjd *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
46172029Spjd *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
47172029Spjd *	8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
48172029Spjd */
49172029Spjdint
50173719Sjbexpand_number(const char *buf, int64_t *num)
51172029Spjd{
52172029Spjd	static const char unit[] = "bkmgtpe";
53172029Spjd	char *endptr, s;
54172029Spjd	int64_t number;
55172029Spjd	int i;
56172029Spjd
57172029Spjd	number = strtoimax(buf, &endptr, 0);
58172029Spjd
59172029Spjd	if (endptr == buf) {
60172029Spjd		/* No valid digits. */
61172029Spjd		errno = EINVAL;
62172029Spjd		return (-1);
63172029Spjd	}
64172029Spjd
65172029Spjd	if (*endptr == '\0') {
66172029Spjd		/* No unit. */
67172029Spjd		*num = number;
68172029Spjd		return (0);
69172029Spjd	}
70172029Spjd
71172029Spjd	s = tolower(*endptr);
72172049Spjd	switch (s) {
73172049Spjd	case 'b':
74172049Spjd	case 'k':
75172049Spjd	case 'm':
76172049Spjd	case 'g':
77172049Spjd	case 't':
78172049Spjd	case 'p':
79172049Spjd	case 'e':
80172049Spjd		break;
81172049Spjd	default:
82172049Spjd		/* Unrecognized unit. */
83172049Spjd		errno = EINVAL;
84172049Spjd		return (-1);
85172049Spjd	}
86172049Spjd
87172049Spjd	for (i = 0; unit[i] != '\0'; i++) {
88172029Spjd		if (s == unit[i])
89172029Spjd			break;
90172029Spjd		if ((number < 0 && (number << 10) > number) ||
91172029Spjd		    (number >= 0 && (number << 10) < number)) {
92172029Spjd			errno = ERANGE;
93172029Spjd			return (-1);
94172029Spjd		}
95172029Spjd		number <<= 10;
96172029Spjd	}
97172029Spjd
98172029Spjd	*num = number;
99172029Spjd	return (0);
100172029Spjd}
101