summaryrefslogtreecommitdiff
path: root/source3/lib/system.c
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2011-05-31 16:14:04 -0700
committerJeremy Allison <jra@samba.org>2011-06-01 02:54:51 +0200
commit1cee71713f75dbee653ea86bd4e7c87efe677cf6 (patch)
treed55d8051935077eda22d05a4078a7097b27d05ae /source3/lib/system.c
parent5abab13851ff2fc3a5792d08cc753c9b479c8cc1 (diff)
downloadsamba-1cee71713f75dbee653ea86bd4e7c87efe677cf6.tar.gz
samba-1cee71713f75dbee653ea86bd4e7c87efe677cf6.tar.bz2
samba-1cee71713f75dbee653ea86bd4e7c87efe677cf6.zip
Change sys_getcd() to take no arguments and always return malloc'ed memory (or NULL).
Part of the efforts to remove PATH_MAX on modern systems.
Diffstat (limited to 'source3/lib/system.c')
-rw-r--r--source3/lib/system.c41
1 files changed, 34 insertions, 7 deletions
diff --git a/source3/lib/system.c b/source3/lib/system.c
index 0dd4b81a43..a6f7de0af5 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)