diff --git a/apps/app.c b/apps/app.c index 2e185b9b..f87ff701 100644 --- a/apps/app.c +++ b/apps/app.c @@ -4,24 +4,18 @@ void bye() { - console_print("byeee!\n"); + printf("byeee!\n"); } int main() { atexit(bye); - - char buffer[64]; - snprintf(buffer, sizeof(buffer), "Welcome to %s!\n", "Luna"); - - console_print(buffer); + printf("Welcome to %s!\n", "Luna"); time_t now = time(NULL); - snprintf(buffer, sizeof(buffer), "Realtime clock: %ld s\n", now); + printf("Realtime clock: %ld s\n", now); - console_print(buffer); + for (int i = 0; i < atoi("8"); i++) { console_write(".", 1); } - for (int i = 0; i < atoi("8"); i++) { console_print("."); } - - console_print("\n"); + console_write("\n", 1); } diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index edaa76f0..12840186 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -17,7 +17,7 @@ set(SOURCES src/thread/Scheduler.cpp src/sys/Syscall.cpp src/sys/exit.cpp - src/sys/console_print.cpp + src/sys/console_write.cpp src/sys/clock_gettime.cpp src/InitRD.cpp src/ELF.cpp diff --git a/kernel/src/sys/console_print.cpp b/kernel/src/sys/console_print.cpp deleted file mode 100644 index b112c5b2..00000000 --- a/kernel/src/sys/console_print.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "memory/MemoryManager.h" -#include "sys/Syscall.h" -#include "video/TextConsole.h" - -Result sys_console_print(Registers*, SyscallArgs args) -{ - if (!MemoryManager::validate_userspace_string(args[0])) return err(EFAULT); - - TextConsole::print((char*)args[0]); - - return { 0 }; -} diff --git a/kernel/src/sys/console_write.cpp b/kernel/src/sys/console_write.cpp new file mode 100644 index 00000000..4c00d560 --- /dev/null +++ b/kernel/src/sys/console_write.cpp @@ -0,0 +1,15 @@ +#include "memory/MemoryManager.h" +#include "sys/Syscall.h" +#include "video/TextConsole.h" + +Result sys_console_write(Registers*, SyscallArgs args) +{ + char* ptr = (char*)args[0]; + usize size = (usize)args[1]; + + if (!MemoryManager::validate_user_read(ptr, size)) return err(EFAULT); + + while (size--) { TextConsole::putchar(*ptr++); } + + return { 0 }; +} diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 06b6216c..25d99540 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -49,8 +49,14 @@ extern "C" /* Write up to max bytes of formatted output into a buffer. */ int vsnprintf(char*, size_t, const char*, va_list); + /* Write formatted output to standard output. */ + int printf(const char*, ...); + + /* Write a string followed by a newline to standard output. */ + int puts(const char* s); + /* Output a message to the console. */ - int console_print(const char* msg); + int console_write(const char* msg, size_t size); #ifdef __cplusplus } diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 44788b06..0d4102f0 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -9,9 +10,9 @@ FILE* stderr = nullptr; extern "C" { - int console_print(const char* str) + int console_write(const char* str, size_t size) { - long rc = syscall(SYS_console_print, str); + long rc = syscall(SYS_console_write, str, size); __errno_return(rc, int); } @@ -48,4 +49,25 @@ extern "C" return rc; } + + int printf(const char* format, ...) + { + va_list ap; + va_start(ap, format); + + int rc = (int)pure_cstyle_format( + format, [](char c, void*) { console_write(&c, 1); }, nullptr, ap); + + va_end(ap); + + return rc; + } + + int puts(const char* s) + { + if (console_write(s, strlen(s)) < 0) return -1; + if (console_write("\n", 1) < 0) return -1; + + return 0; + } } diff --git a/luna/include/luna/Syscall.h b/luna/include/luna/Syscall.h index aeb98f95..2b36b2e9 100644 --- a/luna/include/luna/Syscall.h +++ b/luna/include/luna/Syscall.h @@ -1,6 +1,6 @@ #pragma once -#define enumerate_syscalls(_e) _e(exit) _e(console_print) _e(clock_gettime) +#define enumerate_syscalls(_e) _e(exit) _e(console_write) _e(clock_gettime) enum Syscalls {