ソースを参照

fix(stdio): add vsnprintf for va_list

greatbridf 2 年 前
コミット
bd41632d2d
3 ファイル変更17 行追加7 行削除
  1. 2 0
      gblibc/include/stdio.h
  2. 14 6
      gblibc/src/stdio.c
  3. 1 1
      user-space-program/sh.c

+ 2 - 0
gblibc/include/stdio.h

@@ -1,6 +1,7 @@
 #ifndef __GBLIBC_STDIO_H_
 #define __GBLIBC_STDIO_H_
 
+#include <stdarg.h>
 #include <stdint.h>
 
 #undef EOF
@@ -10,6 +11,7 @@
 extern "C" {
 #endif
 
+int vsnprintf(char* buf, size_t bufsize, const char* fmt, va_list args);
 int snprintf(char* buf, size_t bufsize, const char* fmt, ...);
 
 #ifdef __cplusplus

+ 14 - 6
gblibc/src/stdio.c

@@ -1,5 +1,6 @@
 #include <devutil.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdarg.h>
 
 // where n is in the range of [0, 9]
@@ -236,12 +237,21 @@ snprint_char(
     return sizeof(c);
 }
 
-int snprintf(char* buf, size_t buf_size, const char* fmt, ...)
+int snprintf(char* buf, size_t bufsize, const char* fmt, ...)
 {
-    ssize_t n_write = 0;
+    va_list lst;
+    va_start(lst, fmt);
+
+    int ret = vsnprintf(buf, bufsize, fmt, lst);
+
+    va_end(lst);
+
+    return ret;
+}
 
-    va_list arg;
-    va_start(arg, fmt);
+int vsnprintf(char* buf, size_t buf_size, const char* fmt, va_list arg)
+{
+    ssize_t n_write = 0;
 
     for (char c; (c = *fmt) != 0x00; ++fmt) {
         if (c == '%') {
@@ -339,7 +349,5 @@ int snprintf(char* buf, size_t buf_size, const char* fmt, ...)
     if (buf_size > 0)
         *buf = 0x00;
 
-    va_end(arg);
-
     return n_write;
 }

+ 1 - 1
user-space-program/sh.c

@@ -12,7 +12,7 @@ int printf(const char* fmt, ...)
     va_start(args, fmt);
 
     char buf[256] = {};
-    int len = snprintf(buf, sizeof(buf), fmt, args);
+    int len = vsnprintf(buf, sizeof(buf), fmt, args);
 
     len = write(STDOUT_FILENO, buf, len);