Source-Changes-HG archive

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

[src/trunk]: src/lib/libutil - add strspct



details:   https://anonhg.NetBSD.org/src/rev/26e1870e98e4
branches:  trunk
changeset: 772536:26e1870e98e4
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jan 07 18:40:55 2012 +0000

description:
- add strspct
- be explicit about string not being NUL terminated if bufsiz == 0

diffstat:

 lib/libutil/Makefile |   3 ++-
 lib/libutil/strpct.3 |  21 ++++++++++++++++-----
 lib/libutil/strpct.c |  39 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 55 insertions(+), 8 deletions(-)

diffs (134 lines):

diff -r 4766e02237e0 -r 26e1870e98e4 lib/libutil/Makefile
--- a/lib/libutil/Makefile      Sat Jan 07 18:10:18 2012 +0000
+++ b/lib/libutil/Makefile      Sat Jan 07 18:40:55 2012 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.69 2011/11/13 22:03:34 christos Exp $
+#      $NetBSD: Makefile,v 1.70 2012/01/07 18:40:55 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/4/93
 
 USE_SHLIBDIR=  yes
@@ -80,5 +80,6 @@
 MLINKS+=stat_flags.3 flags_to_string.3
 MLINKS+=snprintb.3 snprintb_m.3
 MLINKS+=util.3 libutil.3
+MLINKS+=strpct.3 strspct.3
 
 .include <bsd.lib.mk>
diff -r 4766e02237e0 -r 26e1870e98e4 lib/libutil/strpct.3
--- a/lib/libutil/strpct.3      Sat Jan 07 18:10:18 2012 +0000
+++ b/lib/libutil/strpct.3      Sat Jan 07 18:40:55 2012 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: strpct.3,v 1.3 2011/09/01 23:13:16 fair Exp $
+.\" $NetBSD: strpct.3,v 1.4 2012/01/07 18:40:55 christos Exp $
 .\"
 .\" Copyright (c) 2011 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -26,18 +26,21 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd September 1, 2011
+.Dd January 7, 2012
 .Dt STRPCT 3
 .Os
 .Sh NAME
-.Nm strpct
-.Nd decimal percent formatter
+.Nm strpct ,
+.Nm strspct
+.Nd decimal percent formatters
 .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"
+.Ft char *
+.Fn strspct "char *buf" "size_t bufsiz" "intmax_t numerator" "intmax_t denominator" "size_t precision"
 .Sh DESCRIPTION
 The
 .Fn strpct
@@ -50,7 +53,13 @@
 without using floating point arithmetic.
 .Sh RETURN VALUES
 .Fn strpct
-always returns a pointer to a NUL-terminated formatted string which
+and
+.Fn strspct
+always return a pointer to a NUL-terminated (unless
+.Fa buflen
+is
+.Dv 0 )
+formatted string which
 is placed in
 .Fa buf
 and is up to
@@ -82,6 +91,8 @@
 .Xr time 1
 started using it.
 .Fn strpct
+and
+.Fn strspct
 appeared separately in libutil for
 .Nx 6.0 .
 .Sh AUTHORS
diff -r 4766e02237e0 -r 26e1870e98e4 lib/libutil/strpct.c
--- a/lib/libutil/strpct.c      Sat Jan 07 18:10:18 2012 +0000
+++ b/lib/libutil/strpct.c      Sat Jan 07 18:40:55 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: strpct.c,v 1.2 2011/09/02 10:13:44 christos Exp $ */
+/* $NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $ */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: strpct.c,v 1.2 2011/09/02 10:13:44 christos Exp $");
+__RCSID("$NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $");
 
 #include <stdint.h>
 #include <locale.h>
@@ -50,6 +50,41 @@
 #include <util.h>
 
 char *
+strspct(char *buf, size_t bufsiz, intmax_t numerator, intmax_t denominator,
+    size_t digits)
+{
+       int sign;
+
+       switch (bufsiz) {
+       case 1:
+               *buf = '\0';
+               /*FALLTHROUGH*/
+       case 0:
+               return buf;
+       default:
+               break;
+       }
+
+       if (denominator < 0) {
+               denominator = -denominator;
+               sign = 1;
+       } else
+               sign = 0;
+
+       if (numerator < 0) {
+               numerator = -numerator;
+               sign++;
+       }
+
+       sign &= 1;
+       (void)strpct(buf + sign, bufsiz - sign, (uintmax_t)numerator,
+           (uintmax_t)denominator, digits);
+       if (sign)
+               *buf = '-';
+       return buf;
+}
+
+char *
 strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator,
     size_t digits)
 {



Home | Main Index | Thread Index | Old Index