Module Name: src Committed By: christos Date: Sun Aug 28 07:45:14 UTC 2011
Modified Files: src/lib/libutil: Makefile shlib_version Added Files: src/lib/libutil: strpct.3 strpct.c Log Message: add strpct, requested by joerg To generate a diff of this commit: cvs rdiff -u -r1.63 -r1.64 src/lib/libutil/Makefile cvs rdiff -u -r1.47 -r1.48 src/lib/libutil/shlib_version cvs rdiff -u -r0 -r1.1 src/lib/libutil/strpct.3 src/lib/libutil/strpct.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libutil/Makefile diff -u src/lib/libutil/Makefile:1.63 src/lib/libutil/Makefile:1.64 --- src/lib/libutil/Makefile:1.63 Wed Jan 27 14:10:31 2010 +++ src/lib/libutil/Makefile Sun Aug 28 03:45:13 2011 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.63 2010/01/27 19:10:31 drochner Exp $ +# $NetBSD: Makefile,v 1.64 2011/08/28 07:45:13 christos Exp $ # @(#)Makefile 8.1 (Berkeley) 6/4/93 USE_SHLIBDIR= yes @@ -19,7 +19,7 @@ passwd.c pw_scan.c pidfile.c pidlock.c pty.c \ raise_default_signal.c \ secure_path.c snprintb.c sockaddr_snprintf.c stat_flags.c \ - ttyaction.c ttymsg.c + strpct.c ttyaction.c ttymsg.c MAN= efun.3 getbootfile.3 getlabelsector.3 getmaxpartitions.3 \ getmntopts.3 \ @@ -29,7 +29,7 @@ opendisk.3 openpty.3 parsedate.3 pidfile.3 pidlock.3 \ pw_getconf.3 pw_init.3 pw_lock.3 secure_path.3 \ raise_default_signal.3 \ - snprintb.3 sockaddr_snprintf.3 stat_flags.3 ttyaction.3 \ + snprintb.3 sockaddr_snprintf.3 stat_flags.3 strpct.3 ttyaction.3 \ ttymsg.3 util.3 YPREFIX=__pd Index: src/lib/libutil/shlib_version diff -u src/lib/libutil/shlib_version:1.47 src/lib/libutil/shlib_version:1.48 --- src/lib/libutil/shlib_version:1.47 Tue May 12 22:50:32 2009 +++ src/lib/libutil/shlib_version Sun Aug 28 03:45:14 2011 @@ -1,5 +1,5 @@ -# $NetBSD: shlib_version,v 1.47 2009/05/13 02:50:32 pgoyette Exp $ +# $NetBSD: shlib_version,v 1.48 2011/08/28 07:45:14 christos Exp $ # Remember to update distrib/sets/lists/base/shl.* when changing # major=7 -minor=17 +minor=18 Added files: Index: src/lib/libutil/strpct.3 diff -u /dev/null src/lib/libutil/strpct.3:1.1 --- /dev/null Sun Aug 28 03:45:14 2011 +++ src/lib/libutil/strpct.3 Sun Aug 28 03:45:14 2011 @@ -0,0 +1,83 @@ +.\" $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 . Index: src/lib/libutil/strpct.c diff -u /dev/null src/lib/libutil/strpct.c:1.1 --- /dev/null Sun Aug 28 03:45:14 2011 +++ src/lib/libutil/strpct.c Sun Aug 28 03:45:14 2011 @@ -0,0 +1,93 @@ +/* $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 <f...@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; +}