expand_number.c revision 172049
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 172049 2007-09-05 14:27:13Z pjd $");
30172029Spjd
31172029Spjd#include <sys/types.h>
32172029Spjd#include <ctype.h>
33172029Spjd#include <errno.h>
34172029Spjd#include <libutil.h>
35172029Spjd#include <stdint.h>
36172029Spjd
37172029Spjd/*
38172029Spjd * Convert an expression of the following forms to a int64_t.
39172029Spjd * 	1) A positive decimal number.
40172029Spjd *	2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
41172029Spjd *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
42172029Spjd *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
43172029Spjd *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
44172029Spjd *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
45172029Spjd *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
46172029Spjd *	8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
47172029Spjd */
48172029Spjdint
49172029Spjdexpand_number(char *buf, int64_t *num)
50172029Spjd{
51172029Spjd	static const char unit[] = "bkmgtpe";
52172029Spjd	char *endptr, s;
53172029Spjd	int64_t number;
54172029Spjd	int i;
55172029Spjd
56172029Spjd	number = strtoimax(buf, &endptr, 0);
57172029Spjd
58172029Spjd	if (endptr == buf) {
59172029Spjd		/* No valid digits. */
60172029Spjd		errno = EINVAL;
61172029Spjd		return (-1);
62172029Spjd	}
63172029Spjd
64172029Spjd	if (*endptr == '\0') {
65172029Spjd		/* No unit. */
66172029Spjd		*num = number;
67172029Spjd		return (0);
68172029Spjd	}
69172029Spjd
70172029Spjd	s = tolower(*endptr);
71172049Spjd	switch (s) {
72172049Spjd	case 'b':
73172049Spjd	case 'k':
74172049Spjd	case 'm':
75172049Spjd	case 'g':
76172049Spjd	case 't':
77172049Spjd	case 'p':
78172049Spjd	case 'e':
79172049Spjd		break;
80172049Spjd	default:
81172049Spjd		/* Unrecognized unit. */
82172049Spjd		errno = EINVAL;
83172049Spjd		return (-1);
84172049Spjd	}
85172049Spjd
86172049Spjd	for (i = 0; unit[i] != '\0'; i++) {
87172029Spjd		if (s == unit[i])
88172029Spjd			break;
89172029Spjd		if ((number < 0 && (number << 10) > number) ||
90172029Spjd		    (number >= 0 && (number << 10) < number)) {
91172029Spjd			errno = ERANGE;
92172029Spjd			return (-1);
93172029Spjd		}
94172029Spjd		number <<= 10;
95172029Spjd	}
96172029Spjd
97172029Spjd	*num = number;
98172029Spjd	return (0);
99172029Spjd}
100