Skip to content

Commit

Permalink
compel/log: Provide %u specifier parsing
Browse files Browse the repository at this point in the history
%u is quite common and I remember there were workarounds to print
(unsigned long) as long or whatever.
Just support it from now - it's not hard and not much code.

Signed-off-by: Dmitry Safonov <dima@arista.com>
  • Loading branch information
0x7f454c46 authored and avagin committed Sep 3, 2021
1 parent c39ed51 commit b28eb7b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions compel/plugins/std/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ static void print_num_l(long num, struct simple_buf *b)
print_string(s, b);
}

static void print_num_u(unsigned long num, struct simple_buf *b)
{
char buf[22], *s;

buf[21] = '\0';
s = &buf[21];

do {
s--;
*s = (num % 10) + '0';
num /= 10;
} while (num > 0);

print_string(s, b);
}

static void hexdigit(unsigned int v, char *to, char **z)
{
*to = "0123456789abcdef"[v & 0xf];
Expand Down Expand Up @@ -329,6 +345,12 @@ static void sbuf_printf(struct simple_buf *b, const char *format, va_list args)
case 'p':
print_hex_l((unsigned long)va_arg(args, void *), b);
break;
case 'u':
if (along)
print_num_u(va_arg(args, unsigned long), b);
else
print_num_u(va_arg(args, unsigned), b);
break;
default:
print_string("\nError: Unknown printf format %", b);
sbuf_putc(b, *s);
Expand Down

0 comments on commit b28eb7b

Please sign in to comment.