Index: Makefile =================================================================== RCS file: /alt/ncvs/src/lib/libutil/Makefile,v retrieving revision 1.63 diff -u -r1.63 Makefile --- Makefile 27 Jul 2006 12:36:46 -0000 1.63 +++ Makefile 3 Apr 2007 13:34:31 -0000 @@ -12,7 +12,7 @@ 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 \ pidfile.c property.c pty.c pw_util.c realhostname.c stub.c \ - trimdomain.c uucplock.c + trimdomain.c unhumanize_number.c uucplock.c INCS= libutil.h login_cap.h CFLAGS+= -DLIBC_SCCS @@ -27,7 +27,7 @@ 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 \ - pidfile.3 + unhumanize_number.3 pidfile.3 MAN+= login.conf.5 auth.conf.5 MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 Index: libutil.h =================================================================== RCS file: /alt/ncvs/src/lib/libutil/libutil.h,v retrieving revision 1.42 diff -u -r1.42 libutil.h --- libutil.h 18 Feb 2006 11:25:28 -0000 1.42 +++ libutil.h 3 Apr 2007 13:05:16 -0000 @@ -81,6 +81,7 @@ struct termios *_termp, struct winsize *_winp); int humanize_number(char *_buf, size_t _len, int64_t _number, const char *_suffix, int _scale, int _flags); +int64_t unhumanize_number(char *_buf); const char *uu_lockerr(int _uu_lockresult); int uu_lock(const char *_ttyname); int uu_unlock(const char *_ttyname); --- /dev/null Tue Apr 3 10:24:54 2007 +++ unhumanize_number.c Tue Apr 3 08:10:34 2007 @@ -0,0 +1,91 @@ +/*- + * Copyright (c) 2007 + * 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 + +#include +#include +#include +#include +#include +#include +#include + + +/* + * Convert an expression of the following forms to a int64_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). + */ +int64_t +unhumanize_number(char *val) +{ + int64_t *prevnum, number; + int64_t mult; + char *expr; + + number = strtoumax(val, &expr, 0); + + if (expr == val) /* No valid digits. */ + printf("illegal numeric value\n"); + + mult = 0; + switch (*expr) { + case 'B': + case 'b': + mult = 1; + break; + case 'K': + case 'k': + mult = 1 << 10; + break; + case 'M': + case 'm': + mult = 1 << 20; + break; + case 'G': + case 'g': + mult = 1 << 30; + break; + case 'T': + case 't': + mult = 1 << 30; + mult *= 1024; + break; + default: + ; + } + + if (mult != 0) { + number *= mult; + } + + return (number); +} --- /dev/null Tue Apr 3 10:24:54 2007 +++ unhumanize_number.3 Tue Apr 3 08:35:18 2007 @@ -0,0 +1,50 @@ +.\" +.\" +.Dd April 3, 2007 +.Dt UNHUMANIZE_NUMBER 3 +.Os +.Sh NAME +.Nm unhumanize_number +.Nd format a number from human readable form +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In libutil.h +.Ft int64_t +.Fo unhumanize_number +.Fa "char *buf" +.Fc +.Sh DESCRIPTION +The +.Fn unhumanize_number +function unformats the +.Fa buffer . +and returns a signed 64-bit quantity. +.Pp +The +.Fn unhumanize_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 +The +.Fn unhumanize_number +function returns the number calculated from the suffix appended +to the +.Fa buffer +.Sh HISTORY +The +.Fn unhumanize_number +function first appeared in +.Fx 7.0 .