Commit 8ef44442 authored by Radim Krčmář's avatar Radim Krčmář
Browse files

libcflat: fix padding in printf



A simple test:

  printf(".%8p.\n",  (void *)0);
  printf(".%-8p.\n", (void *)0);
  printf(".%8p.\n",  (void *)1);
  printf(".%-8p.\n", (void *)1);
  printf(".%8p.\n",  (void *)0x123456);
  printf(".%-8p.\n", (void *)0x123456);

  printf(".%2p.\n",  (void *)0);
  printf(".%-2p.\n", (void *)0);
  printf(".%2p.\n",  (void *)1);
  printf(".%-2p.\n", (void *)1);

glibc:

  .   (nil).
  .(nil)   .
  .     0x1.
  .0x1     .
  .0x123456.
  .0x123456.
  .(nil).
  .(nil).
  .0x1.
  .0x1.

before patch:

  .      0x       0.
  .0x      0       .
  .      0x       1.
  .0x      1       .
  .      0x  123456.
  .0x      123456  .
  .0x 0.
  .0x0 .
  .0x 1.
  .0x1 .

after patch:

  .       0.
  .0       .
  .     0x1.
  .0x1     .
  .0x123456.
  .0x123456.
  . 0.
  .0 .
  .0x1.
  .0x1.

(nil) would be possible with a small change, but the standard leaves it
to the implementation and 0 is acceptable, IMO.

Signed-off-by: default avatarRadim Krčmář <rkrcmar@redhat.com>
parent 4a3245a1
...@@ -18,6 +18,7 @@ typedef struct pstream { ...@@ -18,6 +18,7 @@ typedef struct pstream {
typedef struct strprops { typedef struct strprops {
char pad; char pad;
int npad; int npad;
bool alternate;
} strprops_t; } strprops_t;
static void addchar(pstream_t *p, char c) static void addchar(pstream_t *p, char c)
...@@ -94,7 +95,7 @@ void print_int(pstream_t *ps, long long n, int base, strprops_t props) ...@@ -94,7 +95,7 @@ void print_int(pstream_t *ps, long long n, int base, strprops_t props)
void print_unsigned(pstream_t *ps, unsigned long long n, int base, void print_unsigned(pstream_t *ps, unsigned long long n, int base,
strprops_t props) strprops_t props)
{ {
char buf[sizeof(long) * 3 + 1], *p = buf; char buf[sizeof(long) * 3 + 3], *p = buf;
int i; int i;
while (n) { while (n) {
...@@ -104,6 +105,10 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base, ...@@ -104,6 +105,10 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base,
if (p == buf) if (p == buf)
*p++ = '0'; *p++ = '0';
else if (props.alternate && base == 16) {
*p++ = 'x';
*p++ = '0';
}
for (i = 0; i < (p - buf) / 2; ++i) { for (i = 0; i < (p - buf) / 2; ++i) {
char tmp; char tmp;
...@@ -225,7 +230,7 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va) ...@@ -225,7 +230,7 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
} }
break; break;
case 'p': case 'p':
print_str(&s, "0x", props); props.alternate = true;
print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props); print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props);
break; break;
case 's': case 's':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment