Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add humanize_number.3 from Tomas Svensson in PR lib/15508.



details:   https://anonhg.NetBSD.org/src/rev/38717bfde551
branches:  trunk
changeset: 535024:38717bfde551
user:      abs <abs%NetBSD.org@localhost>
date:      Thu Aug 08 16:50:35 2002 +0000

description:
Add humanize_number.3 from Tomas Svensson in PR lib/15508.
        Bump version of libutil

diffstat:

 distrib/sets/lists/base/shl.mi |    4 +-
 distrib/sets/lists/comp/mi     |    4 +-
 include/util.h                 |   11 +++-
 lib/libutil/Makefile           |    6 +-
 lib/libutil/humanize_number.3  |  136 +++++++++++++++++++++++++++++++++++++++++
 lib/libutil/humanize_number.c  |  132 +++++++++++++++++++++++++++++++++++++++
 lib/libutil/util.3             |    5 +-
 7 files changed, 289 insertions(+), 9 deletions(-)

diffs (truncated from 400 to 300 lines):

diff -r 61d41f4b7aa7 -r 38717bfde551 distrib/sets/lists/base/shl.mi
--- a/distrib/sets/lists/base/shl.mi    Thu Aug 08 16:23:19 2002 +0000
+++ b/distrib/sets/lists/base/shl.mi    Thu Aug 08 16:50:35 2002 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: shl.mi,v 1.170 2002/08/04 16:43:44 jdc Exp $
+# $NetBSD: shl.mi,v 1.171 2002/08/08 16:50:37 abs Exp $
 # Note: libtermcap and libtermlib are hardlinked and share the same version.
 ./usr/lib/libamu.so.2.1                        base-amd-shlib
 ./usr/lib/libasn1.so.6.0               base-krb5-shlib
@@ -42,7 +42,7 @@
 ./usr/lib/libtermcap.so.0.5            base-sys-shlib
 ./usr/lib/libtermlib.so.0.5            base-sys-shlib
 ./usr/lib/libusbhid.so.1.0             base-sys-shlib
-./usr/lib/libutil.so.6.1               base-sys-shlib
+./usr/lib/libutil.so.6.2               base-sys-shlib
 ./usr/lib/libwrap.so.0.2               base-net-shlib
 ./usr/lib/libz.so.0.3                  base-sys-shlib
 ./usr/lib/i18n/libBIG5.so.4.0          base-i18n-shlib
diff -r 61d41f4b7aa7 -r 38717bfde551 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Thu Aug 08 16:23:19 2002 +0000
+++ b/distrib/sets/lists/comp/mi        Thu Aug 08 16:50:35 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.467 2002/08/07 14:47:09 tron Exp $
+#      $NetBSD: mi,v 1.468 2002/08/08 16:50:37 abs Exp $
 ./sys                          comp-sysutil-root
 ./usr/bin/addr2line            comp-debug-bin
 ./usr/bin/ar                   comp-util-bin
@@ -1972,6 +1972,7 @@
 ./usr/share/man/cat3/hstrerror.0       comp-c-catman
 ./usr/share/man/cat3/htonl.0   comp-c-catman
 ./usr/share/man/cat3/htons.0   comp-c-catman
+./usr/share/man/cat3/humanize_number.0 comp-c-catman
 ./usr/share/man/cat3/hypot.0   comp-c-catman
 ./usr/share/man/cat3/hypotf.0  comp-c-catman
 ./usr/share/man/cat3/ieee.0    comp-c-catman
@@ -4571,6 +4572,7 @@
 ./usr/share/man/man3/hstrerror.3               comp-c-man
 ./usr/share/man/man3/htonl.3           comp-c-man
 ./usr/share/man/man3/htons.3           comp-c-man
+./usr/share/man/man3/humanize_number.3         comp-c-man
 ./usr/share/man/man3/hypot.3           comp-c-man
 ./usr/share/man/man3/hypotf.3          comp-c-man
 ./usr/share/man/man3/ieee.3            comp-c-man
