|
46 | 46 |
|
47 | 47 | // We will take the maximum number of open files |
48 | 48 | // 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 |
50 | 50 |
|
51 | 51 | // Macro to retrieve a file system descriptor and check it's validity. |
52 | 52 | #define GET_FILE_SYSTEM_DESCRIPTOR(fd, ptr) \ |
@@ -598,43 +598,49 @@ __wasi_errno_t |
598 | 598 | os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt, |
599 | 599 | size_t *nwritten) |
600 | 600 | { |
601 | | - struct zephyr_fs_desc *ptr = NULL; |
602 | 601 | ssize_t total_written = 0; |
603 | | - char buffer[256]; |
604 | 602 |
|
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 | + } |
606 | 609 |
|
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. |
611 | 610 | 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); |
618 | 613 |
|
619 | | - } |
620 | | - *nwritten = total_written; |
| 614 | + if (bytes_written < 0) { |
| 615 | + return convert_errno(-bytes_written); |
| 616 | + } |
621 | 617 |
|
622 | | - return __WASI_ESUCCESS; |
623 | | - } |
| 618 | + total_written += bytes_written; |
624 | 619 |
|
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 | + } |
631 | 624 | } |
| 625 | + } |
| 626 | + else { |
| 627 | + struct zephyr_fs_desc *ptr = NULL; |
| 628 | + GET_FILE_SYSTEM_DESCRIPTOR(handle->fd, ptr); |
632 | 629 |
|
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 | + } |
634 | 637 |
|
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 | + } |
638 | 644 | } |
639 | 645 | } |
640 | 646 |
|
@@ -787,7 +793,7 @@ os_unlinkat(os_file_handle handle, const char *path, bool is_dir) |
787 | 793 | if (!build_absolute_path(abs_path, sizeof(abs_path), path)) { |
788 | 794 | return __WASI_ENOMEM; |
789 | 795 | } |
790 | | - |
| 796 | + |
791 | 797 | if (is_dir) { |
792 | 798 | return __WASI_ENOTDIR; |
793 | 799 | } |
|
0 commit comments