Rewrite getcwd()

This change addresses a bug that was reported in #923 where bash on
Windows behaved strangely. It turned out that our weak linking of
malloc() caused bash's configure script to favor its own getcwd()
function, which is implemented in the most astonishing way, using
opendir() and readdir() to recursively construct the current path.

This change moves getcwd() into LIBC_STDIO so it can strongly link
malloc(). A new __getcwd() function is now introduced, so all the
low-level runtime services can still use the actual system call. It
provides the Linux Kernel API convention across platforms, and is
overall a higher-quality implementation than what we had before.

In the future, we should probably take a closer look into why bash's
getcwd() polyfill wasn't working as intended on Windows, since there
might be a potential opportunity there to improve our readdir() too.
This commit is contained in:
Justine Tunney 2023-11-02 13:06:23 -07:00
parent a46ec61787
commit 1eb6484c9c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
13 changed files with 251 additions and 211 deletions

View file

@ -13,8 +13,6 @@ COSMOPOLITAN_C_START_
axdx_t __sys_fork(void);
axdx_t __sys_pipe(i32[hasatleast 2], i32);
axdx_t sys_getpid(void);
char *sys_getcwd(char *, u64);
char *sys_getcwd_xnu(char *, u64);
i32 __sys_dup3(i32, i32, i32);
i32 __sys_execve(const char *, char *const[], char *const[]);
i32 __sys_fcntl(i32, i32, ...);
@ -53,6 +51,7 @@ i32 sys_fork(void);
i32 sys_fsync(i32);
i32 sys_ftruncate(i32, i64, i64);
i32 sys_getcontext(void *);
i32 sys_getcwd(char *, u64);
i32 sys_getpgid(i32);
i32 sys_getppid(void);
i32 sys_getpriority(i32, u32);
@ -85,9 +84,9 @@ i32 sys_posix_openpt(i32);
i32 sys_renameat(i32, const char *, i32, const char *);
i32 sys_sem_close(i64);
i32 sys_sem_destroy(i64);
i32 sys_sem_destroy(i64);
i32 sys_sem_getvalue(i64, u32 *);
i32 sys_sem_init(u32, i64 *);
i32 sys_sem_destroy(i64);
i32 sys_sem_open(const char *, int, u32, i64 *);
i32 sys_sem_post(i64);
i32 sys_sem_trywait(i64);