From 13e16fa34f04f4a87115eb5eabdcb2acf2bccc6b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 7 Sep 2021 16:29:12 +0200 Subject: [PATCH] cpu/native: make use of stdio_read() / stdio_write() On `native` the functions stdio_read() / stdio_write() were not used. Those functions are intended for alternative stdio implementations. As a result, no alternative stdio could be used on `native`. To fix this, call the functions in `_native_read()` / `_native_write()` when dealing with stdio fds. --- cpu/native/syscalls.c | 57 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index e0b5e4ec8967..620392bb8cc7 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -40,6 +40,7 @@ #include "cpu.h" #include "irq.h" #include "xtimer.h" +#include "stdio_base.h" #include "native_internal.h" @@ -217,6 +218,10 @@ ssize_t _native_read(int fd, void *buf, size_t count) { ssize_t r; + if (fd == STDIN_FILENO) { + return stdio_read(buf, count); + } + _native_syscall_enter(); r = real_read(fd, buf, count); _native_syscall_leave(); @@ -228,6 +233,10 @@ ssize_t _native_write(int fd, const void *buf, size_t count) { ssize_t r; + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { + return stdio_write(buf, count); + } + _native_syscall_enter(); r = real_write(fd, buf, count); _native_syscall_leave(); @@ -237,7 +246,27 @@ ssize_t _native_write(int fd, const void *buf, size_t count) ssize_t _native_writev(int fd, const struct iovec *iov, int iovcnt) { - ssize_t r; + ssize_t r = 0; + + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { + while (iovcnt--) { + ssize_t res = stdio_write(iov->iov_base, iov->iov_len); + + if (res >= 0) { + r += res; + } else { + return res; + } + + if (res < (int)iov->iov_len) { + break; + } + + iov++; + } + + return r; + } _native_syscall_enter(); r = real_writev(fd, iov, iovcnt); @@ -251,8 +280,14 @@ ssize_t _native_writev(int fd, const struct iovec *iov, int iovcnt) #endif int putchar(int c) { - _native_write(STDOUT_FILENO, &c, 1); - return 0; + char tmp = c; + return _native_write(STDOUT_FILENO, &tmp, sizeof(tmp)); +} + +int putc(int c, FILE *fp) +{ + char tmp = c; + return _native_write(fileno(fp), &tmp, sizeof(tmp)); } int puts(const char *s) @@ -263,6 +298,22 @@ int puts(const char *s) return r; } +int fgetc(FILE *fp) +{ + return getc(fp); +} + +int getc(FILE *fp) +{ + char c; + + if (_native_read(fileno(fp), &c, sizeof(c)) <= 0) { + return EOF; + } + + return c; +} + /* Solve 'format string is not a string literal' as it is validly used in this * function */ __attribute__((__format__ (__printf__, 1, 0)))