xref: /netbsd/sys/kern/subr_humanize.c (revision 6ac4a90a)
1*6ac4a90aSkre /*	$NetBSD: subr_humanize.c,v 1.2 2019/03/12 00:25:44 kre Exp $	*/
268f37adaSpooka 
368f37adaSpooka /*-
468f37adaSpooka  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
568f37adaSpooka  * All rights reserved.
668f37adaSpooka  *
768f37adaSpooka  * This code is derived from software contributed to The NetBSD Foundation
868f37adaSpooka  * by Luke Mewburn.
968f37adaSpooka  *
1068f37adaSpooka  * Redistribution and use in source and binary forms, with or without
1168f37adaSpooka  * modification, are permitted provided that the following conditions
1268f37adaSpooka  * are met:
1368f37adaSpooka  * 1. Redistributions of source code must retain the above copyright
1468f37adaSpooka  *    notice, this list of conditions and the following disclaimer.
1568f37adaSpooka  * 2. Redistributions in binary form must reproduce the above copyright
1668f37adaSpooka  *    notice, this list of conditions and the following disclaimer in the
1768f37adaSpooka  *    documentation and/or other materials provided with the distribution.
1868f37adaSpooka  *
1968f37adaSpooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2068f37adaSpooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2168f37adaSpooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2268f37adaSpooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2368f37adaSpooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2468f37adaSpooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2568f37adaSpooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2668f37adaSpooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2768f37adaSpooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2868f37adaSpooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2968f37adaSpooka  * POSSIBILITY OF SUCH DAMAGE.
3068f37adaSpooka  */
3168f37adaSpooka 
3268f37adaSpooka #include <sys/cdefs.h>
33*6ac4a90aSkre __KERNEL_RCSID(0, "$NetBSD: subr_humanize.c,v 1.2 2019/03/12 00:25:44 kre Exp $");
3468f37adaSpooka 
3568f37adaSpooka #include <sys/types.h>
3668f37adaSpooka #include <sys/systm.h>
3768f37adaSpooka 
3868f37adaSpooka /*
3968f37adaSpooka  * snprintf() `bytes' into `buf', reformatting it so that the number,
4068f37adaSpooka  * plus a possible `x' + suffix extension) fits into len bytes (including
4168f37adaSpooka  * the terminating NUL).
4268f37adaSpooka  * Returns the number of bytes stored in buf, or -1 if there was a problem.
4368f37adaSpooka  * E.g, given a len of 9 and a suffix of `B':
4468f37adaSpooka  *	bytes		result
4568f37adaSpooka  *	-----		------
4668f37adaSpooka  *	99999		`99999 B'
4768f37adaSpooka  *	100000		`97 kB'
4868f37adaSpooka  *	66715648	`65152 kB'
4968f37adaSpooka  *	252215296	`240 MB'
5068f37adaSpooka  */
5168f37adaSpooka int
humanize_number(char * buf,size_t len,uint64_t bytes,const char * suffix,int divisor)5268f37adaSpooka humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix,
5368f37adaSpooka     int divisor)
5468f37adaSpooka {
5568f37adaSpooka        	/* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
5668f37adaSpooka 	const char *prefixes;
5768f37adaSpooka 	int		r;
58*6ac4a90aSkre 	uint64_t	umax, b10;
5968f37adaSpooka 	size_t		i, suffixlen;
6068f37adaSpooka 
6168f37adaSpooka 	if (buf == NULL || suffix == NULL)
6268f37adaSpooka 		return (-1);
6368f37adaSpooka 	if (len > 0)
6468f37adaSpooka 		buf[0] = '\0';
6568f37adaSpooka 	suffixlen = strlen(suffix);
6668f37adaSpooka 	/* check if enough room for `x y' + suffix + `\0' */
6768f37adaSpooka 	if (len < 4 + suffixlen)
6868f37adaSpooka 		return (-1);
6968f37adaSpooka 
7068f37adaSpooka 	if (divisor == 1024) {
7168f37adaSpooka 		/*
7268f37adaSpooka 		 * binary multiplies
7368f37adaSpooka 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
7468f37adaSpooka 		 */
7568f37adaSpooka 		prefixes = " KMGTPE";
7668f37adaSpooka 	} else
7768f37adaSpooka 		prefixes = " kMGTPE"; /* SI for decimal multiplies */
7868f37adaSpooka 
7968f37adaSpooka 	umax = 1;
80*6ac4a90aSkre 	b10 = bytes/10;
8168f37adaSpooka 	for (i = 0; i < len - suffixlen - 3; i++) {
82*6ac4a90aSkre 		if (umax > b10) {
83*6ac4a90aSkre 			/*
84*6ac4a90aSkre 			 * there is space for the unscaled number
85*6ac4a90aSkre 			 * but bytes might be ~0 - there is no bigger
86*6ac4a90aSkre 			 * value available for umax, so we must skip
87*6ac4a90aSkre 			 * the "bytes >= umax" test below
88*6ac4a90aSkre 			 */
89*6ac4a90aSkre 			i = 0;
90*6ac4a90aSkre 			goto nodiv;
91*6ac4a90aSkre 		}
9268f37adaSpooka 		umax *= 10;
9368f37adaSpooka 	}
9468f37adaSpooka 	for (i = 0; bytes >= umax && prefixes[i + 1]; i++)
9568f37adaSpooka 		bytes /= divisor;
96*6ac4a90aSkre  nodiv:
9768f37adaSpooka 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
9868f37adaSpooka 	    i == 0 ? "" : " ", prefixes[i], suffix);
9968f37adaSpooka 
10068f37adaSpooka 	return (r);
10168f37adaSpooka }
10268f37adaSpooka 
10368f37adaSpooka int
format_bytes(char * buf,size_t len,uint64_t bytes)10468f37adaSpooka format_bytes(char *buf, size_t len, uint64_t bytes)
10568f37adaSpooka {
10668f37adaSpooka 	int	rv;
10768f37adaSpooka 	size_t	nlen;
10868f37adaSpooka 
10968f37adaSpooka 	rv = humanize_number(buf, len, bytes, "B", 1024);
11068f37adaSpooka 	if (rv != -1) {
11168f37adaSpooka 			/* nuke the trailing ` B' if it exists */
11268f37adaSpooka 		nlen = strlen(buf) - 2;
11368f37adaSpooka 		if (strcmp(&buf[nlen], " B") == 0)
11468f37adaSpooka 			buf[nlen] = '\0';
11568f37adaSpooka 	}
11668f37adaSpooka 	return (rv);
11768f37adaSpooka }
118