about summary refs log tree commit diff
path: root/src/passwd/getgr_r.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/passwd/getgr_r.c')
-rw-r--r--src/passwd/getgr_r.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/passwd/getgr_r.c b/src/passwd/getgr_r.c
index 3fe2e2b2..68b867d8 100644
--- a/src/passwd/getgr_r.c
+++ b/src/passwd/getgr_r.c
@@ -5,7 +5,6 @@
 
 static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
 {
-	FILE *f;
 	char *line = 0;
 	size_t len = 0;
 	char **mem = 0;
@@ -16,37 +15,24 @@ static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, siz
 
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 
-	f = fopen("/etc/group", "rbe");
-	if (!f) {
-		rv = errno;
-		goto done;
+	rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
+	if (!rv && size < len + (nmem+1)*sizeof(char *) + 32) {
+		*res = 0;
+		rv = ERANGE;
 	}
-
-	*res = 0;
-	while (__getgrent_a(f, gr, &line, &len, &mem, &nmem)) {
-		if (name && !strcmp(name, gr->gr_name)
-		|| !name && gr->gr_gid == gid) {
-			if (size < len + (nmem+1)*sizeof(char *) + 32) {
-				rv = ERANGE;
-				break;
-			}
-			*res = gr;
-			buf += (16-(uintptr_t)buf)%16;
-			gr->gr_mem = (void *)buf;
-			buf += (nmem+1)*sizeof(char *);
-			memcpy(buf, line, len);
-			FIX(name);
-			FIX(passwd);
-			for (i=0; mem[i]; i++)
-				gr->gr_mem[i] = mem[i]-line+buf;
-			gr->gr_mem[i] = 0;
-			break;
-		}
+	if (!rv) {
+		buf += (16-(uintptr_t)buf)%16;
+		gr->gr_mem = (void *)buf;
+		buf += (nmem+1)*sizeof(char *);
+		memcpy(buf, line, len);
+		FIX(name);
+		FIX(passwd);
+		for (i=0; mem[i]; i++)
+			gr->gr_mem[i] = mem[i]-line+buf;
+		gr->gr_mem[i] = 0;
 	}
  	free(mem);
  	free(line);
-	fclose(f);
-done:
 	pthread_setcancelstate(cs, 0);
 	return rv;
 }