Skip to content

Commit 369fe49

Browse files
committed
Updated os_writev to use fwrite for STDOUT/STDERR
Signed-off-by: Stephen Berard <stephen.berard@outlook.com>
1 parent 96a2cfb commit 369fe49

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

core/shared/platform/zephyr/platform_internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
#define PATH_MAX 256
8787
#endif
8888

89+
#ifndef STDIN_FILENO
90+
#define STDIN_FILENO 0
91+
#endif
92+
93+
#ifndef STDOUT_FILENO
94+
#define STDOUT_FILENO 1
95+
#endif
96+
97+
#ifndef STDERR_FILENO
98+
#define STDERR_FILENO 2
99+
#endif
100+
101+
89102
/* Synchronization primitives for usermode.
90103
* The macros are prefixed with 'z' because when building
91104
* with WAMR_BUILD_LIBC_WASI the same functions are defined,

core/shared/platform/zephyr/zephyr_file.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
// We will take the maximum number of open files
4848
// from the Zephyr POSIX configuration
49-
#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_POSIX_MAX_FDS
49+
#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_ZVFS_OPEN_MAX
5050

5151
// Macro to retrieve a file system descriptor and check it's validity.
5252
#define GET_FILE_SYSTEM_DESCRIPTOR(fd, ptr) \
@@ -598,43 +598,49 @@ __wasi_errno_t
598598
os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt,
599599
size_t *nwritten)
600600
{
601-
struct zephyr_fs_desc *ptr = NULL;
602601
ssize_t total_written = 0;
603-
char buffer[256];
604602

605-
GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr);
603+
// If the fd is stdout or stderr, we call fwrite
604+
if ((handle->fd == STDOUT_FILENO) || (handle->fd == STDERR_FILENO)) {
605+
FILE *fd = stdout;
606+
if (handle->fd == STDERR_FILENO) {
607+
fd = stderr;
608+
}
606609

607-
if (strncmp(ptr->path, "std", 3) == 0) {
608-
// for std[in/out/err] we don't write because they are not real opened
609-
// files. Instead we emulate a write operation to make it work with
610-
// printf.
611610
for (int i = 0; i < iovcnt; i++) {
612-
if (iov[i].buf_len == 0)
613-
continue;
614-
memset(buffer, 0, sizeof(buffer));
615-
memcpy(buffer, iov[i].buf, iov[i].buf_len);
616-
os_printf("%s", buffer);
617-
total_written += iov[i].buf_len;
611+
ssize_t bytes_written =
612+
fwrite(iov[i].buf, 1, iov[i].buf_len, fd);
618613

619-
}
620-
*nwritten = total_written;
614+
if (bytes_written < 0) {
615+
return convert_errno(-bytes_written);
616+
}
621617

622-
return __WASI_ESUCCESS;
623-
}
618+
total_written += bytes_written;
624619

625-
// Write data from each buffer
626-
for (int i = 0; i < iovcnt; i++) {
627-
ssize_t bytes_written =
628-
fs_write(&ptr->file, iov[i].buf, iov[i].buf_len);
629-
if (bytes_written < 0) {
630-
return convert_errno(-bytes_written);
620+
// If we wrote less than we asked for, stop writing
621+
if (bytes_written < iov[i].buf_len) {
622+
break;
623+
}
631624
}
625+
}
626+
else {
627+
struct zephyr_fs_desc *ptr = NULL;
628+
GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr);
632629

633-
total_written += bytes_written;
630+
// Write data from each buffer
631+
for (int i = 0; i < iovcnt; i++) {
632+
ssize_t bytes_written =
633+
fs_write(&ptr->file, iov[i].buf, iov[i].buf_len);
634+
if (bytes_written < 0) {
635+
return convert_errno(-bytes_written);
636+
}
634637

635-
// If we wrote less than we asked for, stop writing
636-
if (bytes_written < iov[i].buf_len) {
637-
break;
638+
total_written += bytes_written;
639+
640+
// If we wrote less than we asked for, stop writing
641+
if (bytes_written < iov[i].buf_len) {
642+
break;
643+
}
638644
}
639645
}
640646

@@ -787,7 +793,7 @@ os_unlinkat(os_file_handle handle, const char *path, bool is_dir)
787793
if (!build_absolute_path(abs_path, sizeof(abs_path), path)) {
788794
return __WASI_ENOMEM;
789795
}
790-
796+
791797
if (is_dir) {
792798
return __WASI_ENOTDIR;
793799
}

0 commit comments

Comments
 (0)