| 745 | } |
| 746 | |
| 747 | int __syscall_getcwd(char* buf, size_t size) { |
| 748 | // Check if buf points to a bad address. |
| 749 | if (!buf && size > 0) { |
| 750 | return -EFAULT; |
| 751 | } |
| 752 | |
| 753 | // Check if the size argument is zero and buf is not a null pointer. |
| 754 | if (buf && size == 0) { |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | |
| 758 | auto curr = wasmFS.getCWD(); |
| 759 | |
| 760 | std::string result = ""; |
| 761 | |
| 762 | while (curr != wasmFS.getRootDirectory()) { |
| 763 | auto parent = curr->locked().getParent(); |
| 764 | // Check if the parent exists. The parent may not exist if the CWD or one |
| 765 | // of its ancestors has been unlinked. |
| 766 | if (!parent) { |
| 767 | return -ENOENT; |
| 768 | } |
| 769 | |
| 770 | auto name = parent->locked().getName(curr); |
| 771 | result = '/' + name + result; |
| 772 | curr = parent; |
| 773 | } |
| 774 | |
| 775 | // Check if the cwd is the root directory. |
| 776 | if (result.empty()) { |
| 777 | result = "/"; |
| 778 | } |
| 779 | |
| 780 | int len = result.length() + 1; |
| 781 | |
| 782 | // Check if the size argument is less than the length of the absolute |
| 783 | // pathname of the working directory, including null terminator. |
| 784 | if (len > size) { |
| 785 | return -ERANGE; |
| 786 | } |
| 787 | |
| 788 | // Return value is a null-terminated c string. |
| 789 | strcpy(buf, result.c_str()); |
| 790 | |
| 791 | return len; |
| 792 | } |
| 793 | |
| 794 | __wasi_errno_t __wasi_fd_fdstat_get(__wasi_fd_t fd, __wasi_fdstat_t* stat) { |
| 795 | // TODO: This is only partial implementation of __wasi_fd_fdstat_get. Enough |