xref: /freebsd-src/usr.sbin/ifmcstat/printb.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
43c0f8ff5SBruce M Simpson  * Copyright (c) 1983, 1993
53c0f8ff5SBruce M Simpson  *	The Regents of the University of California.  All rights reserved.
63c0f8ff5SBruce M Simpson  *
73c0f8ff5SBruce M Simpson  * Redistribution and use in source and binary forms, with or without
83c0f8ff5SBruce M Simpson  * modification, are permitted provided that the following conditions
93c0f8ff5SBruce M Simpson  * are met:
103c0f8ff5SBruce M Simpson  * 1. Redistributions of source code must retain the above copyright
113c0f8ff5SBruce M Simpson  *    notice, this list of conditions and the following disclaimer.
123c0f8ff5SBruce M Simpson  * 2. Redistributions in binary form must reproduce the above copyright
133c0f8ff5SBruce M Simpson  *    notice, this list of conditions and the following disclaimer in the
143c0f8ff5SBruce M Simpson  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
163c0f8ff5SBruce M Simpson  *    may be used to endorse or promote products derived from this software
173c0f8ff5SBruce M Simpson  *    without specific prior written permission.
183c0f8ff5SBruce M Simpson  *
193c0f8ff5SBruce M Simpson  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
203c0f8ff5SBruce M Simpson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213c0f8ff5SBruce M Simpson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223c0f8ff5SBruce M Simpson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
233c0f8ff5SBruce M Simpson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243c0f8ff5SBruce M Simpson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253c0f8ff5SBruce M Simpson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263c0f8ff5SBruce M Simpson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273c0f8ff5SBruce M Simpson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283c0f8ff5SBruce M Simpson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293c0f8ff5SBruce M Simpson  * SUCH DAMAGE.
303c0f8ff5SBruce M Simpson  */
313c0f8ff5SBruce M Simpson 
323c0f8ff5SBruce M Simpson #include <sys/cdefs.h>
33*65273b48SWarner Losh #include <assert.h>
343c0f8ff5SBruce M Simpson #include <stdio.h>
353c0f8ff5SBruce M Simpson 
363c0f8ff5SBruce M Simpson /*
373c0f8ff5SBruce M Simpson  * Print a value a la the %b format of the kernel's printf
383c0f8ff5SBruce M Simpson  */
393c0f8ff5SBruce M Simpson void
printb(const char * s,unsigned int v,const char * bits)403c0f8ff5SBruce M Simpson printb(const char *s, unsigned int v, const char *bits)
413c0f8ff5SBruce M Simpson {
423c0f8ff5SBruce M Simpson 	int i, any = 0;
433c0f8ff5SBruce M Simpson 	char c;
443c0f8ff5SBruce M Simpson 
45*65273b48SWarner Losh 	assert(bits != NULL);
46*65273b48SWarner Losh 	if (*bits == 8)
473c0f8ff5SBruce M Simpson 		printf("%s=%o", s, v);
483c0f8ff5SBruce M Simpson 	else
493c0f8ff5SBruce M Simpson 		printf("%s=%x", s, v);
503c0f8ff5SBruce M Simpson 	bits++;
513c0f8ff5SBruce M Simpson 	if (bits) {
523c0f8ff5SBruce M Simpson 		putchar('<');
533c0f8ff5SBruce M Simpson 		while ((i = *bits++) != '\0') {
543c0f8ff5SBruce M Simpson 			if (v & (1 << (i-1))) {
553c0f8ff5SBruce M Simpson 				if (any)
563c0f8ff5SBruce M Simpson 					putchar(',');
573c0f8ff5SBruce M Simpson 				any = 1;
583c0f8ff5SBruce M Simpson 				for (; (c = *bits) > 32; bits++)
593c0f8ff5SBruce M Simpson 					putchar(c);
603c0f8ff5SBruce M Simpson 			} else
613c0f8ff5SBruce M Simpson 				for (; *bits > 32; bits++)
623c0f8ff5SBruce M Simpson 					;
633c0f8ff5SBruce M Simpson 		}
643c0f8ff5SBruce M Simpson 		putchar('>');
653c0f8ff5SBruce M Simpson 	}
663c0f8ff5SBruce M Simpson }
67