Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu/native: make use of stdio_read() / stdio_write() #16822

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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