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

pkarashchenko 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 96ff41d5b2 libc: Don't duplicate string in chdir and lib_restoredir
96ff41d5b2 is described below

commit 96ff41d5b2da65333508be5dddc9bc9b05ff5791
Author: Xiang Xiao <[email protected]>
AuthorDate: Sun Apr 17 22:21:22 2022 +0800

    libc: Don't duplicate string in chdir and lib_restoredir
    
    since the new layout doesn't reallocate the unchanged environ variable 
anymore
    
    Signed-off-by: Xiang Xiao <[email protected]>
---
 libs/libc/stdlib/lib_realpath.c   |  1 +
 libs/libc/unistd/lib_chdir.c      | 70 ++++++++-------------------------------
 libs/libc/unistd/lib_restoredir.c |  8 ++---
 3 files changed, 16 insertions(+), 63 deletions(-)

diff --git a/libs/libc/stdlib/lib_realpath.c b/libs/libc/stdlib/lib_realpath.c
index 466432a7a0..86ec60ca2b 100644
--- a/libs/libc/stdlib/lib_realpath.c
+++ b/libs/libc/stdlib/lib_realpath.c
@@ -67,6 +67,7 @@ FAR char *realpath(FAR const char *path, FAR char *resolved)
       fres = resolved = lib_malloc(PATH_MAX);
       if (resolved == NULL)
         {
+          set_errno(ENOMEM);
           return NULL;
         }
     }
diff --git a/libs/libc/unistd/lib_chdir.c b/libs/libc/unistd/lib_chdir.c
index b4f6c1b244..82438aa812 100644
--- a/libs/libc/unistd/lib_chdir.c
+++ b/libs/libc/unistd/lib_chdir.c
@@ -26,7 +26,6 @@
 
 #include <sys/stat.h>
 #include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 #include <errno.h>
 
@@ -34,30 +33,6 @@
 
 #ifndef CONFIG_DISABLE_ENVIRON
 
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: _trimdir
- ****************************************************************************/
-
-#if 0
-static inline void _trimdir(char *path)
-{
-  /* Skip any trailing '/' characters (unless it is also the leading '/') */
-
-  int len = strlen(path) - 1;
-  while (len > 0 && path[len] == '/')
-    {
-      path[len] = '\0';
-      len--;
-    }
-}
-#else
-#  define _trimdir(p)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -98,42 +73,35 @@ static inline void _trimdir(char *path)
 int chdir(FAR const char *path)
 {
   struct stat buf;
-  char *oldpwd;
-  char *alloc;
-  char *abspath;
-  int errcode;
+  FAR char *oldpwd;
+  FAR char *abspath;
   int ret;
 
-  /* Verify the input parameters */
-
-  if (!path)
-    {
-      errcode = ENOENT;
-      goto errout;
-    }
-
   /* Verify that 'path' refers to a directory */
 
   ret = stat(path, &buf);
   if (ret < 0)
     {
-      errcode = ENOENT;
-      goto errout;
+      return ret;
     }
 
   /* Something exists here... is it a directory? */
 
   if (!S_ISDIR(buf.st_mode))
     {
-      errcode = ENOTDIR;
-      goto errout;
+      set_errno(ENOTDIR);
+      return ERROR;
     }
 
   /* Yes, it is a directory.
    * Remove any trailing '/' characters from the path
    */
 
-  _trimdir(path);
+  abspath = realpath(path, NULL);
+  if (abspath == NULL)
+    {
+      return ERROR;
+    }
 
   /* Replace any preceding OLDPWD with the current PWD (this is to
    * support 'cd -' in NSH)
@@ -146,26 +114,14 @@ int chdir(FAR const char *path)
       oldpwd = CONFIG_LIBC_HOMEDIR;
     }
 
-  alloc = strdup(oldpwd);  /* kludge needed because environment is realloc'ed 
*/
-  setenv("OLDPWD", alloc, TRUE);
-  lib_free(alloc);
+  setenv("OLDPWD", oldpwd, TRUE);
 
   /* Set the cwd to the input 'path' */
 
-  abspath = realpath(path, NULL);
-  if (abspath == NULL)
-    {
-      errcode = ENOENT;
-      goto errout;
-    }
-
-  setenv("PWD", abspath, TRUE);
+  ret = setenv("PWD", abspath, TRUE);
   lib_free(abspath);
   sched_unlock();
-  return OK;
 
-errout:
-  set_errno(errcode);
-  return ERROR;
+  return ret;
 }
 #endif /* !CONFIG_DISABLE_ENVIRON */
diff --git a/libs/libc/unistd/lib_restoredir.c 
b/libs/libc/unistd/lib_restoredir.c
index 13fa3b6f11..e86f2f3693 100644
--- a/libs/libc/unistd/lib_restoredir.c
+++ b/libs/libc/unistd/lib_restoredir.c
@@ -24,12 +24,10 @@
 
 #include <nuttx/config.h>
 
-#include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
 
-#include "libc.h"
-
 #ifndef CONFIG_DISABLE_ENVIRON
 
 /****************************************************************************
@@ -42,15 +40,13 @@
 
 int lib_restoredir(void)
 {
-  char *oldpwd;
+  FAR char *oldpwd;
   int ret = OK;
 
   oldpwd = getenv("OLDPWD");
   if (oldpwd)
     {
-      oldpwd = strdup(oldpwd);  /* kludge needed because environment is 
realloc'ed */
       ret = setenv("PWD", oldpwd, TRUE);
-      lib_free(oldpwd);
     }
 
   return ret;

Reply via email to