NetBSD Problem Report #15508

Received: (qmail 7690 invoked from network); 6 Feb 2002 15:15:09 -0000
Message-Id: <20020206151421.16291.qmail@h187n2fls4o804.telia.com>
Date: 6 Feb 2002 15:14:21 -0000
From: Tomas Svensson <tsn@gbdev.net>
Reply-To: Tomas Svensson <tsn@gbdev.net>
To: gnats-bugs@gnats.netbsd.org
Subject: humanize_number(3) 
X-Send-Pr-Version: 3.95

>Number:         15508
>Category:       lib
>Synopsis:       humanize_number(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    abs
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Wed Feb 06 15:16:00 +0000 2002
>Closed-Date:    Thu Aug 08 17:22:26 +0000 2002
>Last-Modified:  Wed May 13 13:15:26 +0000 2015
>Originator:     
>Release:        NetBSD 1.5ZA
>Organization:
>Environment:
		NetBSD 1.5ZA
>Description:

Here is a new version of humanize_number(9) for libc to be used
in programs such as df(1)/du(1)/ls(1).

Differences from the kernel version are:

- the bytes argument is signed (negative sizes can be used).
- the divisor argument is replaced with a flag, see below.
- rounding is done like floating point numbers were used.
- the suffix-prefix K is changed to k, which should be correct
  according to standards.
- three flags can be used : HN_DECIMAL tells it to use one decimal
  for numbers less than 10, HN_NOSPACE removes the space between
  the number and the prefix, HN_DIVISOR_1000 makes the divisor
  1000 (otherwise 1024 is assumed) and HN_B will use the prefix "B" for
  numbers that would normally not have a prefix (bytes less than
  1000). Use 0 if no flags are used (not worth the trouble with
  varargs?).

>How-To-Repeat:
>Fix:

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

#define HN_DECIMAL	1
#define HN_NOSPACE      2	
#define HN_B		4
#define HN_DIVISOR_1000 8

int humanize_number(char *, size_t, int64_t, const char *, int);

int humanize_number(char *buf, size_t len, int64_t bytes,
        const char *suffix, int flags) {

	static const char prefixes[] = " kMGTPE";

	int	divisor, i, r;
	int64_t	max, s1, s2, sign;
	size_t	baselen, suffixlen;

	if (buf == NULL)
		return -1;

	if (len > 0)
		buf[0] = '\0';
	if (bytes < 0) {
		sign = -1;
		bytes *= -100;
		baselen = 4;
	} else {
		sign = 1;
		bytes *= 100;
		baselen = 3;
	}

	suffixlen = strlen(suffix);

	/* check if enough room for `x y' + suffix + `\0' */
	if (len < baselen + suffixlen + 1)
		return (-1);

	if (flags & HN_DIVISOR_1000)
		divisor = 1000;
	else
		divisor = 1024;

	max = 100;
	for (i = 0; i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
             1 : 0) ; i++)
		max *= 10;

	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
		bytes /= divisor;

	if (bytes < 1000 && flags & HN_DECIMAL) {
		if (len < (baselen + 2  + ( (flags & HN_NOSPACE) || (i == 0 &&
		    !(flags & HN_B)) ? 0 : 1)))
			return (-1);
		s1 = bytes / 100;
		if ((s2 = (( (bytes % 100 ) + 5 ) /  10 ) ) == 10 ) {
			s1++;
			s2 = 0;
		}
		r = snprintf(buf, len, "%qd%s%qd%s%c%s", sign * s1, 
		    localeconv()->decimal_point, s2,
		    (i == 0 && !(flags & HN_B) ) || flags & HN_NOSPACE ?
		    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
		    prefixes[i], suffix);

	} else
	r = snprintf(buf, len, "%qd%s%c%s", sign * ((bytes + 50) / 100), 
	    i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
	    (flags & HN_B)) ? 'B' : prefixes[i], suffix);

	return r;
}
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: bin-bug-people->lib-bug-people 
Responsible-Changed-By: wiz 
Responsible-Changed-When: Mon Mar 4 03:33:24 PST 2002 
Responsible-Changed-Why:  
Wrong category. 

