Skip to content

Commit

Permalink
correctly implement printf
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Sep 7, 2023
1 parent b52cb02 commit 21e0121
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@ $<
Expand Down
2 changes: 1 addition & 1 deletion include/ckb-c-stdlib
Submodule ckb-c-stdlib updated 1 files
+5 −0 libc/src/impl.c
1 change: 1 addition & 0 deletions lua-loader/lua-loader.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define CKB_MALLOC_DECLARATION_ONLY 1
#define CKB_PRINTF_DECLARATION_ONLY 1

#define lua_c

Expand Down
29 changes: 28 additions & 1 deletion lualib/c-stdlib/src/printf_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, ...);

/**
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion tests/test_cases/dylibtest.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#define CKB_C_STDLIB_PRINTF
#define CKB_MALLOC_DECLARATION_ONLY 1
#define CKB_PRINTF_DECLARATION_ONLY 1

#include <stdio.h>

#include "blake2b.h"
Expand Down

0 comments on commit 21e0121

Please sign in to comment.