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 7, 2021
1 parent f79207e commit 1f50d08
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
16 changes: 14 additions & 2 deletions cpu/native/stdio_native/stdio_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ void stdio_init(void)

ssize_t stdio_read(void* buffer, size_t max_len)
{
return real_read(STDIN_FILENO, buffer, max_len);
ssize_t res;

_native_syscall_enter();
res = real_read(STDIN_FILENO, buffer, max_len);
_native_syscall_leave();

return res;
}

ssize_t stdio_write(const void* buffer, size_t len)
{
return real_write(STDOUT_FILENO, buffer, len);
ssize_t res;

_native_syscall_enter();
res = real_write(STDOUT_FILENO, buffer, len);
_native_syscall_leave();

return res;
}

/** @} */
44 changes: 43 additions & 1 deletion 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) {
return stdio_write(buf, count);
}

_native_syscall_enter();
r = real_write(fd, buf, count);
_native_syscall_leave();
Expand All @@ -237,7 +246,18 @@ 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) {
for (int i = 0; i < iovcnt; ++i) {
ssize_t res = stdio_write(iov->iov_base, iov->iov_len);
iov++;
if (res < 0) {
return res;
}
r += res;
}
}

_native_syscall_enter();
r = real_writev(fd, iov, iovcnt);
Expand All @@ -255,6 +275,12 @@ int putchar(int c)
return 0;
}

int putc(int c, FILE *fp)
{
_native_write(fileno(fp), &c, 1);
return 0;
}

int puts(const char *s)
{
int r;
Expand All @@ -263,6 +289,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 1f50d08

Please sign in to comment.