diff --git a/system/lib/libc/musl/src/unistd/getcwd.c b/system/lib/libc/musl/src/unistd/getcwd.c index f407ffe07ed60..13108bdc15298 100644 --- a/system/lib/libc/musl/src/unistd/getcwd.c +++ b/system/lib/libc/musl/src/unistd/getcwd.c @@ -17,7 +17,18 @@ char *getcwd(char *buf, size_t size) long ret = syscall(SYS_getcwd, buf, size); if (ret < 0) return 0; +#ifdef __EMSCRIPTEN__ + // In upstream musl, `buf[0] != '/'` checks for Linux's `(unreachable)` + // prefix returned by the kernel's getcwd syscall. Emscripten's + // __syscall_getcwd never returns `(unreachable)` (it returns -ENOENT + // directly), and under NODERAWFS on Windows valid paths can start with + // a drive letter (e.g. `C:\...`) or UNC prefix (`\\...`). That is, the + // Emscripten change here is to get musl to work properly on Windows (which + // musl does not normally do). + if (ret == 0) { +#else if (ret == 0 || buf[0] != '/') { +#endif errno = ENOENT; return 0; } diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index 6307966e308ef..ecc5f257dc567 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { "a.out.js": 270695, - "a.out.nodebug.wasm": 588110, - "total": 858805, + "a.out.nodebug.wasm": 588098, + "total": 858793, "sent": [ "IMG_Init", "IMG_Load", diff --git a/test/test_other.py b/test/test_other.py index e077c568b8593..fccdcc331baeb 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -9753,6 +9753,7 @@ def test_noderawfs_disables_embedding(self): self.assert_fail(base + ['--preload-file', 'somefile'], expected) self.assert_fail(base + ['--embed-file', 'somefile'], expected) + @crossplatform def test_noderawfs_access_abspath(self): create_file('foo', 'bar') create_file('access.c', r''' @@ -9763,6 +9764,24 @@ def test_noderawfs_access_abspath(self): ''') self.do_runf('access.c', cflags=['-sNODERAWFS'], args=[os.path.abspath('foo')]) + @crossplatform + def test_noderawfs_getcwd(self): + create_file('getcwd.c', r''' + #include + #include + #include + #include + + int main() { + char buf[PATH_MAX]; + char* cwd = getcwd(buf, sizeof(buf)); + assert(cwd == buf); + printf("cwd: %s\n", cwd); + return 0; + } + ''') + self.do_runf('getcwd.c', f'cwd: {os.getcwd()}\n', cflags=['-sNODERAWFS']) + def test_noderawfs_readfile_prerun(self): create_file('foo', 'bar') self.add_pre_run("console.log(FS.readFile('foo', { encoding: 'utf8' }));") diff --git a/test/unistd/unlink.c b/test/unistd/unlink.c index 5872004236881..96fc227368309 100644 --- a/test/unistd/unlink.c +++ b/test/unistd/unlink.c @@ -140,7 +140,8 @@ void test() { // Update: Removing cwd on Linux does not return EBUSY. // WASMFS behaviour will match the native FS. #ifndef __APPLE__ - getcwd(buffer, sizeof(buffer)); + char* cwd = getcwd(buffer, sizeof(buffer)); + assert(cwd != NULL); printf("CWD: %s\n", buffer); err = rmdir(buffer); assert(err == -1);