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 <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2023-06-25 19:31:06 +08:00
committed by Alan Carvalho de Assis
parent b9b615b425
commit f1f33917f7
4 changed files with 5 additions and 64 deletions
+1
View File
@@ -21,6 +21,7 @@
# Add the pwd C files to the build # Add the pwd C files to the build
CSRCS += lib_getpwnam.c lib_getpwnamr.c lib_getpwuid.c lib_getpwuidr.c CSRCS += lib_getpwnam.c lib_getpwnamr.c lib_getpwuid.c lib_getpwuidr.c
CSRCS += lib_pwd_globals.c
ifeq ($(CONFIG_LIBC_PASSWD_FILE),y) ifeq ($(CONFIG_LIBC_PASSWD_FILE),y)
CSRCS += lib_find_pwdfile.c lib_pwd_globals.c CSRCS += lib_find_pwdfile.c lib_pwd_globals.c
+4 -58
View File
@@ -24,20 +24,9 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pwd.h> #include <pwd.h>
#include "pwd/lib_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 * 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 *gecos, FAR const char *dir,
FAR const char *shell) FAR const char *shell)
{ {
FAR struct passwd *result; FAR struct passwd *pwd = NULL;
FAR char *newbuf; int ret = getpwbuf_r(uid, gid, name, gecos, dir, shell, &g_passwd,
size_t buflen; g_passwd_buffer, sizeof(g_passwd_buffer), &pwd);
int err; return ret == 0 ? pwd : NULL;
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;
} }
-2
View File
@@ -51,12 +51,10 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
#ifdef CONFIG_LIBC_PASSWD_FILE
/* Data for non-reentrant group functions */ /* Data for non-reentrant group functions */
EXTERN struct passwd g_passwd; EXTERN struct passwd g_passwd;
EXTERN char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE]; EXTERN char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
#endif
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes
-4
View File
@@ -26,8 +26,6 @@
#include "pwd/lib_pwd.h" #include "pwd/lib_pwd.h"
#ifdef CONFIG_LIBC_PASSWD_FILE
/**************************************************************************** /****************************************************************************
* Public Data * Public Data
****************************************************************************/ ****************************************************************************/
@@ -40,5 +38,3 @@ char g_passwd_buffer[CONFIG_LIBC_PASSWD_LINESIZE];
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#endif /* CONFIG_LIBC_GROUP_FILE */