Index: libutil.h =================================================================== RCS file: /private/FreeBSD/src/lib/libutil/libutil.h,v retrieving revision 1.40 diff -u -p -r1.40 libutil.h --- libutil.h 24 May 2004 22:19:27 -0000 1.40 +++ libutil.h 16 Sep 2004 18:31:02 -0000 @@ -84,6 +84,9 @@ int realhostname(char *host, size_t hsiz struct sockaddr; int realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen); +#ifdef _SYS_TYPES_H_ +off_t parse_capacity(const char *_capacity); +#endif #ifdef _STDIO_H_ /* avoid adding new includes */ char *fparseln(FILE *, size_t *, size_t *, const char[3], int); #endif Index: Makefile =================================================================== RCS file: /private/FreeBSD/src/lib/libutil/Makefile,v retrieving revision 1.56 diff -u -p -r1.56 Makefile --- Makefile 24 May 2004 22:19:27 -0000 1.56 +++ Makefile 16 Sep 2004 18:12:00 -0000 @@ -8,15 +8,16 @@ CFLAGS+=-DLIBC_SCCS -I${.CURDIR} -I${.CU CFLAGS+=-DINET6 SRCS= _secure_path.c auth.c fparseln.c humanize_number.c login.c \ login_auth.c login_cap.c login_class.c login_crypt.c login_ok.c \ - login_times.c login_tty.c logout.c logwtmp.c property.c pty.c \ - pw_util.c realhostname.c stub.c \ + login_times.c login_tty.c logout.c logwtmp.c parse_capacity.c \ + property.c pty.c pw_util.c realhostname.c stub.c \ trimdomain.c uucplock.c INCS= libutil.h login_cap.h MAN+= login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \ login_cap.3 login_class.3 login_times.3 login_ok.3 \ _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ - realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 + realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \ + parse_capacity.3 MAN+= login.conf.5 auth.conf.5 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 MLINKS+= property.3 property_find.3 Index: parse_capacity.c =================================================================== RCS file: parse_capacity.c diff -N parse_capacity.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ parse_capacity.c 16 Sep 2004 18:27:43 -0000 @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 2004 Pawel Jakub Dawidek + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include + +off_t +parse_capacity(const char *capacity) +{ + static char unit[] = "kmgtpe"; + off_t number; + char *p; + int errnob, n; + + errnob = errno; + errno = 0; + number = (off_t)strtoimax(capacity, &p, 0); + if (errno != 0) + return (-1); + errno = errnob; + if (*p == '\0') + return (number); + for (n = 0; unit[n] != '\0'; n++) { + if (unit[n] == *p || toupper(unit[n]) == *p) + break; + } + if (unit[n] == '\0') { + errno = EINVAL; + return (-1); + } + for (; n >= 0; n--) + number *= 1024; + return (number); +} Index: parse_capacity.3 =================================================================== RCS file: parse_capacity.3 diff -N parse_capacity.3 --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ parse_capacity.3 16 Sep 2004 18:26:44 -0000 @@ -0,0 +1,68 @@ +.\" Copyright (c) 2004 Pawel Jakub Dawidek +.\" 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 Sep 16, 2004 +.Dt PARSE_CAPACITY +.Os +.Sh NAME +.Nm parse_capacity +.Nd format capacity into a number +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In sys/types.h +.In libutil.h +.Ft off_t +.Fo parse_capacity +.Fa "const char *capacity" +.Fc +.Sh DESCRIPTION +The +.Fn parse_capacity +function converts the string with a human readable capacity value into a signed +64-bit number. +.Pp +The recognized capacity units are: +.Bl -column "Suffix" "Description" "Multiplier" -offset indent +.It Sy "Suffix" 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 +.Pp +The +capacity unit can be a upper or lower case character. +.Sh RETURN VALUES +The +.Fn parse_capacity +function returns converted number upon success, or \-1 upon failure. +.Sh SEE ALSO +.Xr humanize_number 3 +.Sh AUTHORS +.An Pawel Jakub Dawidek Aq pjd@FreeBSD.org