Sun Aug 28 07:45:14 2011 UTC ()
add strpct, requested by joerg


(christos)
diff -r1.63 -r1.64 src/lib/libutil/Makefile
diff -r1.47 -r1.48 src/lib/libutil/shlib_version
diff -r0 -r1.1 src/lib/libutil/strpct.3
diff -r0 -r1.1 src/lib/libutil/strpct.c

cvs diff -r1.63 -r1.64 src/lib/libutil/Makefile (expand / switch to unified diff)

--- src/lib/libutil/Makefile 2010/01/27 19:10:31 1.63
+++ src/lib/libutil/Makefile 2011/08/28 07:45:13 1.64
@@ -1,45 +1,45 @@ @@ -1,45 +1,45 @@
1# $NetBSD: Makefile,v 1.63 2010/01/27 19:10:31 drochner Exp $ 1# $NetBSD: Makefile,v 1.64 2011/08/28 07:45:13 christos Exp $
2# @(#)Makefile 8.1 (Berkeley) 6/4/93 2# @(#)Makefile 8.1 (Berkeley) 6/4/93
3 3
4USE_SHLIBDIR= yes 4USE_SHLIBDIR= yes
5 5
6.include <bsd.own.mk> 6.include <bsd.own.mk>
7 7
8.include "${NETBSDSRCDIR}/common/lib/libutil/Makefile.inc" 8.include "${NETBSDSRCDIR}/common/lib/libutil/Makefile.inc"
9 9
10WARNS= 4 10WARNS= 4
11LIB= util 11LIB= util
12CPPFLAGS+=-DLIBC_SCCS -I${.CURDIR} 12CPPFLAGS+=-DLIBC_SCCS -I${.CURDIR}
13SRCS= efun.c getbootfile.c getlabelsector.c getmaxpartitions.c \ 13SRCS= efun.c getbootfile.c getlabelsector.c getmaxpartitions.c \
14 getmntopts.c getrawpartition.c \ 14 getmntopts.c getrawpartition.c \
15 disklabel_dkcksum.c disklabel_scan.c \ 15 disklabel_dkcksum.c disklabel_scan.c \
16 if_media.c \ 16 if_media.c \
17 login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \ 17 login.c loginx.c login_cap.c login_tty.c logout.c logoutx.c \
18 logwtmp.c logwtmpx.c opendisk.c parsedate.y \ 18 logwtmp.c logwtmpx.c opendisk.c parsedate.y \
19 passwd.c pw_scan.c pidfile.c pidlock.c pty.c \ 19 passwd.c pw_scan.c pidfile.c pidlock.c pty.c \
20 raise_default_signal.c \ 20 raise_default_signal.c \
21 secure_path.c snprintb.c sockaddr_snprintf.c stat_flags.c \ 21 secure_path.c snprintb.c sockaddr_snprintf.c stat_flags.c \
22 ttyaction.c ttymsg.c 22 strpct.c ttyaction.c ttymsg.c
23 23
24MAN= efun.3 getbootfile.3 getlabelsector.3 getmaxpartitions.3 \ 24MAN= efun.3 getbootfile.3 getlabelsector.3 getmaxpartitions.3 \
25 getmntopts.3 \ 25 getmntopts.3 \
26 getrawpartition.3 \ 26 getrawpartition.3 \
27 login.3 login_cap.3 loginx.3 \ 27 login.3 login_cap.3 loginx.3 \
28 disklabel_dkcksum.3 disklabel_scan.3 \ 28 disklabel_dkcksum.3 disklabel_scan.3 \
29 opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \ 29 opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \
30 pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \ 30 pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \
31 raise_default_signal.3 \ 31 raise_default_signal.3 \
32 snprintb.3 sockaddr_snprintf.3 stat_flags.3 ttyaction.3 \ 32 snprintb.3 sockaddr_snprintf.3 stat_flags.3 strpct.3 ttyaction.3 \
33 ttymsg.3 util.3 33 ttymsg.3 util.3
34 34
35YPREFIX=__pd 35YPREFIX=__pd
36.PATH: ${NETBSDSRCDIR}/lib/libc/gen 36.PATH: ${NETBSDSRCDIR}/lib/libc/gen
37 37
38.include "compat/Makefile.inc" 38.include "compat/Makefile.inc"
39 39
40MLINKS+=getlabelsector.3 getlabeloffset.3 40MLINKS+=getlabelsector.3 getlabeloffset.3
41MLINKS+=login.3 logout.3 41MLINKS+=login.3 logout.3
42MLINKS+=login.3 logwtmp.3 42MLINKS+=login.3 logwtmp.3
43MLINKS+=login_cap.3 login_getclass.3 43MLINKS+=login_cap.3 login_getclass.3
44MLINKS+=login_cap.3 login_getcapbool.3 44MLINKS+=login_cap.3 login_getcapbool.3
45MLINKS+=login_cap.3 login_getcapnum.3 45MLINKS+=login_cap.3 login_getcapnum.3

