- add strspct

- be explicit about string not being NUL terminated if bufsiz == 0
This commit is contained in:
christos 2012-01-07 18:40:55 +00:00
parent 01a67a2dc9
commit 89cf253562
3 changed files with 55 additions and 8 deletions

View File

@ -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>

View File

@ -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 given number of digits of
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

View File

@ -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>
@ -49,6 +49,41 @@ __RCSID("$NetBSD: strpct.c,v 1.2 2011/09/02 10:13:44 christos Exp $");
#include <errno.h>
#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)