diff options
Diffstat (limited to 'source3/lib/system.c')
-rw-r--r-- | source3/lib/system.c | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/source3/lib/system.c b/source3/lib/system.c index 0dd4b81a43a..a6f7de0af50 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -959,18 +959,45 @@ int sys_waitpid(pid_t pid,int *status,int options) } /******************************************************************* - System wrapper for getwd + System wrapper for getwd. Always returns MALLOC'ed memory, or NULL + on error (malloc fail usually). ********************************************************************/ -char *sys_getwd(char *s) +char *sys_getwd(void) { - char *wd; -#ifdef HAVE_GETCWD - wd = (char *)getcwd(s, PATH_MAX); +#ifdef GETCWD_TAKES_NULL + return getcwd(NULL, 0); +#elif HAVE_GETCWD + char *wd = NULL, *s = NULL; + size_t allocated = PATH_MAX; + + while (1) { + s = SMB_REALLOC_ARRAY(s, char, allocated); + if (s == NULL) { + return NULL; + } + wd = getcwd(s, allocated); + if (wd) { + break; + } + if (errno != ERANGE) { + SAFE_FREE(s); + break; + } + allocated *= 2; + if (allocated < PATH_MAX) { + SAFE_FREE(s); + break; + } + } + return wd; #else - wd = (char *)getwd(s); + char *s = SMB_MALLOC_ARRAY(char, PATH_MAX); + if (s == NULL) { + return NULL; + } + return getwd(s); #endif - return wd; } #if defined(HAVE_POSIX_CAPABILITIES) |