Implement string formatting into libc :)
All checks were successful
continuous-integration/drone/push Build is passing

Of course, using <luna/Format.h> makes it so simple. No need for duplicate code!
This commit is contained in:
apio 2023-01-06 20:15:43 +01:00
parent 367a2ce521
commit 80f5c790f8
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 63 additions and 3 deletions

View File

@ -10,6 +10,11 @@ int main()
{ {
atexit(bye); 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("."); } for (int i = 0; i < atoi("8"); i++) { console_print("."); }
console_print("\n"); console_print("\n");

View File

@ -36,7 +36,18 @@ extern "C"
int fprintf(FILE*, const char*, ...); int fprintf(FILE*, const char*, ...);
int vfprintf(FILE*, const char*, va_list); 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. */ /* Output a message to the console. */
int console_print(const char* msg); int console_print(const char* msg);

View File

@ -1,4 +1,6 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <bits/errno-return.h> #include <bits/errno-return.h>
#include <luna/Format.h>
#include <stdio.h> #include <stdio.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
@ -12,4 +14,38 @@ extern "C"
long rc = syscall(SYS_console_print, str); long rc = syscall(SYS_console_print, str);
__errno_return(rc, int); __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;
}
} }

View File

@ -10,7 +10,9 @@
char* const rc = new (std::nothrow) char[size]; char* const rc = new (std::nothrow) char[size];
return (void*)rc; return (void*)rc;
#else #else
return malloc(size); // return malloc(size);
(void)size;
return NULL;
#endif #endif
} }
@ -20,11 +22,17 @@ void raw_free(void* ptr)
char* const arr = (char*)ptr; char* const arr = (char*)ptr;
delete[] arr; delete[] arr;
#else #else
return free(ptr); // return free(ptr);
(void)ptr;
#endif #endif
} }
void operator delete(void* ptr, usize size, std::align_val_t) noexcept void operator delete(void* ptr, usize size, std::align_val_t) noexcept
{ {
#ifdef USE_FREESTANDING
operator delete(ptr, size); operator delete(ptr, size);
#else
(void)ptr;
(void)size;
#endif
} }