Home | History | Annotate | Line # | Download | only in kern
subr_humanize.c revision 1.1
      1  1.1  pooka /*	$NetBSD: subr_humanize.c,v 1.1 2009/10/02 15:48:41 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pooka  * by Luke Mewburn.
      9  1.1  pooka  *
     10  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     11  1.1  pooka  * modification, are permitted provided that the following conditions
     12  1.1  pooka  * are met:
     13  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     18  1.1  pooka  *
     19  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  pooka  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  pooka  */
     31  1.1  pooka 
     32  1.1  pooka #include <sys/cdefs.h>
     33  1.1  pooka __KERNEL_RCSID(0, "$NetBSD: subr_humanize.c,v 1.1 2009/10/02 15:48:41 pooka Exp $");
     34  1.1  pooka 
     35  1.1  pooka #include <sys/types.h>
     36  1.1  pooka #include <sys/systm.h>
     37  1.1  pooka 
     38  1.1  pooka /*
     39  1.1  pooka  * snprintf() `bytes' into `buf', reformatting it so that the number,
     40  1.1  pooka  * plus a possible `x' + suffix extension) fits into len bytes (including
     41  1.1  pooka  * the terminating NUL).
     42  1.1  pooka  * Returns the number of bytes stored in buf, or -1 if there was a problem.
     43  1.1  pooka  * E.g, given a len of 9 and a suffix of `B':
     44  1.1  pooka  *	bytes		result
     45  1.1  pooka  *	-----		------
     46  1.1  pooka  *	99999		`99999 B'
     47  1.1  pooka  *	100000		`97 kB'
     48  1.1  pooka  *	66715648	`65152 kB'
     49  1.1  pooka  *	252215296	`240 MB'
     50  1.1  pooka  */
     51  1.1  pooka int
     52  1.1  pooka humanize_number(char *buf, size_t len, uint64_t bytes, const char *suffix,
     53  1.1  pooka     int divisor)
     54  1.1  pooka {
     55  1.1  pooka        	/* prefixes are: (none), kilo, Mega, Giga, Tera, Peta, Exa */
     56  1.1  pooka 	const char *prefixes;
     57  1.1  pooka 	int		r;
     58  1.1  pooka 	uint64_t	umax;
     59  1.1  pooka 	size_t		i, suffixlen;
     60  1.1  pooka 
     61  1.1  pooka 	if (buf == NULL || suffix == NULL)
     62  1.1  pooka 		return (-1);
     63  1.1  pooka 	if (len > 0)
     64  1.1  pooka 		buf[0] = '\0';
     65  1.1  pooka 	suffixlen = strlen(suffix);
     66  1.1  pooka 	/* check if enough room for `x y' + suffix + `\0' */
     67  1.1  pooka 	if (len < 4 + suffixlen)
     68  1.1  pooka 		return (-1);
     69  1.1  pooka 
     70  1.1  pooka 	if (divisor == 1024) {
     71  1.1  pooka 		/*
     72  1.1  pooka 		 * binary multiplies
     73  1.1  pooka 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
     74  1.1  pooka 		 */
     75  1.1  pooka 		prefixes = " KMGTPE";
     76  1.1  pooka 	} else
     77  1.1  pooka 		prefixes = " kMGTPE"; /* SI for decimal multiplies */
     78  1.1  pooka 
     79  1.1  pooka 	umax = 1;
     80  1.1  pooka 	for (i = 0; i < len - suffixlen - 3; i++) {
     81  1.1  pooka 		umax *= 10;
     82  1.1  pooka 		if (umax > bytes)
     83  1.1  pooka 			break;
     84  1.1  pooka 	}
     85  1.1  pooka 	for (i = 0; bytes >= umax && prefixes[i + 1]; i++)
     86  1.1  pooka 		bytes /= divisor;
     87  1.1  pooka 
     88  1.1  pooka 	r = snprintf(buf, len, "%qu%s%c%s", (unsigned long long)bytes,
     89  1.1  pooka 	    i == 0 ? "" : " ", prefixes[i], suffix);
     90  1.1  pooka 
     91  1.1  pooka 	return (r);
     92  1.1  pooka }
     93  1.1  pooka 
     94  1.1  pooka int
     95  1.1  pooka format_bytes(char *buf, size_t len, uint64_t bytes)
     96  1.1  pooka {
     97  1.1  pooka 	int	rv;
     98  1.1  pooka 	size_t	nlen;
     99  1.1  pooka 
    100  1.1  pooka 	rv = humanize_number(buf, len, bytes, "B", 1024);
    101  1.1  pooka 	if (rv != -1) {
    102  1.1  pooka 			/* nuke the trailing ` B' if it exists */
    103  1.1  pooka 		nlen = strlen(buf) - 2;
    104  1.1  pooka 		if (strcmp(&buf[nlen], " B") == 0)
    105  1.1  pooka 			buf[nlen] = '\0';
    106  1.1  pooka 	}
    107  1.1  pooka 	return (rv);
    108  1.1  pooka }
    109