From 017c8132edae466e2892faec7ef6b834dfecbd34 Mon Sep 17 00:00:00 2001
From: Eric Andersen <andersen@codepoet.org>
Date: Fri, 27 Jun 2003 10:43:43 +0000
Subject: Yet more cleanup for the reentrant pwd/grp functions so they should
 now actually be doing the right thing

---
 libc/pwd_grp/fgetpwent.c | 11 ++++++++---
 libc/pwd_grp/fgetspent.c | 11 ++++++++---
 libc/pwd_grp/getpwnam.c  |  6 +++++-
 libc/pwd_grp/getpwuid.c  |  7 +++++--
 libc/pwd_grp/getspnam.c  |  7 +++++--
 libc/pwd_grp/getspuid.c  |  8 ++++++--
 libc/pwd_grp/pwent.c     |  7 +++++--
 libc/pwd_grp/sgetspent.c | 11 ++++++++---
 libc/pwd_grp/spent.c     |  7 +++++--
 9 files changed, 55 insertions(+), 20 deletions(-)

(limited to 'libc')

diff --git a/libc/pwd_grp/fgetpwent.c b/libc/pwd_grp/fgetpwent.c
index 8917c29d0..174629fcb 100644
--- a/libc/pwd_grp/fgetpwent.c
+++ b/libc/pwd_grp/fgetpwent.c
@@ -34,12 +34,16 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
 int fgetpwent_r (FILE *file, struct passwd *password,
-	char *buff, size_t buflen, struct passwd **crap)
+	char *buff, size_t buflen, struct passwd **result)
 {
+    int res;
     if (file == NULL) {
 	return EINTR;
     }
-    return(__getpwent_r(password, buff, buflen, fileno(file)));
+    *result = NULL;
+    res = __getpwent_r(password, buff, buflen, fileno(file));
+    *result = password;
+    return res;
 }
 
 struct passwd *fgetpwent(FILE * file)
@@ -47,9 +51,10 @@ struct passwd *fgetpwent(FILE * file)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct passwd pwd;
+    struct passwd *result;
 
     LOCK;
-    if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &pwd;
     }
diff --git a/libc/pwd_grp/fgetspent.c b/libc/pwd_grp/fgetspent.c
index 79594ecea..2ecf3a174 100644
--- a/libc/pwd_grp/fgetspent.c
+++ b/libc/pwd_grp/fgetspent.c
@@ -33,12 +33,16 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
 int fgetspent_r (FILE *file, struct spwd *spwd,
-	char *buff, size_t buflen, struct spwd **crap)
+	char *buff, size_t buflen, struct spwd **result)
 {
+    int res;
     if (file == NULL) {
 	return EINTR;
     }
-    return(__getspent_r(spwd, buff, buflen, fileno(file)));
+    *result = NULL;
+    res = __getspent_r(spwd, buff, buflen, fileno(file));
+    *result = spwd;
+    return res;
 }
 
 struct spwd *fgetspent(FILE * file)
@@ -46,9 +50,10 @@ struct spwd *fgetspent(FILE * file)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
+    struct spwd *result;
 
     LOCK;
