Index: Makefile =================================================================== --- Makefile (revision 257754) +++ Makefile (working copy) @@ -10,6 +10,7 @@ SHLIB_MAJOR= 9 SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ hexdump.c humanize_number.c kinfo_getfile.c kinfo_getfile.c \ + dehumanize_number.c \ kinfo_getallproc.c kinfo_getproc.c kinfo_getvmmap.c kld.c \ login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ Index: dehumanize_number.c =================================================================== --- dehumanize_number.c (revision 0) +++ dehumanize_number.c (working copy) @@ -0,0 +1,119 @@ +/* $NetBSD: dehumanize_number.c,v 1.3 2008/04/28 20:22:59 martin Exp $ + * $FreeBSD$ + */ + +/* + * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Julio M. Merino Vidal, developed as part of Google's Summer of Code + * 2005 program. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "libutil.h" + +/* + * Converts the number given in 'str', which may be given in a humanized + * form (as described in humanize_number(3), but with some limitations), + * to an int64_t without units. + * In case of success, 0 is returned and *size holds the value. + * Otherwise, -1 is returned and *size is untouched. + * + * TODO: Internationalization, SI units. + */ +int +dehumanize_number(const char *str, int64_t *size) +{ + char *ep, unit; + const char *delimit; + long multiplier; + long long tmp, tmp2; + size_t len; + + len = strlen(str); + if (len == 0) { + errno = EINVAL; + return -1; + } + + multiplier = 1; + + unit = str[len - 1]; + if (isalpha((unsigned char)unit)) { + switch (tolower((unsigned char)unit)) { + case 'e': + multiplier *= 1024; + /* FALLTHROUGH */ + case 'p': + multiplier *= 1024; + /* FALLTHROUGH */ + case 't': + multiplier *= 1024; + /* FALLTHROUGH */ + case 'g': + multiplier *= 1024; + /* FALLTHROUGH */ + case 'm': + multiplier *= 1024; + /* FALLTHROUGH */ + case 'k': + multiplier *= 1024; + /* FALLTHROUGH */ + case 'b': + break; + + default: + errno = EINVAL; + return -1; /* Invalid suffix. */ + } + + delimit = &str[len - 1]; + } else + delimit = NULL; + + errno = 0; + tmp = strtoll(str, &ep, 10); + if (str[0] == '\0' || (ep != delimit && *ep != '\0')) + return -1; /* Not a number. */ + else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN)) + return -1; /* Out of range. */ + + tmp2 = tmp * multiplier; + tmp2 = tmp2 / multiplier; + if (tmp != tmp2) { + errno = ERANGE; + return -1; /* Out of range. */ + } + *size = tmp * multiplier; + + return 0; +} Property changes on: dehumanize_number.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: humanize_number.3 =================================================================== --- humanize_number.3 (revision 257754) +++ humanize_number.3 (working copy) @@ -28,17 +28,20 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd October 7, 2013 +.Dd November 7, 2013 .Dt HUMANIZE_NUMBER 3 .Os .Sh NAME +.Nm dehumanize_number , .Nm humanize_number -.Nd format a number into a human readable form +.Nd format a number into a human readable form and vice versa .Sh LIBRARY .Lb libutil .Sh SYNOPSIS .In libutil.h .Ft int +.Fn dehumanize_number "const char *str" "int64_t *result" +.Ft int .Fo humanize_number .Fa "char *buf" "size_t len" "int64_t number" "const char *suffix" .Fa "int scale" "int flags" @@ -159,9 +162,18 @@ This flag has no effect when .Dv HN_DIVISOR_1000 is also specified. .El +.Pp +The +.Fn dehumanize_number +function parses the string representing an integral value given in +.Fa str +and stores the numerical value in the integer pointed to by +.Fa result . +The provided string may hold one of the suffixes, which will be interpreted +and used to scale up its accompanying numerical value. .Sh RETURN VALUES Upon success, the -.Nm +.Fn humanize_number function returns the number of characters that would have been stored in .Fa buf (excluding the terminating @@ -175,6 +187,12 @@ may be modified. If .Dv HN_GETSCALE is specified, the prefix index number will be returned instead. +.Pp +The +.Fn dehumanize_number +function returns 0 if the string was parsed correctly. +A \-1 is returned to indicate failure and an error code is stored in +.Va errno . .Sh SEE ALSO .Xr expand_number 3 .Sh STANDARDS @@ -200,3 +218,11 @@ The .Dv HN_IEC_PREFIXES flag was introduced in .Fx 9.0 . +The +.Fn dehumanize_number +function first appeared in +.Nx 5.0 +and +.Fx 10.1 . +This implementation was taken from +.Dx . Index: libutil.h =================================================================== --- libutil.h (revision 257754) +++ libutil.h (working copy) @@ -95,6 +95,7 @@ int extattr_string_to_namespace(const char *_strin int flopen(const char *_path, int _flags, ...); int forkpty(int *_amaster, char *_name, struct termios *_termp, struct winsize *_winp); +int dehumanize_number(const char *, int64_t *); void hexdump(const void *_ptr, int _length, const char *_hdr, int _flags); int humanize_number(char *_buf, size_t _len, int64_t _number, const char *_suffix, int _scale, int _flags);