diff options
-rw-r--r-- | src/init/stdlib.c | 20 | ||||
-rw-r--r-- | src/init/stdlib.h | 1 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 9b7a329..cbaed61 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -18,6 +18,26 @@ int printf(const char *fmt, ...) { return ret; } +static void backend_buf(void *arg, const char *buf, size_t len) { + char **ptrs = arg; + size_t space = ptrs[1] - ptrs[0]; + if (len > space) len = space; + memcpy(ptrs[0], buf, len); + ptrs[0] += len; +} + +int snprintf(char *str, size_t len, const char *fmt, ...) { + int ret = 0; + char *ptrs[2] = {str, str + len}; + va_list argp; + va_start(argp, fmt); + ret = __printf_internal(fmt, argp, backend_buf, &ptrs); + va_end(argp); + if (ptrs[0] < ptrs[1]) *ptrs[0] = '\0'; + return ret; +} + + int file_open(libc_file *f, const char *path, int flags) { f->pos = 0; f->eof = false; diff --git a/src/init/stdlib.h b/src/init/stdlib.h index 3dc1c81..03e1717 100644 --- a/src/init/stdlib.h +++ b/src/init/stdlib.h @@ -5,6 +5,7 @@ #include <stddef.h> int printf(const char *fmt, ...); +int snprintf(char *str, size_t len, const char *fmt, ...); typedef struct { int fd; |