Skip to content

Commit

Permalink
Add kprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Mar 30, 2024
1 parent a5bc54a commit d82edab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions include/kprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef KPRINTF_H_
#define KPRINTF_H_

void kprintf(const char *fmt, ...);

#endif // KPRINTF_H_
6 changes: 2 additions & 4 deletions src/kmain.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <attribs.h>
#include <kprintf.h>
#include <types.h>
#include <uart.h>

noreturn void kmain(void) {
const char *s = "Hello, world!\n";
for (int i = 0; s[i] != '\0'; i++)
uart_putc(s[i]);
kprintf("%s, world!\n", "Hello");

while (true)
;
Expand Down
34 changes: 34 additions & 0 deletions src/kprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdarg.h>

#include <uart.h>

void kprintf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);

while (*fmt) {
if (*fmt != '%') {
uart_putc(*fmt);
fmt++;
continue;
}

fmt++;

switch (*fmt++) {
case 'c': {
char c = va_arg(args, int);
uart_putc(c);
break;
}
case 's': {
const char *s = va_arg(args, const char *);
for (; *s; s++)
uart_putc(*s);
break;
}
}
}

va_end(args);
}

0 comments on commit d82edab

Please sign in to comment.