summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2022-07-01 20:31:20 +0200
committerdzwdz2022-07-01 20:31:20 +0200
commit5b9f1ea3911a6e972263d00ea2ec04a56f56166d (patch)
tree8682f126dff11aed89a7a2c3f08ba1b8ae3c9202 /src
parenta66b497d688465685ed6269af622b5fb8cf017c9 (diff)
init/stdlib: implement snprintf
Diffstat (limited to 'src')
-rw-r--r--src/init/stdlib.c20
-rw-r--r--src/init/stdlib.h1
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;