about summary refs log tree commit diff
path: root/src/passwd/getpw_r.c
diff options
context:
space:
mode:
authorJosiah Worcester <josiahw@gmail.com>2015-02-10 18:32:55 -0600
committerRich Felker <dalias@aerifal.cx>2015-02-10 22:57:02 -0500
commit700e08993c3f6a808773d56424aa7e633da13e2e (patch)
tree752558943d55b652eb61469e818ac1dceaad6238 /src/passwd/getpw_r.c
parent74e334dcd177b585c64ddafa732a3dc9e3f6b5ec (diff)
downloadmusl-700e08993c3f6a808773d56424aa7e633da13e2e.tar.gz
musl-700e08993c3f6a808773d56424aa7e633da13e2e.tar.xz
musl-700e08993c3f6a808773d56424aa7e633da13e2e.zip
refactor passwd file access code
this allows getpwnam and getpwuid to share code with the _r versions
in preparation for alternate backend support.
Diffstat (limited to 'src/passwd/getpw_r.c')
-rw-r--r--src/passwd/getpw_r.c36
1 files changed, 11 insertions, 25 deletions
diff --git a/src/passwd/getpw_r.c b/src/passwd/getpw_r.c
index 28552572..0e71e43f 100644
--- a/src/passwd/getpw_r.c
+++ b/src/passwd/getpw_r.c
@@ -5,7 +5,6 @@
 
 static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
 {
-	FILE *f;
 	char *line = 0;
 	size_t len = 0;
 	int rv = 0;
@@ -13,33 +12,20 @@ static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, si
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 
-	f = fopen("/etc/passwd", "rbe");
-	if (!f) {
-		rv = errno;
-		goto done;
+	rv = __getpw_a(name, uid, pw, &line, &len, res);
+	if (!rv && size < len) {
+		*res = 0;
+		rv = ERANGE;
 	}
-
-	*res = 0;
-	while (__getpwent_a(f, pw, &line, &len)) {
-		if (name && !strcmp(name, pw->pw_name)
-		|| !name && pw->pw_uid == uid) {
-			if (size < len) {
-				rv = ERANGE;
-				break;
-			}
-			*res = pw;
-			memcpy(buf, line, len);
-			FIX(name);
-			FIX(passwd);
-			FIX(gecos);
-			FIX(dir);
-			FIX(shell);
-			break;
-		}
+	if (!rv) {
+		memcpy(buf, line, len);
+		FIX(name);
+		FIX(passwd);
+		FIX(gecos);
+		FIX(dir);
+		FIX(shell);
 	}
  	free(line);
-	fclose(f);
-done:
 	pthread_setcancelstate(cs, 0);
 	return rv;
 }