diff --git a/apps/app.c b/apps/app.c index 30759530..9caf3a4d 100644 --- a/apps/app.c +++ b/apps/app.c @@ -10,6 +10,11 @@ int main() { atexit(bye); + char buffer[64]; + snprintf(buffer, sizeof(buffer), "Welcome to %s!\n", "Luna"); + + console_print(buffer); + for (int i = 0; i < atoi("8"); i++) { console_print("."); } console_print("\n"); diff --git a/libc/include/stdio.h b/libc/include/stdio.h index f99fc754..06b6216c 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -36,7 +36,18 @@ extern "C" int fprintf(FILE*, const char*, ...); int vfprintf(FILE*, const char*, va_list); - int sprintf(char*, const char*, ...); + + /* Write formatted output into a buffer. */ + int sprintf(char* buf, const char* format, ...); + + /* Write up to max bytes of formatted output into a buffer. */ + int snprintf(char* buf, size_t max, const char* format, ...); + + /* Write formatted output into a buffer. */ + int vsprintf(char*, const char*, va_list); + + /* Write up to max bytes of formatted output into a buffer. */ + int vsnprintf(char*, size_t, const char*, va_list); /* Output a message to the console. */ int console_print(const char* msg); diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index cecba48f..44788b06 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -1,4 +1,6 @@ +#define _LUNA_SYSTEM_ERROR_EXTENSIONS #include +#include #include #include #include @@ -12,4 +14,38 @@ extern "C" long rc = syscall(SYS_console_print, str); __errno_return(rc, int); } + + int vsnprintf(char* buf, size_t max, const char* format, va_list ap) + { + return (int)vstring_format(buf, max, format, ap); + } + + int snprintf(char* buf, size_t max, const char* format, ...) + { + va_list ap; + va_start(ap, format); + + int rc = vsnprintf(buf, max, format, ap); + + va_end(ap); + + return rc; + } + + int vsprintf(char* buf, const char* format, va_list ap) + { + return vsnprintf(buf, (size_t)-1, format, ap); + } + + int sprintf(char* buf, const char* format, ...) + { + va_list ap; + va_start(ap, format); + + int rc = vsnprintf(buf, (size_t)-1, format, ap); + + va_end(ap); + + return rc; + } } diff --git a/luna/src/Alloc.cpp b/luna/src/Alloc.cpp index 23f80e6c..65d8c609 100644 --- a/luna/src/Alloc.cpp +++ b/luna/src/Alloc.cpp @@ -10,7 +10,9 @@ char* const rc = new (std::nothrow) char[size]; return (void*)rc; #else - return malloc(size); + // return malloc(size); + (void)size; + return NULL; #endif } @@ -20,11 +22,17 @@ void raw_free(void* ptr) char* const arr = (char*)ptr; delete[] arr; #else - return free(ptr); + // return free(ptr); + (void)ptr; #endif } void operator delete(void* ptr, usize size, std::align_val_t) noexcept { +#ifdef USE_FREESTANDING operator delete(ptr, size); +#else + (void)ptr; + (void)size; +#endif }