Skip to content

Commit e01fc90

Browse files
committed
workaround for zephyr stdio with picolibc
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 70b45b2 commit e01fc90

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

core/shared/platform/zephyr/zephyr_file.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "platform_api_extension.h"
88
#include "libc_errno.h"
99

10+
#include <stdio.h>
1011
#include <string.h>
1112
#include <stdlib.h>
1213
#include <unistd.h>
@@ -720,17 +721,33 @@ os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt,
720721
if (handle->raw_fd >= 0) {
721722
/* Use the real OS file descriptor */
722723
for (int i = 0; i < iovcnt; i++) {
723-
ssize_t bytes_written =
724-
write(handle->raw_fd, iov[i].buf, iov[i].buf_len);
724+
ssize_t bytes_written = 0;
725+
if (handle->raw_fd == STDOUT_FILENO
726+
|| handle->raw_fd == STDERR_FILENO) {
727+
/* Zephyr still does not support write to stdout with
728+
* picolibc. fprintf works. so we use it here. */
729+
fprintf(stdout, "%*s", iov[i].buf_len, (char *)iov[i].buf);
730+
731+
/* fprintf will also sometimes return more bytes than we
732+
* request, so we just bypass here */
733+
bytes_written = iov[i].buf_len;
734+
}
735+
else {
736+
bytes_written =
737+
write(handle->raw_fd, iov[i].buf, iov[i].buf_len);
738+
}
739+
725740
if (bytes_written < 0)
726741
return convert_errno(errno);
742+
727743
total_written += bytes_written;
728744
}
729745
}
730746
else {
731747
/* No real fd: fall back to fwrite on stdout/stderr */
732748
FILE *f = (handle->fd == STDERR_FILENO) ? stderr : stdout;
733749
for (int i = 0; i < iovcnt; i++) {
750+
printf("2222\n");
734751
ssize_t bytes_written =
735752
fwrite(iov[i].buf, 1, iov[i].buf_len, f);
736753
if (bytes_written < 0)

0 commit comments

Comments
 (0)