vfprintf(3): Add support for kernel %b format

This is a direct port of the kernel %b format.

I'm unclear on if (more) non-portable printf extensions will be a
problem. I think it's desirable to have userspace formats include all
kernel formats, but there may be competing goals I'm not aware of.

Reviewed by:	no one, unfortunately
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8426
This commit is contained in:
cem 2016-12-16 01:44:50 +00:00
parent 6256ee171f
commit 682530b4a8

View File

@ -611,6 +611,37 @@ reswitch: switch (ch) {
case 'z':
flags |= SIZET;
goto rflag;
case 'b':
{
const char *q;
int anybitset, bit;
ulval = (u_int)GETARG(int);
cp = GETARG(char *);
q = __ultoa(ulval, buf + BUF, *cp++, 0, xdigs_lower);
PRINT(q, buf + BUF - q);
if (ulval == 0)
break;
for (anybitset = 0; *cp;) {
bit = *cp++;
if (ulval & (1 << (bit - 1))) {
PRINT(anybitset ? "," : "<", 1);
q = cp;
for (; (bit = *cp) > ' '; ++cp)
continue;
PRINT(q, cp - q);
anybitset = 1;
} else
for (; *cp > ' '; ++cp)
continue;
}
if (anybitset)
PRINT(">", 1);
}
continue;
case 'C':
flags |= LONGINT;
/*FALLTHROUGH*/