Change sys_getcd() to take no arguments and always return malloc'ed memory (or NULL).
authorJeremy Allison <jra@samba.org>
Tue, 31 May 2011 23:14:04 +0000 (16:14 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 1 Jun 2011 00:54:51 +0000 (02:54 +0200)
Part of the efforts to remove PATH_MAX on modern systems.

source3/client/client.c
source3/include/proto.h
source3/lib/system.c
source3/modules/vfs_default.c
source3/printing/print_generic.c

index 7cffddb76cab7ba7475cabb3ff4c1c1ebfe96e2e..f088c7c8158a87cdde77eca53ae4c2dde6b21f4b 100644 (file)
@@ -3876,11 +3876,12 @@ static int cmd_lcd(void)
                                buf, strerror(errno));
                }
        }
-       d = TALLOC_ARRAY(ctx, char, PATH_MAX+1);
+       d = sys_getwd();
        if (!d) {
                return 1;
        }
-       DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
+       DEBUG(2,("the local directory is now %s\n",d));
+       SAFE_FREE(d);
        return 0;
 }
 
index 23654e1ec6e9c7ee96ead6a6e72303ff67b1a0c5..83382594f91ce532af77e0bc7a996a1ae9e8f14f 100644 (file)
@@ -391,7 +391,7 @@ void sys_rewinddir(SMB_STRUCT_DIR *dirp);
 int sys_closedir(SMB_STRUCT_DIR *dirp);
 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev);
 int sys_waitpid(pid_t pid,int *status,int options);
-char *sys_getwd(char *s);
+char *sys_getwd(void);
 void set_effective_capability(enum smbd_capability capability);
 void drop_effective_capability(enum smbd_capability capability);
 long sys_random(void);
index 0dd4b81a43adeb60d87ef274264131cb89468b68..a6f7de0af50f8d10ddb483e3c993a51a20475713 100644 (file)
@@ -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)
index 4d06a10f42607a58cc4ff91004c87cff8796cf87..faacf25599216b53d1ead72f586cddcde2463109 100644 (file)
@@ -753,9 +753,11 @@ static char *vfswrap_getwd(vfs_handle_struct *handle,  char *path)
        char *result;
 
        START_PROFILE(syscall_getwd);
-       result = sys_getwd(path);
+       result = sys_getwd();
        END_PROFILE(syscall_getwd);
-       return result;
+       /* FIXME - with a VFS change. JRA !! */
+       strlcpy(path, result, PATH_MAX);
+       return path;
 }
 
 /*********************************************************************
index b925bedaceb6e99e4e435a69fc24615e87a25687..14f4c6dbe79e23ff5c2e00f95435ca9b3c7c243a 100644 (file)
@@ -155,17 +155,17 @@ static int generic_job_submit(int snum, struct printjob *pjob)
 
        /* we print from the directory path to give the best chance of
            parsing the lpq output */
-       current_directory = TALLOC_ARRAY(ctx,
-                                       char,
-                                       PATH_MAX+1);
-       if (!current_directory) {
-               return -1;
-       }
-       wd = sys_getwd(current_directory);
+       wd = sys_getwd();
        if (!wd) {
                return -1;
        }
 
+       current_directory = talloc_strdup(ctx, wd);
+       SAFE_FREE(wd);
+
+       if (!current_directory) {
+               return -1;
+       }
        print_directory = talloc_strdup(ctx, pjob->filename);
        if (!print_directory) {
                return -1;
@@ -205,7 +205,7 @@ static int generic_job_submit(int snum, struct printjob *pjob)
 
  out:
 
-       if (chdir(wd) == -1) {
+       if (chdir(current_directory) == -1) {
                smb_panic("chdir failed in generic_job_submit");
        }
        TALLOC_FREE(current_directory);