mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-01-24 02:51:43 +01:00
Add new expand_number() function
Taken from FreeBSD. [guillem@hadrons.org: - Include <stdint.h> in <bsd/libutil.h>. ] Signed-off-by: Guillem Jover <guillem@hadrons.org>
This commit is contained in:
parent
943939d0e5
commit
6434858314
27
COPYING
27
COPYING
@ -304,6 +304,33 @@
|
|||||||
|
|
||||||
--
|
--
|
||||||
|
|
||||||
|
Copyright © 2007 Eric Anderson <anderson@FreeBSD.org>
|
||||||
|
Copyright © 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
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 AUTHORS 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 AUTHORS 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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
Copyright © 2007 Dag-Erling Coïdan Smørgrav
|
Copyright © 2007 Dag-Erling Coïdan Smørgrav
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
|
|
||||||
#include <features.h>
|
#include <features.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/* for pidfile.c */
|
/* for pidfile.c */
|
||||||
struct pidfh {
|
struct pidfh {
|
||||||
@ -53,6 +54,7 @@ struct pidfh {
|
|||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
int humanize_number(char *buf, size_t len, int64_t bytes,
|
int humanize_number(char *buf, size_t len, int64_t bytes,
|
||||||
const char *suffix, int scale, int flags);
|
const char *suffix, int scale, int flags);
|
||||||
|
int expand_number(const char *_buf, uint64_t *_num);
|
||||||
|
|
||||||
int flopen(const char *_path, int _flags, ...);
|
int flopen(const char *_path, int _flags, ...);
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ dist_man_MANS = \
|
|||||||
arc4random_uniform.3 \
|
arc4random_uniform.3 \
|
||||||
closefrom.3 \
|
closefrom.3 \
|
||||||
dehumanize_number.3 \
|
dehumanize_number.3 \
|
||||||
|
expand_number.3 \
|
||||||
fgetln.3 \
|
fgetln.3 \
|
||||||
flopen.3 \
|
flopen.3 \
|
||||||
fmtcheck.3 \
|
fmtcheck.3 \
|
||||||
|
87
man/expand_number.3
Normal file
87
man/expand_number.3
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
.\" Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
|
||||||
|
.\" Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
.\" All rights reserved.
|
||||||
|
.\"
|
||||||
|
.\" 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 AUTHORS 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 AUTHORS 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.
|
||||||
|
.\"
|
||||||
|
.\" $FreeBSD$
|
||||||
|
.\"
|
||||||
|
.Dd August 15, 2010
|
||||||
|
.Dt EXPAND_NUMBER 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm expand_number
|
||||||
|
.Nd format a number from human readable form
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In bsd/libutil.h
|
||||||
|
.Ft int
|
||||||
|
.Fo expand_number
|
||||||
|
.Fa "const char *buf" "uint64_t *num"
|
||||||
|
.Fc
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn expand_number
|
||||||
|
function unformats the
|
||||||
|
.Fa buf
|
||||||
|
string and stores a unsigned 64-bit quantity at address pointed out by the
|
||||||
|
.Fa num
|
||||||
|
argument.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn expand_number
|
||||||
|
function
|
||||||
|
follows the SI power of two convention.
|
||||||
|
.Pp
|
||||||
|
The prefixes are:
|
||||||
|
.Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
|
||||||
|
.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
|
||||||
|
.It Li k Ta No kilo Ta 1024
|
||||||
|
.It Li M Ta No mega Ta 1048576
|
||||||
|
.It Li G Ta No giga Ta 1073741824
|
||||||
|
.It Li T Ta No tera Ta 1099511627776
|
||||||
|
.It Li P Ta No peta Ta 1125899906842624
|
||||||
|
.It Li E Ta No exa Ta 1152921504606846976
|
||||||
|
.El
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
.Rv -std
|
||||||
|
.Sh ERRORS
|
||||||
|
The
|
||||||
|
.Fn expand_number
|
||||||
|
function will fail if:
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
The given string contains no digits.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
An unrecognized prefix was given.
|
||||||
|
.It Bq Er ERANGE
|
||||||
|
Result doesn't fit into 64 bits.
|
||||||
|
.El
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr humanize_number 3
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Fn expand_number
|
||||||
|
function first appeared in
|
||||||
|
.Fx 6.3 .
|
@ -44,6 +44,7 @@ libbsd_la_SOURCES = \
|
|||||||
closefrom.c \
|
closefrom.c \
|
||||||
dehumanize_number.c \
|
dehumanize_number.c \
|
||||||
err.c \
|
err.c \
|
||||||
|
expand_number.c \
|
||||||
fgetln.c \
|
fgetln.c \
|
||||||
flopen.c \
|
flopen.c \
|
||||||
fmtcheck.c \
|
fmtcheck.c \
|
||||||
|
101
src/expand_number.c
Normal file
101
src/expand_number.c
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
|
||||||
|
* Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* 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 AUTHORS 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 AUTHORS 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 <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <libutil.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert an expression of the following forms to a uint64_t.
|
||||||
|
* 1) A positive decimal number.
|
||||||
|
* 2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
|
||||||
|
* 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
|
||||||
|
* 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
|
||||||
|
* 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
|
||||||
|
* 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
|
||||||
|
* 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
|
||||||
|
* 8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
expand_number(const char *buf, uint64_t *num)
|
||||||
|
{
|
||||||
|
uint64_t number;
|
||||||
|
unsigned shift;
|
||||||
|
char *endptr;
|
||||||
|
|
||||||
|
number = strtoumax(buf, &endptr, 0);
|
||||||
|
|
||||||
|
if (endptr == buf) {
|
||||||
|
/* No valid digits. */
|
||||||
|
errno = EINVAL;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (tolower((unsigned char)*endptr)) {
|
||||||
|
case 'e':
|
||||||
|
shift = 60;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
shift = 50;
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
shift = 40;
|
||||||
|
break;
|
||||||
|
case 'g':
|
||||||
|
shift = 30;
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
shift = 20;
|
||||||
|
break;
|
||||||
|
case 'k':
|
||||||
|
shift = 10;
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
case '\0': /* No unit. */
|
||||||
|
*num = number;
|
||||||
|
return (0);
|
||||||
|
default:
|
||||||
|
/* Unrecognized unit. */
|
||||||
|
errno = EINVAL;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((number << shift) >> shift != number) {
|
||||||
|
/* Overflow */
|
||||||
|
errno = ERANGE;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
*num = number << shift;
|
||||||
|
return (0);
|
||||||
|
}
|
@ -92,4 +92,5 @@ LIBBSD_0.3 {
|
|||||||
|
|
||||||
LIBBSD_0.4 {
|
LIBBSD_0.4 {
|
||||||
closefrom;
|
closefrom;
|
||||||
|
expand_number;
|
||||||
} LIBBSD_0.3;
|
} LIBBSD_0.3;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user