Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.1
      1  1.1  christos /*	$NetBSD: snprintb.c,v 1.1 2008/12/16 22:33:11 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #ifndef _KERNEL
     30  1.1  christos 
     31  1.1  christos # if HAVE_NBTOOL_CONFIG_H
     32  1.1  christos #  include "nbtool_config.h"
     33  1.1  christos # endif
     34  1.1  christos 
     35  1.1  christos # include <sys/cdefs.h>
     36  1.1  christos # if defined(LIBC_SCCS) && !defined(lint)
     37  1.1  christos __RCSID("$NetBSD: snprintb.c,v 1.1 2008/12/16 22:33:11 christos Exp $");
     38  1.1  christos # endif
     39  1.1  christos 
     40  1.1  christos /*
     41  1.1  christos  * snprintb: print an interpreted bitmask to a buffer
     42  1.1  christos  *
     43  1.1  christos  * => returns the length of the buffer that would require to print the string.
     44  1.1  christos  */
     45  1.1  christos 
     46  1.1  christos # include <sys/types.h>
     47  1.1  christos # include <sys/inttypes.h>
     48  1.1  christos # include <stdio.h>
     49  1.1  christos # include <util.h>
     50  1.1  christos # include <errno.h>
     51  1.1  christos # else
     52  1.1  christos # include <sys/cdefs.h>
     53  1.1  christos __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.1 2008/12/16 22:33:11 christos Exp $");
     54  1.1  christos # include <sys/types.h>
     55  1.1  christos # include <sys/inttypes.h>
     56  1.1  christos # include <sys/systm.h>
     57  1.1  christos # include <lib/libkern/libkern.h>
     58  1.1  christos #endif
     59  1.1  christos 
     60  1.1  christos int
     61  1.1  christos snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val)
     62  1.1  christos {
     63  1.1  christos 	char *bp = buf;
     64  1.1  christos 	const char *sbase;
     65  1.1  christos 	int bit, ch, len, sep, flen;
     66  1.1  christos 	uint64_t field;
     67  1.1  christos 
     68  1.1  christos #ifdef _KERNEL
     69  1.1  christos 	/*
     70  1.1  christos 	 * For safety; no other *s*printf() do this, but in the kernel
     71  1.1  christos 	 * we don't usually check the return value
     72  1.1  christos 	 */
     73  1.1  christos 	(void)memset(buf, 0, buflen);
     74  1.1  christos #endif /* _KERNEL */
     75  1.1  christos 
     76  1.1  christos 	ch = *bitfmt++;
     77  1.1  christos 	switch (ch != '\177' ? ch : *bitfmt++) {
     78  1.1  christos 	case 8:
     79  1.1  christos 		sbase = "0%" PRIo64;
     80  1.1  christos 		break;
     81  1.1  christos 	case 10:
     82  1.1  christos 		sbase = "%" PRId64;
     83  1.1  christos 		break;
     84  1.1  christos 	case 16:
     85  1.1  christos 		sbase = "0x%" PRIx64;
     86  1.1  christos 		break;
     87  1.1  christos 	default:
     88  1.1  christos 		goto internal;
     89  1.1  christos 	}
     90  1.1  christos 
     91  1.1  christos 	len = snprintf(bp, buflen, sbase, val);
     92  1.1  christos 	if (len < 0)
     93  1.1  christos 		goto internal;
     94  1.1  christos 
     95  1.1  christos 	if (len < buflen)
     96  1.1  christos 		bp += len;
     97  1.1  christos 	else
     98  1.1  christos 		bp += buflen - 1;
     99  1.1  christos 
    100  1.1  christos 	/*
    101  1.1  christos 	 * If the value we printed was 0 and we're using the old-style format,
    102  1.1  christos 	 * we're done.
    103  1.1  christos 	 */
    104  1.1  christos 	if ((val == 0) && (ch != '\177'))
    105  1.1  christos 		goto terminate;
    106  1.1  christos 
    107  1.1  christos #define PUTC(c) if (++len < buflen) *bp++ = (c)
    108  1.1  christos #define PUTS(s) while ((ch = *(s)++) != 0) PUTC(ch)
    109  1.1  christos 
    110  1.1  christos 	/*
    111  1.1  christos 	 * Chris Torek's new bitmask format is identified by a leading \177
    112  1.1  christos 	 */
    113  1.1  christos 	sep = '<';
    114  1.1  christos 	if (ch != '\177') {
    115  1.1  christos 		/* old (standard) format. */
    116  1.1  christos 		for (;(bit = *bitfmt++) != 0;) {
    117  1.1  christos 			if (val & (1 << (bit - 1))) {
    118  1.1  christos 				PUTC(sep);
    119  1.1  christos 				for (; (ch = *bitfmt) > ' '; ++bitfmt)
    120  1.1  christos 					PUTC(ch);
    121  1.1  christos 				sep = ',';
    122  1.1  christos 			} else
    123  1.1  christos 				for (; *bitfmt > ' '; ++bitfmt)
    124  1.1  christos 					continue;
    125  1.1  christos 		}
    126  1.1  christos 	} else {
    127  1.1  christos 		/* new quad-capable format; also does fields. */
    128  1.1  christos 		field = val;
    129  1.1  christos 		while ((ch = *bitfmt++) != '\0') {
    130  1.1  christos 			bit = *bitfmt++;	/* now 0-origin */
    131  1.1  christos 			switch (ch) {
    132  1.1  christos 			case 'b':
    133  1.1  christos 				if (((u_int)(val >> bit) & 1) == 0)
    134  1.1  christos 					goto skip;
    135  1.1  christos 				PUTC(sep);
    136  1.1  christos 				PUTS(bitfmt);
    137  1.1  christos 				sep = ',';
    138  1.1  christos 				break;
    139  1.1  christos 			case 'f':
    140  1.1  christos 			case 'F':
    141  1.1  christos 				flen = *bitfmt++;	/* field length */
    142  1.1  christos 				field = (val >> bit) &
    143  1.1  christos 					    (((uint64_t)1 << flen) - 1);
    144  1.1  christos 				if (ch == 'F')	/* just extract */
    145  1.1  christos 					break;
    146  1.1  christos 				PUTC(sep);
    147  1.1  christos 				sep = ',';
    148  1.1  christos 				PUTS(bitfmt);
    149  1.1  christos 				PUTC('=');
    150  1.1  christos 				flen = snprintf(bp, buflen - len, sbase, field);
    151  1.1  christos 				if (flen < 0)
    152  1.1  christos 					goto internal;
    153  1.1  christos 				len += flen;
    154  1.1  christos 				if (len < buflen)
    155  1.1  christos 					bp += flen;
    156  1.1  christos 				break;
    157  1.1  christos 			case '=':
    158  1.1  christos 			case ':':
    159  1.1  christos 				/*
    160  1.1  christos 				 * Here "bit" is actually a value instead,
    161  1.1  christos 				 * to be compared against the last field.
    162  1.1  christos 				 * This only works for values in [0..255],
    163  1.1  christos 				 * of course.
    164  1.1  christos 				 */
    165  1.1  christos 				if ((int)field != bit)
    166  1.1  christos 					goto skip;
    167  1.1  christos 				if (ch == '=') {
    168  1.1  christos 					PUTC('=');
    169  1.1  christos 				}
    170  1.1  christos 				PUTS(bitfmt);
    171  1.1  christos 				break;
    172  1.1  christos 			default:
    173  1.1  christos 			skip:
    174  1.1  christos 				while (*bitfmt++ != '\0')
    175  1.1  christos 					continue;
    176  1.1  christos 				break;
    177  1.1  christos 			}
    178  1.1  christos 		}
    179  1.1  christos 	}
    180  1.1  christos 	if (sep != '<') {
    181  1.1  christos 		PUTC('>');
    182  1.1  christos 	}
    183  1.1  christos terminate:
    184  1.1  christos 	*bp = '\0';
    185  1.1  christos 	return len;
    186  1.1  christos internal:
    187  1.1  christos #ifndef _KERNEL
    188  1.1  christos 	errno = EINVAL;
    189  1.1  christos #endif
    190  1.1  christos 	return -1;
    191  1.1  christos }
    192