From 21e0121d358360386c6edeb5f32b015ba3facb39 Mon Sep 17 00:00:00 2001 From: YI Date: Thu, 7 Sep 2023 13:23:17 +0800 Subject: [PATCH] correctly implement printf --- Makefile | 6 +++--- include/ckb-c-stdlib | 2 +- lua-loader/lua-loader.c | 1 + lualib/c-stdlib/src/printf_impl.h | 29 ++++++++++++++++++++++++++++- tests/test_cases/dylibtest.c | 4 +++- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index b6ce695..57866ab 100644 --- a/Makefile +++ b/Makefile @@ -27,13 +27,13 @@ lualib/liblua.a: make -C lualib liblua.a build/dylibtest: tests/test_cases/dylibtest.c - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a build/dylibexample: examples/dylib.c - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a build/spawnexample: examples/spawn.c - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a build/lua-loader.o: lua-loader/lua-loader.c $(CC) -c $(CFLAGS) -o $@ $< diff --git a/include/ckb-c-stdlib b/include/ckb-c-stdlib index aba7f2b..dffd9e6 160000 --- a/include/ckb-c-stdlib +++ b/include/ckb-c-stdlib @@ -1 +1 @@ -Subproject commit aba7f2bf1b2ff1b4a60a5cd0988215bd4eca3431 +Subproject commit dffd9e66dbba4de8213e8b8f83112cd62469269b diff --git a/lua-loader/lua-loader.c b/lua-loader/lua-loader.c index 84b2232..a8805ce 100644 --- a/lua-loader/lua-loader.c +++ b/lua-loader/lua-loader.c @@ -1,4 +1,5 @@ #define CKB_MALLOC_DECLARATION_ONLY 1 +#define CKB_PRINTF_DECLARATION_ONLY 1 #define lua_c diff --git a/lualib/c-stdlib/src/printf_impl.h b/lualib/c-stdlib/src/printf_impl.h index 382e94f..9843313 100644 --- a/lualib/c-stdlib/src/printf_impl.h +++ b/lualib/c-stdlib/src/printf_impl.h @@ -2,6 +2,7 @@ #define LUA_C_STDLIB_PRINTF_H_ #undef CKB_C_STDLIB_PRINTF +#define CKB_MALLOC_DECLARATION_ONLY 1 // Code copied from // https://github.com/mpaland/printf/tree/d3b984684bb8a8bdc48cc7a1abecb93ce59bbe3e @@ -27,7 +28,6 @@ void _putchar(char character); * \return The number of characters that are written into the array, not * counting the terminating null character */ -#define printf printf_ int printf_(const char *format, ...); /** @@ -1031,4 +1031,31 @@ int fctprintf(void (*out)(char character, void *arg), void *arg, return ret; } +// Default PRINTF_BUFFER_SIZE +#ifndef CKB_C_STDLIB_PRINTF_BUFFER_SIZE +#define CKB_C_STDLIB_PRINTF_BUFFER_SIZE 256 +#endif +// syscall +int ckb_debug(const char *s); + +int printf(const char *format, ...) { + static char buf[CKB_C_STDLIB_PRINTF_BUFFER_SIZE]; + va_list va; + va_start(va, format); + int ret = vsnprintf_(buf, CKB_C_STDLIB_PRINTF_BUFFER_SIZE, format, va); + va_end(va); + ckb_debug(buf); + return ret; +} + +int ckb_printf(const char *format, ...) { + static char buf[CKB_C_STDLIB_PRINTF_BUFFER_SIZE]; + va_list va; + va_start(va, format); + int ret = vsnprintf_(buf, CKB_C_STDLIB_PRINTF_BUFFER_SIZE, format, va); + va_end(va); + ckb_debug(buf); + return ret; +} + #endif diff --git a/tests/test_cases/dylibtest.c b/tests/test_cases/dylibtest.c index a9e9afa..aba2f5d 100644 --- a/tests/test_cases/dylibtest.c +++ b/tests/test_cases/dylibtest.c @@ -1,4 +1,6 @@ -#define CKB_C_STDLIB_PRINTF +#define CKB_MALLOC_DECLARATION_ONLY 1 +#define CKB_PRINTF_DECLARATION_ONLY 1 + #include #include "blake2b.h"