1 /* $NetBSD: strpct.c,v 1.6 2025/05/13 04:50:45 rillig Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2025 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Erik E. Fair and Roland Illig. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Calculate a percentage without resorting to floating point 34 * and return a pointer to a string 35 * 36 * "digits" is the number of digits past the decimal place you want 37 * (zero being the straight percentage with no decimals) 38 * 39 * Erik E. Fair <fair (at) clock.org>, May 8, 1997 40 */ 41 42 #include <sys/cdefs.h> 43 __RCSID("$NetBSD: strpct.c,v 1.6 2025/05/13 04:50:45 rillig Exp $"); 44 45 #include <limits.h> 46 #include <locale.h> 47 #include <stdbool.h> 48 #include <stdint.h> 49 #include <stdio.h> 50 #include <util.h> 51 52 static uintmax_t 53 imax_abs(intmax_t x) 54 { 55 56 return x < 0 ? -(uintmax_t)x : (uintmax_t)x; 57 } 58 59 char * 60 strspct(char *buf, size_t bufsiz, intmax_t numerator, intmax_t denominator, 61 size_t digits) 62 { 63 64 if (bufsiz == 1) 65 buf[0] = '\0'; 66 if (bufsiz <= 1) 67 return buf; 68 69 int sign = (numerator < 0) != (denominator < 0); 70 (void)strpct(buf + sign, bufsiz - sign, imax_abs(numerator), 71 imax_abs(denominator), digits); 72 if (sign) 73 *buf = '-'; 74 return buf; 75 } 76 77 typedef struct { 78 unsigned hi; 79 uintmax_t lo; 80 } bignum; 81 82 static bignum 83 bignum_plus(bignum x, bignum y) 84 { 85 x.hi += y.hi; 86 if (x.lo + y.lo < x.lo) 87 x.hi++; 88 x.lo += y.lo; 89 return x; 90 } 91 92 static bignum 93 bignum_minus_u(bignum x, uintmax_t y) 94 { 95 if (x.lo - y > x.lo) 96 x.hi--; 97 x.lo -= y; 98 return x; 99 } 100 101 static bignum 102 bignum_times_10(bignum x) 103 { 104 bignum x2 = bignum_plus(x, x); 105 bignum x4 = bignum_plus(x2, x2); 106 bignum x8 = bignum_plus(x4, x4); 107 return bignum_plus(x2, x8); 108 } 109 110 static bool 111 bignum_ge_u(bignum x, uintmax_t y) 112 { 113 return x.hi > 0 || x.lo >= y; 114 } 115 116 char * 117 strpct(char *buf, size_t bufsiz, uintmax_t numerator, uintmax_t denominator, 118 size_t digits) 119 { 120 char *p = buf; 121 size_t n = bufsiz; 122 123 if (denominator == 0) 124 denominator = 1; 125 126 if (numerator >= denominator) { 127 size_t nw = snprintf(p, n, "%ju", numerator / denominator); 128 if (nw >= n) 129 return buf; 130 p += nw, n -= nw; 131 numerator %= denominator; 132 } 133 134 bignum num = { 0, numerator }; 135 for (size_t i = 0; i < 2 + digits; i++) { 136 num = bignum_times_10(num); 137 138 unsigned digit = 0; 139 for (; bignum_ge_u(num, denominator); digit++) 140 num = bignum_minus_u(num, denominator); 141 142 if (i > 0 || p > buf || digit > 0) { 143 if (n >= 2) 144 *p++ = '0' + digit, n--; 145 if (n >= 1) 146 *p = '\0'; 147 if (n <= 1) 148 break; 149 } 150 151 if (i == 1 && digits > 0) { 152 const char *dp = localeconv()->decimal_point; 153 while (*dp != '\0' && n >= 2) 154 *p++ = *dp++, n--; 155 if (n >= 1) 156 *p = '\0'; 157 if (n <= 1) 158 break; 159 } 160 } 161 return buf; 162 } 163