Skip to content

Commit

Permalink
add hackish _snprintf() and _vsnprintf()
Browse files Browse the repository at this point in the history
Clean-ups are welcome. :)
  • Loading branch information
stsp committed Jul 22, 2018
1 parent e6e04d0 commit 5b1a628
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion kernel/dyninit.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ far_t DynAlloc(const char *what, unsigned num, unsigned size)
if ((ULONG) total + Dynp->Allocated > 0xffff)
{
char buf[256];
snprintf(buf, sizeof(buf), "Dyn %u\n", (ULONG) total + Dynp->Allocated);
_snprintf(buf, sizeof(buf), "Dyn %u\n", (ULONG) total + Dynp->Allocated);
panic(buf);
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ STATIC void InitIO(void)
VOID init_fatal(const BYTE * err_msg)
{
char buf[256];
snprintf(buf, sizeof(buf), "\nInternal kernel error - %s\nSystem halted\n", err_msg);
_snprintf(buf, sizeof(buf), "\nInternal kernel error - %s\nSystem halted\n", err_msg);
panic(buf);
}

Expand Down
74 changes: 62 additions & 12 deletions kernel/prf.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ static BYTE SSFAR *charp = 0;

STATIC VOID handle_char(COUNT);
STATIC void ltob(LONG, BYTE SSFAR *, COUNT);
STATIC void do_printf(const char *, REG va_list);
STATIC void do_printf(CONST BYTE *, va_list);
STATIC void do_printf_n(size_t size, CONST BYTE * fmt, va_list arg);

/* special handler to switch between sprintf and printf */
STATIC VOID handle_char(COUNT c)
Expand Down Expand Up @@ -251,19 +252,40 @@ int _vsprintf(char * buff, CONST char * fmt, va_list arg)
return 0;
}

STATIC void do_printf(CONST BYTE * fmt, va_list arg)
PRINTF(3)
int VA_CDECL _snprintf(char * buff, size_t size, CONST char * fmt, ...)
{
int base;
BYTE s[11], * p;
int size;
unsigned char flags;
va_list arg;

va_start(arg, fmt);
charp = buff;
do_printf_n(size, fmt, arg);
va_end(arg);
handle_char('\0');
return 0;
}

int _vsnprintf(char * buff, size_t size, CONST char * fmt, va_list arg)
{
charp = buff;
do_printf_n(size, fmt, arg);
handle_char('\0');
return 0;
}

enum { RET_CONT, RET_RET };
STATIC int printf_handle_char(CONST BYTE ** _fmt, va_list arg)
{
#define fmt (*_fmt)
int base;
BYTE s[11], * p;
int size;
unsigned char flags;

for (;*fmt != '\0'; fmt++)
{
if (*fmt != '%')
{
handle_char(*fmt);
continue;
return RET_CONT;
}

fmt++;
Expand Down Expand Up @@ -300,11 +322,11 @@ STATIC void do_printf(CONST BYTE * fmt, va_list arg)
switch (*fmt)
{
case '\0':
return;
return RET_RET;

case 'c':
handle_char(va_arg(arg, int));
continue;
return RET_CONT;

case 'P':
{
Expand Down Expand Up @@ -369,7 +391,7 @@ STATIC void do_printf(CONST BYTE * fmt, va_list arg)
case '%':

handle_char(*fmt);
continue;
return RET_CONT;

}
{
Expand All @@ -390,6 +412,34 @@ STATIC void do_printf(CONST BYTE * fmt, va_list arg)

for (; size > 0; size--)
handle_char(' ');

return RET_CONT;
#undef fmt
}

STATIC void do_printf(CONST BYTE * fmt, va_list arg)
{
for (;*fmt != '\0'; fmt++)
{
switch (printf_handle_char(&fmt, arg)) {
case RET_CONT:
continue;
case RET_RET:
return;
}
}
}

STATIC void do_printf_n(size_t size, CONST BYTE * fmt, va_list arg)
{
for (;*fmt != '\0' && size > 0; fmt++, size--)
{
switch (printf_handle_char(&fmt, arg)) {
case RET_CONT:
continue;
case RET_RET:
return;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions kernel/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@ UWORD ASMCFUNC SEGM(HMA_TEXT) syscall_MUX14(__FAR(iregs) regs);
#ifdef DEBUG
int VA_CDECL _printf(CONST char * fmt, ...) PRINTF(1);
int VA_CDECL _sprintf(char * buff, CONST char * fmt, ...) PRINTF(2);
int VA_CDECL _snprintf(char * buff, size_t size, CONST char * fmt, ...) PRINTF(3);
int _vprintf(CONST char *fmt, va_list arg);
int _vsprintf(char * buff, CONST char * fmt, va_list arg);
int _vsnprintf(char * buff, size_t size, CONST char * fmt, va_list arg);
#endif
VOID hexd(const char *title,__FAR(VOID) p, COUNT numBytes);
void put_unsigned(unsigned n, int base, int width);
Expand Down

0 comments on commit 5b1a628

Please sign in to comment.