Add function "kernaux_console_printf"

This commit is contained in:
Alex Kotov 2020-12-06 07:42:56 +05:00
parent 8197388340
commit fac70421d7
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 61 additions and 1 deletions

View File

@ -23,7 +23,7 @@ API
* [Simple command line parser](/include/kernaux/cmdline.h) *(work in progress)*
* [Multiboot 2 (GRUB 2) information parser](/include/kernaux/multiboot2.h)
* [Serial console](/include/kernaux/console.h)
* [Serial console](/include/kernaux/console.h) *(work in progress)*
* [Page Frame Allocator](/include/kernaux/pfa.h) *(work in progress)*
* ELF utils *(planned)*
* [Architecture-specific helpers](/include/kernaux/arch/)

View File

@ -6,6 +6,7 @@ extern "C" {
#endif
void kernaux_console_print(const char *s);
void kernaux_console_printf(const char *format, ...);
void kernaux_console_putc(char c);
void kernaux_console_puts(const char *s);
void kernaux_console_write(const char *data, unsigned int size);

View File

@ -31,3 +31,62 @@ void kernaux_console_write(const char *const data, const unsigned int size)
kernaux_console_putc(data[i]);
}
}
void kernaux_console_printf(const char *format, ...)
{
char **arg = (char **) &format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0)
{
if (c == '\n') {
if (*format != 0) {
kernaux_console_putc('\n');
}
}
else if (c != '%') {
kernaux_console_putc(c);
}
else {
char *p, *p2;
int pad0 = 0, pad = 0;
c = *format++;
if (c == '0')
{
pad0 = 1;
c = *format++;
}
if (c >= '0' && c <= '9')
{
pad = c - '0';
c = *format++;
}
switch (c)
{
case 'd':
case 'u':
case 'x':
kernaux_itoa(*((int*)arg++), buf, c);
p = buf;
goto string;
break;
case 's':
p = *arg++;
if (! p)
p = "(null)";
string:
for (p2 = p; *p2; p2++);
for (; p2 < p + pad; p2++)
kernaux_console_putc(pad0 ? '0' : ' ');
while (*p)
kernaux_console_putc(*p++);
break;
default:
kernaux_console_putc(*((int *) arg++));
break;
}
}
}
}