Skip to content

Commit

Permalink
cpu/native: make use of stdio_read() / stdio_write()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benpicco committed Sep 22, 2021
1 parent f79207e commit 13e16fa
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "cpu.h"
#include "irq.h"
#include "xtimer.h"
#include "stdio_base.h"

#include "native_internal.h"

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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)))
Expand Down

0 comments on commit 13e16fa

Please sign in to comment.