Module Name: src Committed By: christos Date: Sat Jan 7 18:40:56 UTC 2012
Modified Files: src/lib/libutil: Makefile strpct.3 strpct.c Log Message: - add strspct - be explicit about string not being NUL terminated if bufsiz == 0 To generate a diff of this commit: cvs rdiff -u -r1.69 -r1.70 src/lib/libutil/Makefile cvs rdiff -u -r1.3 -r1.4 src/lib/libutil/strpct.3 cvs rdiff -u -r1.2 -r1.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.69 src/lib/libutil/Makefile:1.70 --- src/lib/libutil/Makefile:1.69 Sun Nov 13 17:03:34 2011 +++ src/lib/libutil/Makefile Sat Jan 7 13:40:55 2012 @@ -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 string_to_flags.3 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> Index: src/lib/libutil/strpct.3 diff -u src/lib/libutil/strpct.3:1.3 src/lib/libutil/strpct.3:1.4 --- src/lib/libutil/strpct.3:1.3 Thu Sep 1 19:13:16 2011 +++ src/lib/libutil/strpct.3 Sat Jan 7 13:40:55 2012 @@ -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 @@ into a percentage representation with gi 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 @@ and .Xr time 1 started using it. .Fn strpct +and +.Fn strspct appeared separately in libutil for .Nx 6.0 . .Sh AUTHORS Index: src/lib/libutil/strpct.c diff -u src/lib/libutil/strpct.c:1.2 src/lib/libutil/strpct.c:1.3 --- src/lib/libutil/strpct.c:1.2 Fri Sep 2 06:13:44 2011 +++ src/lib/libutil/strpct.c Sat Jan 7 13:40:56 2012 @@ -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 @@ __RCSID("$NetBSD: strpct.c,v 1.2 2011/09 #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) {