cvs diff -r1.47 -r1.48 src/lib/libutil/shlib_version (expand / switch to unified diff)

--- src/lib/libutil/shlib_version 2009/05/13 02:50:32 1.47
+++ src/lib/libutil/shlib_version 2011/08/28 07:45:14 1.48
@@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
1# $NetBSD: shlib_version,v 1.47 2009/05/13 02:50:32 pgoyette Exp $ 1# $NetBSD: shlib_version,v 1.48 2011/08/28 07:45:14 christos Exp $
2# Remember to update distrib/sets/lists/base/shl.* when changing 2# Remember to update distrib/sets/lists/base/shl.* when changing
3# 3#
4major=7 4major=7
5minor=17 5minor=18

File Added: src/lib/libutil/strpct.3
.\" $NetBSD: strpct.3,v 1.1 2011/08/28 07:45:14 christos Exp $
.\"
.\" Copyright (c) 2011 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This file was contributed to The NetBSD Foundation by Christos Zoulas.
.\"
.\" 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 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 August 28, 2011
.Dt STRPCT 3
.Os
.Sh NAME
.Nm strpct
.Nd decimal percent formatter
.Sh LIBRARY
.Lb libutil
.Sh SYNOPSIS
.In util.h
.Ft char *
.Fn strpct "char *buf" "size_t bufsiz" "uintmax_t numerator" "uintmax_t denominator" "size_t precision"
.Sh DESCRIPTION
The
.Fn strpct
function formats the fraction represented by
.Fa numerator
and
.Fa denominator
into a percentage representation with given number of digits of
.Fa precision .
.Sh RETURN VALUES
.Fn strpct
always returns a pointer to a NUL terminated formatted string which
is placed in
.Fa buf
and it is up to
.Fa buflen
characters.
If there was an overflow, the formatted string will reflect that precision
loss.
.Sh EXAMPLES
.Bd -literal -offset indent
strpct(buf, buflen, 1, 16, 3);
\(rA "6.250"
strpct(buf, buflen, 1, 2, 0);
\(rA "50"
.Ed
.Sh HISTORY
.Fn strpct
was originally implemented in
.Xr csh 1 .
It printed into a static buffer, was not locale aware, handled 
.Ft unsigned long
numbers, and printed a
.Dq % 
at the end of the number.
Other programs such as
.Xr df 1
and
.Xr time 1
started using it.
.Fn strpct
appeared in
.Nx 6 .

File Added: src/lib/libutil/strpct.c
/* $NetBSD: strpct.c,v 1.1 2011/08/28 07:45:14 christos Exp $ */

/*-
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Erik E. Fair
 *
 * 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 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.
 */

/*
 * Calculate a percentage without resorting to floating point
 * and return a pointer to a string
 *
 * "digits" is the number of digits past the decimal place you want
 * (zero being the straight percentage with no decimals)
 *
 * Erik E. Fair <fair@clock.org>, May 8, 1997
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: strpct.c,v 1.1 2011/08/28 07:45:14 christos Exp $");

#include <stdint.h>
#include <locale.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <util.h>

char *
strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator,
    size_t digits)
{
	uintmax_t factor, result;
	size_t u;

	/* I should check for digit overflow here, too XXX */
	factor = 100;
	for (u = 0; u < digits; u++) {
		/* watch out for overflow! */
		if (factor < (UINTMAX_MAX / 10))
			factor *= 10;
		else
			break;
	}

	/* watch out for overflow! */
	if (numerator < (UINTMAX_MAX / factor))
		numerator *= factor;
	else {
		/* toss some of the bits of lesser significance */
		denominator /= factor;
	}

	if (denominator == 0)
		denominator = 1;

	result = numerator / denominator;

	if (digits == 0)
		(void)snprintf(buf, bufsiz, "%ju", result);
	else {
		factor /= 100;		/* undo initialization */

		(void)snprintf(buf, sizeof(buf), "%ju%s%0*ju",
		    result / factor, localeconv()->decimal_point, (int)u,
		    result % factor);
	}       

	return buf;
}