FreeBSD/contrib/ntp/util/ntptime.c
/*
 * Print a value a la the %b format of the kernel's printf
 */
Print a value a la the %b format of the kernel's printf
Print a value a la the %b format of the kernel ' s printf
const char *
sprintb(
	u_int		v,
	const char *	bits
	)
{
	char *cp;
	char *cplim;
	int i;
	int any;
	char c;
	static char buf[132];
	if (bits != NULL && *bits == 8)
		snprintf(buf, sizeof(buf), "0%o", v);
	else
		snprintf(buf, sizeof(buf), "0x%x", v);
	cp = buf + strlen(buf);
	cplim = buf + sizeof(buf);
	if (bits != NULL) {
		bits++;
		*cp++ = ' ';
		*cp++ = '(';
		any = FALSE;
		while ((i = *bits++) != 0) {
			if (v & (1 << (i - 1))) {
				if (any) {
					*cp++ = ',';
					if (cp >= cplim)
						goto overrun;
				}
				any = TRUE;
				for (; (c = *bits) > 32; bits++) {
					*cp++ = c;
					if (cp >= cplim)
						goto overrun;
				}
			} else {
				for (; *bits > 32; bits++)
					continue;
			}
		}
		*cp++ = ')';
		if (cp >= cplim)
			goto overrun;
	}
	*cp = '\0';
	return buf;
    overrun:
	return "sprintb buffer too small";
}
const char * sprintb ( u_int v , const char * bits ) { char * cp ; char * cplim ; int i ; int any ; char c ; static char buf [ 132 ] ; if ( bits != NULL && * bits == 8 ) snprintf ( buf , sizeof ( buf ) , " " , v ) ; else snprintf ( buf , sizeof ( buf ) , " " , v ) ; cp = buf + strlen ( buf ) ; cplim = buf + sizeof ( buf ) ; if ( bits != NULL ) { bits ++ ; * cp ++ = ' ' ; * cp ++ = ' ' ; any = FALSE ; while ( ( i = * bits ++ ) != 0 ) { if ( v & ( 1 << ( i - 1 ) ) ) { if ( any ) { * cp ++ = ' ' ; if ( cp >= cplim ) goto overrun ; } any = TRUE ; for ( ; ( c = * bits ) > 32 ; bits ++ ) { * cp ++ = c ; if ( cp >= cplim ) goto overrun ; } } else { for ( ; * bits > 32 ; bits ++ ) continue ; } } * cp ++ = ' ' ; if ( cp >= cplim ) goto overrun ; } * cp = ' \0 ' ; return buf ; overrun : return " " ; }
Print a value a la the %b format of the kernel's printf
Print a value a la the %b format of the kernel ' s printf