You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2023/06/27 19:19:04 UTC

[nuttx] 01/02: libc/pwd: Reuse g_passwd and g_passwd_buffer in getpwbuf

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

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

commit f1f33917f7316ebcbbc9553446953fa668fb52e8
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Jun 25 19:31:06 2023 +0800

    libc/pwd: Reuse g_passwd and g_passwd_buffer in getpwbuf
    
    like other similar functions(e.g. getpwnam and getpwuid)
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/pwd/Make.defs         |  1 +
 libs/libc/pwd/lib_getpwbuf.c    | 62 +++--------------------------------------
 libs/libc/pwd/lib_pwd.h         |  2 --
 libs/libc/pwd/lib_pwd_globals.c |  4 ---
 4 files changed, 5 insertions(+), 64 deletions(-)

diff --git a/libs/libc/pwd/Make.defs b/libs/libc/pwd/Make.defs
index 996555be5a..73b7cb5130 100644
--- a/libs/libc/pwd/Make.defs
+++ b/libs/libc/pwd/Make.defs
@@ -21,6 +21,7 @@
 # Add the pwd C files to the build
 
 CSRCS += lib_getpwnam.c lib_getpwnamr.c lib_getpwuid.c lib_getpwuidr.c
+CSRCS += lib_pwd_globals.c
 
 ifeq ($(CONFIG_LIBC_PASSWD_FILE),y)
 CSRCS += lib_find_pwdfile.c lib_pwd_globals.c
diff --git a/libs/libc/pwd/lib_getpwbuf.c b/libs/libc/pwd/lib_getpwbuf.c
index 5a3fe8f292..7d85f91dd0 100644
--- a/libs/libc/pwd/lib_getpwbuf.c
+++ b/libs/libc/pwd/lib_getpwbuf.c
@@ -24,20 +24,9 @@
 
 #include <nuttx/config.h>
 
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
 #include <pwd.h>
 
 #include "pwd/lib_pwd.h"
-#include "libc.h"
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static FAR char *g_buf;
-static FAR struct passwd *g_pwd;
 
 /****************************************************************************
  * Public Functions
@@ -68,51 +57,8 @@ FAR struct passwd *getpwbuf(uid_t uid, gid_t gid, FAR const char *name,
                             FAR const char *gecos, FAR const char *dir,
                             FAR const char *shell)
 {
-  FAR struct passwd *result;
-  FAR char *newbuf;
-  size_t buflen;
-  int err;
-
-  buflen = strlen(name) + 1 + strlen(gecos) + 1 + strlen(dir) + 1 +
-           strlen(shell) + 1;
-
-  newbuf = (FAR char *)lib_realloc(g_buf, buflen);
-
-  if (!newbuf)
-    {
-      err = ENOMEM;
-      goto error;
-    }
-
-  g_buf = newbuf;
-
-  if (!g_pwd)
-    {
-      g_pwd = (FAR struct passwd *)lib_malloc(sizeof(struct passwd));
-    }
-
-  if (!g_pwd)
-    {
-      err = ENOMEM;
-      goto error;
-    }
-
-  err = getpwbuf_r(uid, gid, name, gecos, dir, shell,
-                   g_pwd, g_buf, buflen, &result);
-
-  if (err)
-    {
-      goto error;
-    }
-
-  return result;
-
-error:
-  lib_free(g_pwd);
-  lib_free(g_buf);
-  g_pwd = NULL;
-  g_buf = NULL;
-  set_errno(err);
-
-  return NULL;
+  FAR struct passwd *pwd = NULL;
+  int ret = getpwbuf_r(uid, gid, name, gecos, dir, shell, &g_passwd,
+                       g_passwd_buffer, sizeof(g_passwd_buffer), &pwd);
+  return ret == 0 ? pwd : NULL;
 }
diff --git a/libs/libc/pwd/lib_pwd.h b/libs/libc/pwd/lib_pwd.h
index 6df60732a6..7a6a0b843c 100644
--- a/libs/libc/pwd/lib_pwd.h
+++ b/libs/libc/pwd/lib_pwd.h
@@ -51,12 +51,10 @@ extern "C"
 #define EXTERN extern
 #endif
 
-#ifdef CONFIG_LIBC_PASSWD_FILE
 /* Data for non-reentrant group functions */
 
 EXTERN struct passwd g_passwd;
 EXTERN char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
-#endif
 
 /****************************************************************************
  * Public Function Prototypes
diff --git a/libs/libc/pwd/lib_pwd_globals.c b/libs/libc/pwd/lib_pwd_globals.c
index dc64df1bec..12deca175a 100644
--- a/libs/libc/pwd/lib_pwd_globals.c
+++ b/libs/libc/pwd/lib_pwd_globals.c
@@ -26,8 +26,6 @@
 
 #include "pwd/lib_pwd.h"
 
-#ifdef CONFIG_LIBC_PASSWD_FILE
-
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -40,5 +38,3 @@ char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
-
-#endif /* CONFIG_LIBC_GROUP_FILE */