1 #include <unistd.h> 2 #include <errno.h> 3 #include <string.h> 4 5 // For threads this needs to synchronize with chdir 6 #ifdef _REENTRANT 7 #error "getcwd doesn't yet support multiple threads" 8 #endif 9 10 char *__wasilibc_cwd = "/"; 11 getcwd(char * buf,size_t size)12char *getcwd(char *buf, size_t size) 13 { 14 if (!buf) { 15 buf = strdup(__wasilibc_cwd); 16 if (!buf) { 17 errno = ENOMEM; 18 return NULL; 19 } 20 } else { 21 size_t len = strlen(__wasilibc_cwd); 22 if (size < len + 1) { 23 errno = ERANGE; 24 return NULL; 25 } 26 strcpy(buf, __wasilibc_cwd); 27 } 28 return buf; 29 } 30 31