diff -r 61d41f4b7aa7 -r 38717bfde551 include/util.h
--- a/include/util.h    Thu Aug 08 16:23:19 2002 +0000
+++ b/include/util.h    Thu Aug 08 16:50:35 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.h,v 1.23 2002/07/27 23:49:00 christos Exp $       */
+/*     $NetBSD: util.h,v 1.24 2002/08/08 16:50:35 abs Exp $    */
 
 /*-
  * Copyright (c) 1995
@@ -54,6 +54,14 @@
 #define        FPARSELN_UNESCREST      0x08
 #define        FPARSELN_UNESCALL       0x0f
 
+#define        HN_DECIMAL              0x01
+#define        HN_NOSPACE              0x02
+#define        HN_B                    0x04
+#define        HN_DIVISOR_1000         0x08
+
+#define        HN_GETSCALE             0x10
+#define        HN_AUTOSCALE            0x20
+
 __BEGIN_DECLS
 struct iovec;
 struct passwd;
@@ -66,6 +74,7 @@
 const char     *getbootfile(void);
 int            getmaxpartitions(void);
 int            getrawpartition(void);
+int            humanize_number(char *, size_t, int64_t, const char *, int, int);
 void           login(const struct utmp *);
 void           loginx(const struct utmpx *);
 int            login_tty(int);
diff -r 61d41f4b7aa7 -r 38717bfde551 lib/libutil/Makefile
--- a/lib/libutil/Makefile      Thu Aug 08 16:23:19 2002 +0000
+++ b/lib/libutil/Makefile      Thu Aug 08 16:50:35 2002 +0000
@@ -1,16 +1,16 @@
-#      $NetBSD: Makefile,v 1.32 2001/12/28 01:32:40 lukem Exp $
+#      $NetBSD: Makefile,v 1.33 2002/08/08 16:50:38 abs Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/4/93
 
 LIB=   util
 USE_SHLIBDIR=  yes
 CPPFLAGS+=-DLIBC_SCCS
 SRCS=  fparseln.c getbootfile.c getmaxpartitions.c getrawpartition.c \
-       login.c login_cap.c login_tty.c \
+       humanize_number.c login.c login_cap.c login_tty.c \
        logout.c logwtmp.c opendisk.c passwd.c pw_scan.c pidfile.c pidlock.c \
        pty.c secure_path.c ttyaction.c ttymsg.c
 
 MAN=   fparseln.3 getbootfile.3 getmaxpartitions.3 getrawpartition.3 \
-       login.3 login_cap.3 opendisk.3 \
+       humanize_number.3 login.3 login_cap.3 opendisk.3 \
        openpty.3 pidfile.3 pidlock.3 pw_getconf.3 pw_init.3 pw_lock.3 \
        secure_path.3 ttyaction.3 ttymsg.3 util.3
 
diff -r 61d41f4b7aa7 -r 38717bfde551 lib/libutil/humanize_number.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/humanize_number.3     Thu Aug 08 16:50:35 2002 +0000
@@ -0,0 +1,136 @@
+.\"    $NetBSD: humanize_number.3,v 1.1 2002/08/08 16:50:38 abs Exp $
+.\"
+.\" Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Luke Mewburn and by Tomas Svensson.
+.\"
+.\" 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"        This product includes software developed by the NetBSD
+.\"        Foundation, Inc. and its contributors.
+.\" 4. Neither the name of The NetBSD Foundation nor the names of its
+.\"    contributors may be used to endorse or promote products derived
+.\"    from this software without specific prior written permission.
+.\"
+.\" 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.
+.\"
+.Dd March 14, 2002
+.Dt HUMANIZE_NUMBER 3
+.Os
+.Sh NAME
+.Nm humanize_number
+.Nd format a number into a human readable form
+.Sh SYNOPSIS
+.Fd #include \*[Lt]util.h\*[Gt]
+.Ft int
+.Fn humanize_number "char *buf" "size_t len" "int64_t number" "const char *suffix" "int scale" "int flags"
+.Sh DESCRIPTION
+.Ss humanize_number
+The
+.Fn humanize_number
+function formats the signed 64 bit quantity given in
+.Fa number
+into
+.Fa buffer .
+A space and then
+.Fa suffix
+is appended to the end.
+.Fa buffer
+must be at least
+.Fa len
+bytes bytes long.
+.Pp
+If the formatted number (including
+.Fa suffix )
+would be too long to fit into
+.Fa buffer ,
+then divide
+.Fa number
+by 1024 until it will.
+In this case, prefix
+.Fa suffix
+with the appropriate SI designator.
+.Pp
+The prefixes are:
+.Bl -column "Prefix" "Description" "Multiplier" -offset indent
+.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
+.It K  kilo    1024
+.It M  mega    1048576
+.It G  giga    1073741824
+.It T  tera    1099511627776
+.It P  peta    1125899906842624
+.It E  exa     1152921504606846976
+.El
+.Pp
+.Fa len
+must be at least 4 plus the length of
+.Fa suffix ,
+in order to ensure a useful result is generated into
+.Fa buffer .
+To use a specific prefix, specify this as
+.Fa scale
+(Multiplier = 1024 ^ scale). This can not be combined with any of the
+.Fa scale
+flags below.
+.Pp
+The following flags may be passed in
+.Pa scale :
+.Bl -tag -width Dv -offset indent
+.It Dv HN_AUTOSCALE
+Format the buffer using the lowest multiplier possible.
+.It Dv HN_GETSCALE
+Return the prefix index number (the number of times
+.Fa number
+must be divided to fit) instead of formatting it to the buffer.
+.El
+.Pp
+The following flags may be passed in
+.Pa flags :
+.Bl -tag -width Dv -offset indent
+.It Dv HN_DECIMAL
+If the final result is less than 10, display it using one digit.
+.It Dv HN_NOSPACE
+Do not put a space between 
+.Fa number
+and the prefix.
+.It Dv HN_B
+Use 'B' (bytes) as prefix if the original result does not have a prefix.
+.It Dv HN_DIVISOR_1000
+Divide 
+.Fa number
+with 1000 instead of 1024.
+.El
+.Pp
+.Sh RETURN VALUES
+.Fn humanize_number
+returns the number of characters stored in
+.Fa buffer
+(excluding the terminating NUL) upon success, or -1 upon failure. If
+HN_GETSCALE is specified, the prefix index number will be returned
+instead.
+.Sh SEE ALSO
+.Xr humanize_number 9
+.Sh HISTORY
+.Fn humanize_number
+first appeared in
+.Nx 1.6 .
diff -r 61d41f4b7aa7 -r 38717bfde551 lib/libutil/humanize_number.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/humanize_number.c     Thu Aug 08 16:50:35 2002 +0000
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed by the NetBSD
+ *      Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * 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 <sys/cdefs.h>
+#ifndef __lint
+__COPYRIGHT("@(#) Copyright (c) 2002\n\
+       The NetBSD Foundation, inc. All rights reserved.\n");
+__RCSID("$NetBSD: humanize_number.c,v 1.1 2002/08/08 16:50:39 abs Exp $");
+#endif /* !__lint */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <locale.h>
+#include <util.h>
+
+int
+humanize_number(char *buf, size_t len, int64_t bytes,
+        const char *suffix, int scale, int flags) {
+
+       static const char prefixes[] = " KMGTPE";
+       
+       int     i, r;



Home | Main Index | Thread Index | Old Index