1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-30 22:43:12 +03:00

Add "%d" support to _dl_debug_vdprintf

"%d" will be used to print out signed value.
This commit is contained in:
H.J. Lu
2020-06-09 12:15:01 -07:00
parent a365ac45b7
commit 533dd2acf7

View File

@ -167,6 +167,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
switch (*fmt) switch (*fmt)
{ {
/* Integer formatting. */ /* Integer formatting. */
case 'd':
case 'u': case 'u':
case 'x': case 'x':
{ {
@ -179,11 +180,34 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
#else #else
unsigned long int num = va_arg (arg, unsigned int); unsigned long int num = va_arg (arg, unsigned int);
#endif #endif
bool negative = false;
if (*fmt == 'd')
{
#if LONG_MAX != INT_MAX
if (long_mod)
{
if ((long int) num < 0)
negative = true;
}
else
{
if ((int) num < 0)
{
num = (unsigned int) num;
negative = true;
}
}
#else
if ((int) num < 0)
negative = true;
#endif
}
/* We use alloca() to allocate the buffer with the most /* We use alloca() to allocate the buffer with the most
pessimistic guess for the size. Using alloca() allows pessimistic guess for the size. Using alloca() allows
having more than one integer formatting in a call. */ having more than one integer formatting in a call. */
char *buf = (char *) alloca (3 * sizeof (unsigned long int)); char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int));
char *endp = &buf[3 * sizeof (unsigned long int)]; char *endp = &buf[1 + 3 * sizeof (unsigned long int)];
char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0); char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
/* Pad to the width the user specified. */ /* Pad to the width the user specified. */
@ -191,6 +215,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
while (endp - cp < width) while (endp - cp < width)
*--cp = fill; *--cp = fill;
if (negative)
*--cp = '-';
iov[niov].iov_base = cp; iov[niov].iov_base = cp;
iov[niov].iov_len = endp - cp; iov[niov].iov_len = endp - cp;
++niov; ++niov;