-    if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
diff --git a/libc/pwd_grp/getpwnam.c b/libc/pwd_grp/getpwnam.c
index 6f041e2d8..7e81fa081 100644
--- a/libc/pwd_grp/getpwnam.c
+++ b/libc/pwd_grp/getpwnam.c
@@ -42,6 +42,8 @@ int getpwnam_r (const char *name, struct passwd *password,
     int ret;
     int passwd_fd;
 
+    *result = NULL;
+
     if (name == NULL) {
 	return EINVAL;
     }
@@ -54,6 +56,7 @@ int getpwnam_r (const char *name, struct passwd *password,
 	if (!strcmp(password->pw_name, name)) {
 	    *result=password;
 	    close(passwd_fd);
+	    *result = password;
 	    return 0;
 	}
     }
@@ -66,7 +69,8 @@ struct passwd *getpwnam(const char *name)
 {
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd, *result;
+    static struct passwd pwd;
+    struct passwd *result;
 
     LOCK;
     if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
diff --git a/libc/pwd_grp/getpwuid.c b/libc/pwd_grp/getpwuid.c
index c54df0977..4a85aa289 100644
--- a/libc/pwd_grp/getpwuid.c
+++ b/libc/pwd_grp/getpwuid.c
@@ -37,16 +37,18 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif      
 
 int getpwuid_r (uid_t uid, struct passwd *password,
-	char *buff, size_t buflen, struct passwd **crap)
+	char *buff, size_t buflen, struct passwd **result)
 {
     int passwd_fd;
 
     if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
 	return errno;
 
+    *result = NULL;
     while (__getpwent_r(password, buff, buflen, passwd_fd) == 0)
 	if (password->pw_uid == uid) {
 	    close(passwd_fd);
+	    *result = password;
 	    return 0;
 	}
 
@@ -60,9 +62,10 @@ struct passwd *getpwuid(uid_t uid)
     /* file descriptor for the password file currently open */
     static char line_buff[PWD_BUFFER_SIZE];
     static struct passwd pwd;
+    struct passwd *result;
 
     LOCK;
-    if ((ret=getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), NULL)) == 0) {
+    if ((ret=getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &pwd;
     }
diff --git a/libc/pwd_grp/getspnam.c b/libc/pwd_grp/getspnam.c
index 6f3d3a128..f0a4155bf 100644
--- a/libc/pwd_grp/getspnam.c
+++ b/libc/pwd_grp/getspnam.c
@@ -35,7 +35,7 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif      
 
 int getspnam_r (const char *name, struct spwd *spwd,
-	char *buff, size_t buflen, struct spwd **crap)
+	char *buff, size_t buflen, struct spwd **result)
 {
     int spwd_fd;
 
@@ -46,9 +46,11 @@ int getspnam_r (const char *name, struct spwd *spwd,
     if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
 	return errno;
 
+    *result = NULL;
     while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
 	if (!strcmp(spwd->sp_namp, name)) {
 	    close(spwd_fd);
+	    *result = spwd;
 	    return 0;
 	}
 
@@ -61,9 +63,10 @@ struct spwd *getspnam(const char *name)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
+    struct spwd *result;
 
     LOCK;
-    if ((ret=getspnam_r(name, &spwd, line_buff,  sizeof(line_buff), NULL)) == 0) {
+    if ((ret=getspnam_r(name, &spwd, line_buff,  sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
diff --git a/libc/pwd_grp/getspuid.c b/libc/pwd_grp/getspuid.c
index 3fa7fb5a1..b25fddb49 100644
--- a/libc/pwd_grp/getspuid.c
+++ b/libc/pwd_grp/getspuid.c
@@ -41,11 +41,14 @@ int getspuid_r (uid_t uid, struct spwd *spwd,
     char pwd_buff[PWD_BUFFER_SIZE];
     struct passwd password;
 
+    *result = NULL;
     ret = getpwuid_r(uid, &password, pwd_buff,  sizeof(pwd_buff), NULL);
     if (ret != 0)
 	return ret;
 
-    return getspnam_r(password.pw_name, spwd, buff, buflen, result);
+    ret = getspnam_r(password.pw_name, spwd, buff, buflen, result);
+    *result = spwd;
+    return ret;
 }
 
 struct spwd *getspuid(uid_t uid)
@@ -53,9 +56,10 @@ struct spwd *getspuid(uid_t uid)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
+    struct spwd *result;
 
     LOCK;
-    if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
diff --git a/libc/pwd_grp/pwent.c b/libc/pwd_grp/pwent.c
index f70a6e378..51d2fba96 100644
--- a/libc/pwd_grp/pwent.c
+++ b/libc/pwd_grp/pwent.c
@@ -65,12 +65,14 @@ void endpwent(void)
 }
 
 int getpwent_r (struct passwd *password, char *buff, 
-	size_t buflen, struct passwd **crap)
+	size_t buflen, struct passwd **result)
 {
     int ret;
     LOCK;
+    *result = NULL;
     if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
 	UNLOCK;
+	*result = password;
 	return 0;
     }
     UNLOCK;
@@ -83,8 +85,9 @@ struct passwd *getpwent(void)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct passwd pwd;
+    struct passwd *result;
 
-    if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	return &pwd;
     }
     __set_errno(ret);
diff --git a/libc/pwd_grp/sgetspent.c b/libc/pwd_grp/sgetspent.c
index 5f018b9f1..d2e2ca71a 100644
--- a/libc/pwd_grp/sgetspent.c
+++ b/libc/pwd_grp/sgetspent.c
@@ -33,9 +33,13 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
 #endif      
 
 int sgetspent_r (const char *string, struct spwd *spwd,
-	char *buff, size_t buflen, struct spwd **crap)
+	char *buff, size_t buflen, struct spwd **result)
 {
-    return(__sgetspent_r(string, spwd, buff, buflen));
+    int ret;
+    *result = NULL;
+    ret = __sgetspent_r(string, spwd, buff, buflen);
+    *result = spwd;
+    return ret;
 }
 
 struct spwd *sgetspent(const char *string)
@@ -43,9 +47,10 @@ struct spwd *sgetspent(const char *string)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
+    struct spwd *result;
 
     LOCK;
-    if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
diff --git a/libc/pwd_grp/spent.c b/libc/pwd_grp/spent.c
index b281072f4..734854b24 100644
--- a/libc/pwd_grp/spent.c
+++ b/libc/pwd_grp/spent.c
@@ -62,12 +62,14 @@ void endspent(void)
 }
 
 int getspent_r (struct spwd *spwd, char *buff, 
-	size_t buflen, struct spwd **crap)
+	size_t buflen, struct spwd **result)
 {
     int ret;
     LOCK;
+    *result = NULL;
     if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) {
 	UNLOCK;
+	*result = spwd;
 	return 0;
     }
     UNLOCK;
@@ -79,9 +81,10 @@ struct spwd *getspent(void)
     int ret;
     static char line_buff[PWD_BUFFER_SIZE];
     static struct spwd spwd;
+    struct spwd *result;
 
     LOCK;
-    if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
+    if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), &result)) == 0) {
 	UNLOCK;
 	return &spwd;
     }
-- 
cgit v1.2.3