xref: /minix3/lib/libutil/strpct.c (revision dba3562d7800d1fed3cb2cd859754872fbb2e84f)
1*dba3562dSLionel Sambuc /* $NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $ */
2*dba3562dSLionel Sambuc 
3*dba3562dSLionel Sambuc /*-
4*dba3562dSLionel Sambuc  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5*dba3562dSLionel Sambuc  * All rights reserved.
6*dba3562dSLionel Sambuc  *
7*dba3562dSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*dba3562dSLionel Sambuc  * by Erik E. Fair
9*dba3562dSLionel Sambuc  *
10*dba3562dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*dba3562dSLionel Sambuc  * modification, are permitted provided that the following conditions
12*dba3562dSLionel Sambuc  * are met:
13*dba3562dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*dba3562dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*dba3562dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*dba3562dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*dba3562dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*dba3562dSLionel Sambuc  *
19*dba3562dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*dba3562dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*dba3562dSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*dba3562dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*dba3562dSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*dba3562dSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*dba3562dSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*dba3562dSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*dba3562dSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*dba3562dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*dba3562dSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*dba3562dSLionel Sambuc  */
31*dba3562dSLionel Sambuc 
32*dba3562dSLionel Sambuc /*
33*dba3562dSLionel Sambuc  * Calculate a percentage without resorting to floating point
34*dba3562dSLionel Sambuc  * and return a pointer to a string
35*dba3562dSLionel Sambuc  *
36*dba3562dSLionel Sambuc  * "digits" is the number of digits past the decimal place you want
37*dba3562dSLionel Sambuc  * (zero being the straight percentage with no decimals)
38*dba3562dSLionel Sambuc  *
39*dba3562dSLionel Sambuc  * Erik E. Fair <fair@clock.org>, May 8, 1997
40*dba3562dSLionel Sambuc  */
41*dba3562dSLionel Sambuc 
42*dba3562dSLionel Sambuc #include <sys/cdefs.h>
43*dba3562dSLionel Sambuc __RCSID("$NetBSD: strpct.c,v 1.3 2012/01/07 18:40:56 christos Exp $");
44*dba3562dSLionel Sambuc 
45*dba3562dSLionel Sambuc #include <stdint.h>
46*dba3562dSLionel Sambuc #include <locale.h>
47*dba3562dSLionel Sambuc #include <limits.h>
48*dba3562dSLionel Sambuc #include <stdio.h>
49*dba3562dSLionel Sambuc #include <errno.h>
50*dba3562dSLionel Sambuc #include <util.h>
51*dba3562dSLionel Sambuc 
52*dba3562dSLionel Sambuc char *
strspct(char * buf,size_t bufsiz,intmax_t numerator,intmax_t denominator,size_t digits)53*dba3562dSLionel Sambuc strspct(char *buf, size_t bufsiz, intmax_t numerator, intmax_t denominator,
54*dba3562dSLionel Sambuc     size_t digits)
55*dba3562dSLionel Sambuc {
56*dba3562dSLionel Sambuc 	int sign;
57*dba3562dSLionel Sambuc 
58*dba3562dSLionel Sambuc 	switch (bufsiz) {
59*dba3562dSLionel Sambuc 	case 1:
60*dba3562dSLionel Sambuc 		*buf = '\0';
61*dba3562dSLionel Sambuc 		/*FALLTHROUGH*/
62*dba3562dSLionel Sambuc 	case 0:
63*dba3562dSLionel Sambuc 		return buf;
64*dba3562dSLionel Sambuc 	default:
65*dba3562dSLionel Sambuc 		break;
66*dba3562dSLionel Sambuc 	}
67*dba3562dSLionel Sambuc 
68*dba3562dSLionel Sambuc 	if (denominator < 0) {
69*dba3562dSLionel Sambuc 		denominator = -denominator;
70*dba3562dSLionel Sambuc 		sign = 1;
71*dba3562dSLionel Sambuc 	} else
72*dba3562dSLionel Sambuc 		sign = 0;
73*dba3562dSLionel Sambuc 
74*dba3562dSLionel Sambuc 	if (numerator < 0) {
75*dba3562dSLionel Sambuc 		numerator = -numerator;
76*dba3562dSLionel Sambuc 		sign++;
77*dba3562dSLionel Sambuc 	}
78*dba3562dSLionel Sambuc 
79*dba3562dSLionel Sambuc 	sign &= 1;
80*dba3562dSLionel Sambuc 	(void)strpct(buf + sign, bufsiz - sign, (uintmax_t)numerator,
81*dba3562dSLionel Sambuc 	    (uintmax_t)denominator, digits);
82*dba3562dSLionel Sambuc 	if (sign)
83*dba3562dSLionel Sambuc 		*buf = '-';
84*dba3562dSLionel Sambuc 	return buf;
85*dba3562dSLionel Sambuc }
86*dba3562dSLionel Sambuc 
87*dba3562dSLionel Sambuc char *
strpct(char * buf,size_t bufsiz,uintmax_t numerator,uintmax_t denominator,size_t digits)88*dba3562dSLionel Sambuc strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator,
89*dba3562dSLionel Sambuc     size_t digits)
90*dba3562dSLionel Sambuc {
91*dba3562dSLionel Sambuc 	uintmax_t factor, result;
92*dba3562dSLionel Sambuc 	size_t u;
93*dba3562dSLionel Sambuc 
94*dba3562dSLionel Sambuc 	factor = 100;
95*dba3562dSLionel Sambuc 	for (u = 0; u < digits; u++) {
96*dba3562dSLionel Sambuc 		/* watch out for overflow! */
97*dba3562dSLionel Sambuc 		if (factor < (UINTMAX_MAX / 10))
98*dba3562dSLionel Sambuc 			factor *= 10;
99*dba3562dSLionel Sambuc 		else
100*dba3562dSLionel Sambuc 			break;
101*dba3562dSLionel Sambuc 	}
102*dba3562dSLionel Sambuc 
103*dba3562dSLionel Sambuc 	/* watch out for overflow! */
104*dba3562dSLionel Sambuc 	if (numerator < (UINTMAX_MAX / factor))
105*dba3562dSLionel Sambuc 		numerator *= factor;
106*dba3562dSLionel Sambuc 	else {
107*dba3562dSLionel Sambuc 		/* toss some of the bits of lesser significance */
108*dba3562dSLionel Sambuc 		denominator /= factor;
109*dba3562dSLionel Sambuc 	}
110*dba3562dSLionel Sambuc 
111*dba3562dSLionel Sambuc 	if (denominator == 0)
112*dba3562dSLionel Sambuc 		denominator = 1;
113*dba3562dSLionel Sambuc 
114*dba3562dSLionel Sambuc 	result = numerator / denominator;
115*dba3562dSLionel Sambuc 
116*dba3562dSLionel Sambuc 	if (digits == 0)
117*dba3562dSLionel Sambuc 		(void)snprintf(buf, bufsiz, "%ju", result);
118*dba3562dSLionel Sambuc 	else {
119*dba3562dSLionel Sambuc 		factor /= 100;		/* undo initialization */
120*dba3562dSLionel Sambuc 
121*dba3562dSLionel Sambuc 		(void)snprintf(buf, bufsiz, "%ju%s%0*ju",
122*dba3562dSLionel Sambuc 		    result / factor, localeconv()->decimal_point, (int)u,
123*dba3562dSLionel Sambuc 		    result % factor);
124*dba3562dSLionel Sambuc 	}
125*dba3562dSLionel Sambuc 
126*dba3562dSLionel Sambuc 	return buf;
127*dba3562dSLionel Sambuc }
128