From: Tomas Svensson <tsn@gbdev.net>
To: gnats-bugs@gnats.netbsd.org
Cc:  
Subject: Re: bin/15508: humanize_number(3)
Date: Mon, 4 Mar 2002 18:43:38 +0100

 Here is a new patch that integrates humanize_number(3) into libutil.
 Works fine as far as I can tell, changed df(1) to use the new function
 for displaying humanized output which worked fine. Changed back the
 'k' prefix to 'K' as both are equally wrong.

 -Tomas


 Index: lib/libutil/Makefile
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libutil/Makefile,v
 retrieving revision 1.32
 diff -u -r1.32 Makefile
 --- Makefile	2001/12/28 01:32:40	1.32
 +++ Makefile	2002/03/04 17:29:17
 @@ -5,12 +5,12 @@
  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

 Index: lib/libutil/humanize_number.3
 ===================================================================
 RCS file: humanize_number.3
 diff -N humanize_number.3
 --- /dev/null	Sun May 20 17:40:04 2001
 +++ humanize_number.3	Mon Mar  4 09:29:17 2002
 @@ -0,0 +1,118 @@
 +.\"	$NetBSD: humanize_number.3,v 1.4 2001/12/26 00:24:16 wiz 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.
 +.\"
 +.\" 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 4, 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 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 .
 +.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.
 +.Sh SEE ALSO
 +.Xr humanize_number 9
 +.Sh HISTORY
 +.Fn humanize_number
 +first appeared in
 +.Nx 1.6 .
 Index: lib/libutil/humanize_number.c
 ===================================================================
 RCS file: humanize_number.c
 diff -N humanize_number.c
 --- /dev/null	Sun May 20 17:40:04 2001
 +++ humanize_number.c	Mon Mar  4 09:29:17 2002
 @@ -0,0 +1,120 @@
 +/*
 + * 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, and by Luke Mewburn.
 + *
 + * 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.0 2002/03/04 13:51:05 tomas 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 flags) {
 +
 +	static const char prefixes[] = " KMGTPE";
 +	
 +	int	i, r;
 +	int64_t	divisor, max, s1, s2, sign;
 +	size_t	baselen, suffixlen;
 +
 +	_DIAGASSERT(buf != NULL);
 +	_DIAGASSERT(suffix != NULL);
 +
 +	if (buf == NULL || suffix == NULL)
 +		return -1;
 +
 +	if (len > 0)
 +		buf[0] = '\0';
 +	if (bytes < 0) {
 +		sign = -1;
 +		bytes *= -100;
 +		baselen = 4;
 +	} else {
 +		sign = 1;
 +		bytes *= 100;
 +		baselen = 3;
 +	}
 +
 +	suffixlen = strlen(suffix);
 +
 +	/* check if enough room for `x y' + suffix + `\0' */
 +	if (len < baselen + suffixlen + 1)
 +		return (-1);
 +
 +	if (flags & HN_DIVISOR_1000)
 +		divisor = 1000;
 +	else
 +		divisor = 1024;
 +
 +	max = 100;
 +	for (i = 0; i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
 +             1 : 0) ; i++)
 +		max *= 10;
 +
 +	for (i = 0; bytes >= max && i < sizeof(prefixes); i++)
 +		bytes /= divisor;
 +
 +	if (bytes < 1000 && flags & HN_DECIMAL) {
 +		if (len < (baselen + 2  + ( (flags & HN_NOSPACE) || (i == 0 &&
 +		    !(flags & HN_B)) ? 0 : 1)))
 +			return (-1);
 +		s1 = bytes / 100;
 +		if ((s2 = (( (bytes % 100 ) + 5 ) /  10 ) ) == 10 ) {
 +			s1++;
 +			s2 = 0;
 +		}
 +		r = snprintf(buf, len, "%qd%s%qd%s%c%s", sign * s1, 
 +		    localeconv()->decimal_point, s2,
 +		    (i == 0 && !(flags & HN_B) ) || flags & HN_NOSPACE ?
 +		    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
 +		    prefixes[i], suffix);
 +		
 +	} else
 +	r = snprintf(buf, len, "%qd%s%c%s", sign * ((bytes + 50) / 100), 
 +	    i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
 +	    (flags & HN_B)) ? 'B' : prefixes[i], suffix);
 +
 +	return r;
 +}
 Index: lib/libutil/util.3
 ===================================================================
 RCS file: /cvsroot/basesrc/lib/libutil/util.3,v
 retrieving revision 1.5
 diff -u -r1.5 util.3
 --- util.3	2002/02/07 07:00:52	1.5
 +++ util.3	2002/03/04 17:29:17
 @@ -34,7 +34,7 @@
  .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  .\" POSSIBILITY OF SUCH DAMAGE.
  .\"
 -.Dd November 23, 2001
 +.Dd March 4, 2002
  .Dt UTIL 3
  .Os
  .Sh NAME
 @@ -64,6 +64,7 @@
  getbootfile.3	get the name of the booted kernel file
  getmaxpartitions.3	get the maximum number of partitions allowed per disk
  getrawpartition.3	get the system ``raw'' partition
 +humanize_number.3	format a number into a human readable form
  login.3	login utility function
  login_close.3	query login.conf database about a user class
  login_getcapbool.3	query login.conf database about a user class
 Index: include/util.h
 ===================================================================
 RCS file: /cvsroot/basesrc/include/util.h,v
 retrieving revision 1.21
 diff -u -r1.21 util.h
 --- util.h	2001/08/18 19:31:47	1.21
 +++ util.h	2002/03/04 17:29:17
 @@ -53,6 +53,11 @@
  #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
 +
  __BEGIN_DECLS
  struct iovec;
  struct passwd;
 @@ -65,6 +70,7 @@
  const char     *getbootfile(void);
  int		getmaxpartitions(void);
  int		getrawpartition(void);
 +int		humanize_number(char *, size_t, int64_t, const char *, int);
  void		login(const struct utmp *);
  int		login_tty(int);
  int		logout(const char *);

From: Tomas Svensson <tsn@gbdev.net>
To: gnats-bugs@netbsd.org
Cc: matthew green <mrg@eterna.com.au>, "Perry E. Metzger" <perry@wasabisystems.com>
Subject: Re: lib/15508
Date: Sat, 23 Mar 2002 17:40:57 +0100

 [Maybe one of you that suggested humanize_number to be in the userland
 libs can look at this before the 1.6 freeze...]

 I put up the latest patches that includes the ability to get and set
 the "prefix index number" (number / 1024^index), to be able to format
 all the numbers with the same prefix.

 I didn't put the new files in the diff as it seems patch(1) doesn't
 work properly with diffs created using -N.

 http://simba.unix1.net/netbsd/humanize6.diff
 http://simba.unix1.net/netbsd/humanize_number.c
 http://simba.unix1.net/netbsd/humanize_number.3

 -Tomas


State-Changed-From-To: open->closed 
State-Changed-By: abs 
State-Changed-When: Thu Aug 8 10:22:03 PDT 2002 
State-Changed-Why:  
PR applied to -current. Many thanks! 


Responsible-Changed-From-To: lib-bug-people->abs 
Responsible-Changed-By: abs 
Responsible-Changed-When: Thu Aug 8 10:22:03 PDT 2002 
Responsible-Changed-Why:  

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.