expand_number.c revision 180347
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
31556Srgrimes * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
41556Srgrimes * All rights reserved.
51556Srgrimes *
61556Srgrimes * Redistribution and use in source and binary forms, with or without
71556Srgrimes * modification, are permitted provided that the following conditions
81556Srgrimes * are met:
91556Srgrimes * 1. Redistributions of source code must retain the above copyright
101556Srgrimes *    notice, this list of conditions and the following disclaimer.
111556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer in the
131556Srgrimes *    documentation and/or other materials provided with the distribution.
141556Srgrimes *
151556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
161556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
191556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251556Srgrimes * SUCH DAMAGE.
261556Srgrimes */
271556Srgrimes
281556Srgrimes#include <sys/cdefs.h>
291556Srgrimes__FBSDID("$FreeBSD: head/lib/libutil/expand_number.c 180347 2008-07-07 12:20:34Z kib $");
301556Srgrimes
311556Srgrimes#include <sys/types.h>
321556Srgrimes#include <ctype.h>
331556Srgrimes#include <errno.h>
341556Srgrimes#include <inttypes.h>
351556Srgrimes#include <libutil.h>
3617987Speter#include <stdint.h>
3738521Scracauer
381556Srgrimes/*
391556Srgrimes * Convert an expression of the following forms to a int64_t.
401556Srgrimes * 	1) A positive decimal number.
4138521Scracauer *	2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
421556Srgrimes *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
4317987Speter *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
4420425Ssteve *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
4531098Sbde *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
4617987Speter *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
4717987Speter *	8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
4817987Speter */
4917987Speterint
5020425Ssteveexpand_number(const char *buf, int64_t *num)
51{
52	static const char unit[] = "bkmgtpe";
53	char *endptr, s;
54	int64_t number;
55	int i;
56
57	number = strtoimax(buf, &endptr, 0);
58
59	if (endptr == buf) {
60		/* No valid digits. */
61		errno = EINVAL;
62		return (-1);
63	}
64
65	if (*endptr == '\0') {
66		/* No unit. */
67		*num = number;
68		return (0);
69	}
70
71	s = tolower(*endptr);
72	switch (s) {
73	case 'b':
74	case 'k':
75	case 'm':
76	case 'g':
77	case 't':
78	case 'p':
79	case 'e':
80		break;
81	default:
82		/* Unrecognized unit. */
83		errno = EINVAL;
84		return (-1);
85	}
86
87	for (i = 0; unit[i] != '\0'; i++) {
88		if (s == unit[i])
89			break;
90		if ((number < 0 && (number << 10) > number) ||
91		    (number >= 0 && (number << 10) < number)) {
92			errno = ERANGE;
93			return (-1);
94		}
95		number <<= 10;
96	}
97
98	*num = number;
99	return (0);
100}
101