diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-10-30 21:42:41 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-10-30 21:42:41 +0000 |
commit | 643d28471fd2f87e85a06875754c57e122bb4848 (patch) | |
tree | fd923b0b385a67fb06fa225bf4ce4483d09d6339 /libc/unistd/getcwd.c | |
parent | 48b5bb26b950baaf7303a81d32cde8c031003ba7 (diff) |
getcwd did not include the alloc-space-as-needed-on-NULL-buffer
feature which busybox needed. So I added it.
Diffstat (limited to 'libc/unistd/getcwd.c')
-rw-r--r-- | libc/unistd/getcwd.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/libc/unistd/getcwd.c b/libc/unistd/getcwd.c index 12eda45ca..c0be5c39e 100644 --- a/libc/unistd/getcwd.c +++ b/libc/unistd/getcwd.c @@ -1,13 +1,10 @@ - +#include <stdlib.h> #include <errno.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> -/* - * These functions find the absolute path to the current working directory. - * - * They don't use malloc or large amounts of stack space. - */ + +/* These functions find the absolute path to the current working directory. */ static char *recurser(); /* Routine to go up tree */ static char *search_dir(); /* Routine to find the step back down */ @@ -19,21 +16,28 @@ static ino_t root_ino; static struct stat st; -char *getcwd(buf, size) -char *buf; -int size; +char *getcwd( char *buf, int size) { - path_buf = buf; path_size = size; if (size < 3) { errno = ERANGE; - return 0; + return NULL; } + + if (buf != NULL) + path_buf = buf; + else + { + path_buf = malloc (size); + if (path_buf == NULL) + return NULL; + } + strcpy(path_buf, "."); if (stat("/", &st) < 0) - return 0; + return NULL; root_dev = st.st_dev; root_ino = st.st_ino; |