MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / __syscall_getcwd

Function __syscall_getcwd

system/lib/wasmfs/syscalls.cpp:747–792  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

745}
746
747int __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

Callers

nothing calls this directly

Calls 9

strcpyFunction · 0.85
getCWDMethod · 0.80
getRootDirectoryMethod · 0.80
getParentMethod · 0.80
lockedMethod · 0.45
getNameMethod · 0.45
emptyMethod · 0.45
lengthMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected