Source-Changes-HG archive

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

[src/trunk]: src/lib/libutil add strpct, requested by joerg



details:   https://anonhg.NetBSD.org/src/rev/26422f861b01
branches:  trunk
changeset: 768860:26422f861b01
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Aug 28 07:45:13 2011 +0000

description:
add strpct, requested by joerg

diffstat:

 lib/libutil/Makefile      |   6 +-
 lib/libutil/shlib_version |   4 +-
 lib/libutil/strpct.3      |  83 +++++++++++++++++++++++++++++++++++++++++
 lib/libutil/strpct.c      |  93 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+), 5 deletions(-)

diffs (222 lines):

diff -r 6c64db537dc0 -r 26422f861b01 lib/libutil/Makefile
--- a/lib/libutil/Makefile      Sun Aug 28 07:22:48 2011 +0000
+++ b/lib/libutil/Makefile      Sun Aug 28 07:45:13 2011 +0000
@@ -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
diff -r 6c64db537dc0 -r 26422f861b01 lib/libutil/shlib_version
--- a/lib/libutil/shlib_version Sun Aug 28 07:22:48 2011 +0000
+++ b/lib/libutil/shlib_version Sun Aug 28 07:45:13 2011 +0000
@@ -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
diff -r 6c64db537dc0 -r 26422f861b01 lib/libutil/strpct.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/strpct.3      Sun Aug 28 07:45:13 2011 +0000
@@ -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 .
diff -r 6c64db537dc0 -r 26422f861b01 lib/libutil/strpct.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libutil/strpct.c      Sun Aug 28 07:45:13 2011 +0000
@@ -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 <fair%clock.org@localhost>, 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;
+}



Home | Main Index | Thread Index | Old Index