Skip to content

Commit

Permalink
Add long conversion to __dprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 19, 2024
1 parent 86dc6f8 commit 71bafc5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 22 additions & 7 deletions libc/malloc/dprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
#include <paths.h>
#include <fcntl.h>

static char *uitostr(unsigned int val, int radix, int width)
static char *fmtultostr(unsigned long val, int radix, int width)
{
static char buf[6];
static char buf[34];
char *p = buf + sizeof(buf) - 1;
unsigned int c;

*p = '\0';
do {
#ifdef _M_I86
c = radix;
val = __divmod(val, &c);
#else
c = val % radix;
val = val / radix;
#endif
if (c > 9)
*--p = 'a' - 10 + c;
else
Expand All @@ -28,19 +33,22 @@ static char *uitostr(unsigned int val, int radix, int width)
/*
* Very tiny printf to console.
* Supports:
* %{0-9} width
* %{0-9} field width
* %l{u,d,x,p}
* %u unsigned decimal
* %d decimal (positive numbers only)
* %p zero-fill hexadecimal width 4
* %p pointer (zero-filled hexadecimal)
* %x hexadecimal
* %s string
*/
int __dprintf(const char *fmt, ...)
{
unsigned int n;
unsigned long val;
int radix, width;
char *p;
va_list va;
int lval;
char b[80];
static int fd = -1;

Expand All @@ -55,27 +63,34 @@ int __dprintf(const char *fmt, ...)
if (*fmt == '%') {
++fmt;
width = 0;
lval = 0;
while (*fmt >= '0' && *fmt <= '9') {
width = width * 10 + *fmt - '0';
fmt++;
}
again:
switch (*fmt) {
case 'l':
fmt++; goto again;
lval = 1;
fmt++;
goto again;
case 's':
p = va_arg(va, char *);
goto outstr;
case 'p':
width = 4;
if (sizeof(char *) == sizeof(unsigned long))
lval = 1;
width = lval? 8: 4;
/* fall through */
case 'x':
radix = 16;
goto convert;
case 'd':
case 'u':
radix = 10;
convert:
p = uitostr(va_arg(va, unsigned int), radix, width);
val = lval? va_arg(va, unsigned long): va_arg(va, unsigned int);
p = fmtultostr(val, radix, width);
outstr:
while (*p && n < sizeof(b))
b[n++] = *p++;
Expand Down
2 changes: 1 addition & 1 deletion libc/malloc/fmemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void __far *fmemalloc(unsigned long size)
#if DEBUG
total += size;
if (size > maxsize) maxsize = size;
debug("total %lu, maxsize %lu\n", (unsigned)total, (unsigned)maxsize);
debug("total %lu, maxsize %lu\n", total, maxsize);
#endif
return MK_FP(seg, 0);
}
Expand Down

0 comments on commit 71bafc5

Please sign in to comment.