You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by da...@apache.org on 2021/02/26 10:32:25 UTC

[incubator-nuttx] branch master updated: unistd/getcwd: enhance getcwd when buf is NULL

This is an automated email from the ASF dual-hosted git repository.

davids5 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6cac6c5  unistd/getcwd: enhance getcwd when buf is NULL
6cac6c5 is described below

commit 6cac6c50d1b99ccc2902a9c443ed1814a669321a
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Mon Jan 25 12:40:27 2021 +0800

    unistd/getcwd: enhance getcwd when buf is NULL
    
    Change-Id: I595e1874349f089691dc5291f31fb376cf0e2e2c
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 libs/libc/unistd/lib_getcwd.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/libs/libc/unistd/lib_getcwd.c b/libs/libc/unistd/lib_getcwd.c
index 7e2c07d..cfded24 100644
--- a/libs/libc/unistd/lib_getcwd.c
+++ b/libs/libc/unistd/lib_getcwd.c
@@ -63,6 +63,12 @@
  *   symbolic links. The 'size' argument is the size in bytes of the
  *   character array pointed to by the 'buf' argument.
  *
+ *   As an extension to the POSIX.1-2001 standard, getcwd() allocates
+ *   the buffer dynamically using lib_malloc if buf is NULL. In this case,
+ *   the allocated buffer has the length size unless size is zero, when buf
+ *   is allocated as big as necessary. The caller should free the
+ *   returned buffer.
+ *
  * Input Parameters:
  *   buf - a pointer to the location in which the current working directory
  *     pathname is returned.
@@ -74,7 +80,7 @@
  *   the error:
  *
  *   EINVAL
- *     The 'size' argument is 0 or the 'buf' argument is NULL.
+ *     The 'size' argument is 0 and the 'buf' argument is not NULL.
  *   ERANGE
  *     The size argument is greater than 0, but is smaller than the length
  *     of the current working directory pathname +1.
@@ -91,18 +97,21 @@ FAR char *getcwd(FAR char *buf, size_t size)
 
   /* Verify input parameters */
 
-#ifdef CONFIG_DEBUG_FEATURES
-  if (!buf || !size)
+  if (buf && size == 0)
     {
       set_errno(EINVAL);
       return NULL;
     }
-#endif
+
+  if (size == 0)
+    {
+      size = PATH_MAX + 1;
+    }
 
   /* If no working directory is defined, then default to the home directory */
 
   pwd = getenv("PWD");
-  if (!pwd)
+  if (pwd == NULL)
     {
       pwd = CONFIG_LIB_HOMEDIR;
     }
@@ -115,6 +124,16 @@ FAR char *getcwd(FAR char *buf, size_t size)
       return NULL;
     }
 
+  if (buf == NULL)
+    {
+      buf = lib_malloc(size);
+      if (!buf)
+        {
+          set_errno(ENOMEM);
+          return NULL;
+        }
+    }
+
   /* Copy the cwd to the user buffer */
 
   strcpy(buf, pwd);