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