[uClibc-cvs] uClibc/libc/pwd_grp __getgrent_r.c, NONE, 1.1 Makefile, 1.21, 1.22 __getpwent_r.c, 1.3, 1.4 __getspent_r.c, 1.5, 1.6 __sgetspent_r.c, 1.3, 1.4 config.h, 1.6, 1.7 fgetgrent.c, 1.7, 1.8 fgetpwent.c, 1.9, 1.10 fgetspent.c, 1.5, 1.6 getgrgid.c, 1.8, 1.9 getgrnam.c, 1.8, 1.9 getpw.c, 1.4, 1.5 getpwnam.c, 1.10, 1.11 getpwuid.c, 1.9, 1.10 getspnam.c, 1.5, 1.6 getspuid.c, 1.5, 1.6 grent.c, 1.9, 1.10 initgroups.c, 1.11, 1.12 lckpwdf.c, 1.3, 1.4 putpwent.c, 1.5, 1.6 putspent.c, 1.2, 1.3 pwent.c, 1.12, 1.13 sgetspent.c, 1.5, 1.6 spent.c, 1.6, 1.7 __getgrent.c, 1.9, NONE

Erik Andersen andersen at uclibc.org
Fri Oct 10 07:34:31 UTC 2003


Update of /var/cvs/uClibc/libc/pwd_grp
In directory winder:/tmp/cvs-serv24001

Modified Files:
	Makefile __getpwent_r.c __getspent_r.c __sgetspent_r.c 
	config.h fgetgrent.c fgetpwent.c fgetspent.c getgrgid.c 
	getgrnam.c getpw.c getpwnam.c getpwuid.c getspnam.c getspuid.c 
	grent.c initgroups.c lckpwdf.c putpwent.c putspent.c pwent.c 
	sgetspent.c spent.c 
Added Files:
	__getgrent_r.c 
Removed Files:
	__getgrent.c 
Log Message:
Implement getgrgid_r and getgrnam_r.  Rework group handling code to be fully
reentrant, since there was still a shared static value.  indent stuff, 


Index: pwent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/pwent.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- pwent.c	27 Jun 2003 19:35:17 -0000	1.12
+++ pwent.c	10 Oct 2003 07:34:27 -0000	1.13
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * pwent.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -47,50 +49,50 @@
 
 void setpwent(void)
 {
-    LOCK;
-    if (pw_fd != -1)
-	close(pw_fd);
+	LOCK;
+	if (pw_fd != -1)
+		close(pw_fd);
 
-    pw_fd = open(_PATH_PASSWD, O_RDONLY);
-    UNLOCK;
+	pw_fd = open(_PATH_PASSWD, O_RDONLY);
+	UNLOCK;
 }
 
 void endpwent(void)
 {
-    LOCK;
-    if (pw_fd != -1)
-	close(pw_fd);
-    pw_fd = -1;
-    UNLOCK;
+	LOCK;
+	if (pw_fd != -1)
+		close(pw_fd);
+	pw_fd = -1;
+	UNLOCK;
 }
 
 int getpwent_r (struct passwd *password, char *buff, 
 	size_t buflen, struct passwd **result)
 {
-    int ret=EINVAL;
-    LOCK;
-    *result = NULL;
-    if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
+	int ret=EINVAL;
+	LOCK;
+	*result = NULL;
+	if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
+		UNLOCK;
+		*result = password;
+		return 0;
+	}
 	UNLOCK;
-	*result = password;
-	return 0;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return ret;
+	__set_errno(ret);
+	return ret;
 }
 
 struct passwd *getpwent(void)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd;
-    struct passwd *result;
+	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), &result)) == 0) {
-	return &pwd;
-    }
-    __set_errno(ret);
-    return NULL;
+	if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		return &pwd;
+	}
+	__set_errno(ret);
+	return NULL;
 }
 

Index: getpwuid.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getpwuid.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- getpwuid.c	27 Jun 2003 10:43:43 -0000	1.9
+++ getpwuid.c	10 Oct 2003 07:34:27 -0000	1.10
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getpwuid.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -39,38 +41,37 @@
 int getpwuid_r (uid_t uid, struct passwd *password,
 	char *buff, size_t buflen, struct passwd **result)
 {
-    int passwd_fd;
-
-    if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
-	return errno;
+	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;
+	*result = NULL;
+	while (__getpwent_r(password, buff, buflen, passwd_fd) == 0) {
+		if (password->pw_uid == uid) {
+			close(passwd_fd);
+			*result = password;
+			return 0;
+		}
 	}
 
-    close(passwd_fd);
-    return EINVAL;
+	close(passwd_fd);
+	return EINVAL;
 }
 
 struct passwd *getpwuid(uid_t uid)
 {
-    int ret;
-    /* file descriptor for the password file currently open */
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd;
-    struct passwd *result;
+	int ret;
+	struct passwd *result;
+	static struct passwd pwd;
+	static char line_buff[PWD_BUFFER_SIZE];
 
-    LOCK;
-    if ((ret=getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), &result)) == 0) {
+	LOCK;
+	if ((ret=getpwuid_r(uid, &pwd, line_buff,  sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &pwd;
+	}
 	UNLOCK;
-	return &pwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }
 

Index: putpwent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/putpwent.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- putpwent.c	10 Sep 2002 05:21:03 -0000	1.5
+++ putpwent.c	10 Oct 2003 07:34:27 -0000	1.6
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * putpwent.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -29,9 +31,9 @@
 		return -1;
 	}
 	if (fprintf
-		(f, "%s:%s:%u:%u:%s:%s:%s\n", passwd->pw_name, passwd->pw_passwd,
-		 passwd->pw_uid, passwd->pw_gid, passwd->pw_gecos, passwd->pw_dir,
-		 passwd->pw_shell) < 0)
+			(f, "%s:%s:%u:%u:%s:%s:%s\n", passwd->pw_name, passwd->pw_passwd,
+			 passwd->pw_uid, passwd->pw_gid, passwd->pw_gecos, passwd->pw_dir,
+			 passwd->pw_shell) < 0)
 		return -1;
 
 	return 0;

Index: spent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/spent.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- spent.c	27 Jun 2003 19:35:17 -0000	1.6
+++ spent.c	10 Oct 2003 07:34:27 -0000	1.7
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * spent.c - Based on pwent.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -45,51 +47,51 @@
 
 void setspent(void)
 {
-    LOCK;
-    if (spwd_fd != -1)
-	close(spwd_fd);
-    spwd_fd = open(_PATH_SHADOW, O_RDONLY);
-    UNLOCK;
+	LOCK;
+	if (spwd_fd != -1)
+		close(spwd_fd);
+	spwd_fd = open(_PATH_SHADOW, O_RDONLY);
+	UNLOCK;
 }
 
 void endspent(void)
 {
-    LOCK;
-    if (spwd_fd != -1)
-	close(spwd_fd);
-    spwd_fd = -1;
-    UNLOCK;
+	LOCK;
+	if (spwd_fd != -1)
+		close(spwd_fd);
+	spwd_fd = -1;
+	UNLOCK;
 }
 
 int getspent_r (struct spwd *spwd, char *buff, 
 	size_t buflen, struct spwd **result)
 {
-    int ret=EINVAL;
-    LOCK;
-    *result = NULL;
-    if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) {
+	int ret=EINVAL;
+	LOCK;
+	*result = NULL;
+	if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) {
+		UNLOCK;
+		*result = spwd;
+		return 0;
+	}
 	UNLOCK;
-	*result = spwd;
-	return 0;
-    }
-    UNLOCK;
-    return ret;
+	return ret;
 }
 
 struct spwd *getspent(void)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct spwd spwd;
-    struct spwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &spwd;
+	}
 	UNLOCK;
-	return &spwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }
 

Index: getspnam.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getspnam.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- getspnam.c	27 Jun 2003 10:43:43 -0000	1.5
+++ getspnam.c	10 Oct 2003 07:34:27 -0000	1.6
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getspnam.c - Based on getpwnam.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -37,41 +39,41 @@
 int getspnam_r (const char *name, struct spwd *spwd,
 	char *buff, size_t buflen, struct spwd **result)
 {
-    int spwd_fd;
+	int spwd_fd;
 
-    if (name == NULL) {
-	return EINVAL;
-    }
+	if (name == NULL) {
+		return EINVAL;
+	}
 
-    if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
-	return errno;
+	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;
-	}
+	*result = NULL;
+	while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
+		if (!strcmp(spwd->sp_namp, name)) {
+			close(spwd_fd);
+			*result = spwd;
+			return 0;
+		}
 
-    close(spwd_fd);
-    return EINVAL;
+	close(spwd_fd);
+	return EINVAL;
 }
 
 struct spwd *getspnam(const char *name)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct spwd spwd;
-    struct spwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret=getspnam_r(name, &spwd, line_buff,  sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &spwd;
+	}
 	UNLOCK;
-	return &spwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }
 

Index: putspent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/putspent.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- putspent.c	12 Jun 2002 23:27:00 -0000	1.2
+++ putspent.c	10 Oct 2003 07:34:27 -0000	1.3
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /* Copyright (C) 1991, 1992, 1996, 1997, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 

Index: sgetspent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/sgetspent.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- sgetspent.c	27 Jun 2003 10:43:43 -0000	1.5
+++ sgetspent.c	10 Oct 2003 07:34:27 -0000	1.6
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * sgetspent.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -35,26 +37,26 @@
 int sgetspent_r (const char *string, struct spwd *spwd,
 	char *buff, size_t buflen, struct spwd **result)
 {
-    int ret;
-    *result = NULL;
-    ret = __sgetspent_r(string, spwd, buff, buflen);
-    *result = spwd;
-    return ret;
+	int ret;
+	*result = NULL;
+	ret = __sgetspent_r(string, spwd, buff, buflen);
+	*result = spwd;
+	return ret;
 }
 
 struct spwd *sgetspent(const char *string)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct spwd spwd;
-    struct spwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &spwd;
+	}
+	__set_errno(ret);
 	UNLOCK;
-	return &spwd;
-    }
-    __set_errno(ret);
-    UNLOCK;
-    return NULL;
+	return NULL;
 }

Index: initgroups.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/initgroups.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- initgroups.c	27 Jun 2003 10:19:29 -0000	1.11
+++ initgroups.c	10 Oct 2003 07:34:27 -0000	1.12
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * initgroups.c - This file is part of the libc-8086/grp package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -28,51 +30,46 @@
 
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-extern pthread_mutex_t __getgrent_lock;
-# define LOCK   pthread_mutex_lock(&__getgrent_lock)
-# define UNLOCK pthread_mutex_unlock(&__getgrent_lock);
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK   pthread_mutex_lock(&mylock)
+# define UNLOCK pthread_mutex_unlock(&mylock);
 #else
 # define LOCK
 # define UNLOCK
 #endif
 
-static char *line_buff = NULL;
-static char **members = NULL;
-
 int initgroups(__const char *user, gid_t gid)
 {
-    register struct group *group;
-
-    gid_t *group_list = NULL;
-    register char **tmp_mem;
-    int num_groups;
-    int grp_fd;
-
+	gid_t *group_list = NULL;
+	register char **tmp_mem;
+	int num_groups;
+	int grp_fd;
+	static struct group group;
+	static char line_buff[PWD_BUFFER_SIZE];
 
-    if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
-	return errno;
+	if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
+		return errno;
 
-    num_groups = 0;
-    group_list = (gid_t *) realloc(group_list, 1);
-    group_list[num_groups] = gid;
-    LOCK;
-    while ((group = __getgrent(grp_fd, line_buff, members)) != NULL)
-    {
-	if (group->gr_gid != gid)
+	num_groups = 0;
+	group_list = (gid_t *) realloc(group_list, 1);
+	group_list[num_groups] = gid;
+	LOCK;
+	while (__getgrent_r(&group, line_buff, sizeof(line_buff), grp_fd) == 0)
 	{
-	    tmp_mem = group->gr_mem;
-	    while (*tmp_mem != NULL) {
-		if (!strcmp(*tmp_mem, user)) {
-		    num_groups++;
-		    group_list = (gid_t *) realloc(group_list, num_groups *
-			    sizeof(gid_t *));
-		    group_list[num_groups-1] = group->gr_gid;
+		if (group.gr_gid != gid)
+		{
+			tmp_mem = group.gr_mem;
+			while (*tmp_mem != NULL) {
+				if (!strcmp(*tmp_mem, user)) {
+					num_groups++;
+					group_list = (gid_t *) realloc(group_list, num_groups * sizeof(gid_t *));
+					group_list[num_groups-1] = group.gr_gid;
+				}
+				tmp_mem++;
+			}
 		}
-		tmp_mem++;
-	    }
 	}
-    }
-    close(grp_fd);
-    UNLOCK;
-    return setgroups(num_groups, group_list);
+	close(grp_fd);
+	UNLOCK;
+	return setgroups(num_groups, group_list);
 }

Index: getgrgid.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getgrgid.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- getgrgid.c	10 Sep 2002 05:53:30 -0000	1.8
+++ getgrgid.c	10 Oct 2003 07:34:26 -0000	1.9
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getgrgid.c - This file is part of the libc-8086/grp package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -22,48 +24,56 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <errno.h>
 #include "config.h"
 
 
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-extern pthread_mutex_t __getgrent_lock;
-# define LOCK   pthread_mutex_lock(&__getgrent_lock)
-# define UNLOCK pthread_mutex_unlock(&__getgrent_lock);
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK   pthread_mutex_lock(&mylock)
+# define UNLOCK pthread_mutex_unlock(&mylock);
 #else
 # define LOCK
 # define UNLOCK
 #endif
-static char *line_buff = NULL;
-static char **members = NULL;
 
 
-struct group *getgrgid(const gid_t gid)
+/* Search for an entry with a matching group ID.  */
+int getgrgid_r (gid_t gid, struct group *group, char *buffer, 
+	size_t buflen, struct group **result)
 {
-    struct group *group;
-    int grp_fd;
-
-    if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
-	return NULL;
+	int grp_fd;
+	if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
+		return errno;
 
-    LOCK;
-    while ((group = __getgrent(grp_fd, line_buff, members)) != NULL)
-	if (group->gr_gid == gid) {
-	    close(grp_fd);
-	    UNLOCK;
-	    return group;
+	*result = NULL;
+	while (__getgrent_r(group, buffer, buflen, grp_fd) == 0) {
+		if (group->gr_gid == gid) {
+			close(grp_fd);
+			*result = group;
+			return 0;
+		}
 	}
 
-    close(grp_fd);
-    UNLOCK;
-    return NULL;
+	close(grp_fd);
+	return EINVAL;
 }
 
-#if 0
-/* Search for an entry with a matching group ID.  */
-int getgrgid_r (gid_t gid, struct group *resultbuf, char *buffer, 
-	size_t buflen, struct group **result)
+struct group *getgrgid(const gid_t gid)
 {
+	int ret;
+	struct group *result;
+	static struct group grp;
+	static char line_buff[GRP_BUFFER_SIZE];
 
+	LOCK;
+	if ((ret=getgrgid_r(gid, &grp, line_buff,  sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &grp;
+	}
+	UNLOCK;
+	__set_errno(ret);
+	return NULL;
 }
-#endif
+

Index: fgetspent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/fgetspent.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- fgetspent.c	27 Jun 2003 10:43:43 -0000	1.5
+++ fgetspent.c	10 Oct 2003 07:34:26 -0000	1.6
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * fgetspent.c - Based on fgetpwent.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -35,29 +37,29 @@
 int fgetspent_r (FILE *file, struct spwd *spwd,
 	char *buff, size_t buflen, struct spwd **result)
 {
-    int res;
-    if (file == NULL) {
-	return EINTR;
-    }
-    *result = NULL;
-    res = __getspent_r(spwd, buff, buflen, fileno(file));
-    *result = spwd;
-    return res;
+	int res;
+	if (file == NULL) {
+		return EINTR;
+	}
+	*result = NULL;
+	res = __getspent_r(spwd, buff, buflen, fileno(file));
+	*result = spwd;
+	return res;
 }
 
 struct spwd *fgetspent(FILE * file)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct spwd spwd;
-    struct spwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &spwd;
+	}
 	UNLOCK;
-	return &spwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }

Index: Makefile
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/Makefile,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- Makefile	31 Oct 2002 18:19:41 -0000	1.21
+++ Makefile	10 Oct 2003 07:34:26 -0000	1.22
@@ -25,7 +25,7 @@
 include $(TOPDIR)Rules.mak
 
 CSRC= pwent.c getpwnam.c getpwuid.c putpwent.c getpw.c fgetpwent.c \
-	__getgrent.c grent.c getgrnam.c getgrgid.c fgetgrent.c \
+	__getgrent_r.c grent.c getgrnam.c getgrgid.c fgetgrent.c \
 	initgroups.c __getpwent_r.c
 
 ifeq ($(HAS_SHADOW),y)
@@ -49,7 +49,7 @@
 
 $(OBJ): Makefile
 
-__getgrent.c: config.h
+__getgrent_r.c: config.h
 initgroups.c: config.h
 
 clean:

Index: __getspent_r.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/__getspent_r.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- __getspent_r.c	27 Jun 2003 10:19:28 -0000	1.5
+++ __getspent_r.c	10 Oct 2003 07:34:26 -0000	1.6
@@ -1,4 +1,6 @@
+/* vi: set sw=4 ts=4: */
 /* __getspent_r.c - Based on __getpwent_r.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -26,32 +28,32 @@
 
 int __getspent_r(struct spwd * spwd, char * line_buff, size_t buflen, int spwd_fd)
 {
-    char *endptr;
-    int line_len;
+	char *endptr;
+	int line_len;
 
-    if (buflen<PWD_BUFFER_SIZE)
-	return ERANGE;
+	if (buflen<PWD_BUFFER_SIZE)
+		return ERANGE;
 
-    /* We use the restart label to handle malformatted lines */
+	/* We use the restart label to handle malformatted lines */
 restart:
-    /* Read the shadow line into the buffer using a minimum of syscalls. */
-    if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
-	return EIO;
-    endptr = strchr(line_buff, '\n');
-    if (endptr != NULL)
-	lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
-    else {
-	/* The line is too long - skip it. :-\ */
-	do {
-	    if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
+	/* Read the shadow line into the buffer using a minimum of syscalls. */
+	if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
 		return EIO;
-	} while (!(endptr = strchr(line_buff, '\n')));
-	lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR);
-	goto restart;
-    }
+	endptr = strchr(line_buff, '\n');
+	if (endptr != NULL)
+		lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
+	else {
+		/* The line is too long - skip it. :-\ */
+		do {
+			if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
+				return EIO;
+		} while (!(endptr = strchr(line_buff, '\n')));
+		lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR);
+		goto restart;
+	}
 
-    if (__sgetspent_r(line_buff, spwd, line_buff, buflen) != 0)
-	goto restart;
+	if (__sgetspent_r(line_buff, spwd, line_buff, buflen) != 0)
+		goto restart;
 
-    return 0;
+	return 0;
 }

Index: getgrnam.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getgrnam.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- getgrnam.c	10 Sep 2002 05:53:30 -0000	1.8
+++ getgrnam.c	10 Oct 2003 07:34:26 -0000	1.9
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getgrnam.c - This file is part of the libc-8086/grp package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -18,6 +20,7 @@
  *
  */
 
+#include <features.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
@@ -27,39 +30,57 @@
 
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-extern pthread_mutex_t __getgrent_lock;
-# define LOCK   pthread_mutex_lock(&__getgrent_lock)
-# define UNLOCK pthread_mutex_unlock(&__getgrent_lock);
-#else
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK   pthread_mutex_lock(&mylock)
+# define UNLOCK pthread_mutex_unlock(&mylock);
+#else       
 # define LOCK
 # define UNLOCK
-#endif
-static char *line_buff = NULL;
-static char **members = NULL;
-
+#endif      
 
-struct group *getgrnam(const char *name)
+int getgrnam_r (const char *name, struct group *group,
+	char *buff, size_t buflen, struct group **result)
 {
-	int grp_fd;
-	struct group *group;
+	int ret;
+	int group_fd;
+
+	*result = NULL;
 
 	if (name == NULL) {
-		__set_errno(EINVAL);
-		return NULL;
+		return EINVAL;
 	}
 
-	if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
-		return NULL;
+	if ((group_fd = open(_PATH_GROUP, O_RDONLY)) < 0) {
+		return ENOENT;
+	}
 
-	LOCK;
-	while ((group = __getgrent(grp_fd, line_buff, members)) != NULL)
+	while ((ret=__getgrent_r(group, buff, buflen, group_fd)) == 0) {
 		if (!strcmp(group->gr_name, name)) {
-			close(grp_fd);
-			UNLOCK;
-			return group;
+			close(group_fd);
+			*result = group;
+			return 0;
 		}
+	}
 
-	close(grp_fd);
+	close(group_fd);
+	return ret;
+}
+
+struct group *getgrnam(const char *name)
+{
+	int ret;
+	static char line_buff[PWD_BUFFER_SIZE];
+	static struct group grp;
+	struct group *result;
+
+	LOCK;
+	if ((ret=getgrnam_r(name, &grp, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &grp;
+	}
+	__set_errno(ret);
 	UNLOCK;
 	return NULL;
 }
+
+

Index: getpw.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getpw.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- getpw.c	12 Jun 2002 23:26:59 -0000	1.4
+++ getpw.c	10 Oct 2003 07:34:26 -0000	1.5
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getpw.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -35,9 +37,9 @@
 		return -1;
 
 	if (sprintf
-		(buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd,
-		 passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, passwd->pw_dir,
-		 passwd->pw_shell) < 0) {
+			(buf, "%s:%s:%u:%u:%s:%s:%s", passwd->pw_name, passwd->pw_passwd,
+			 passwd->pw_gid, passwd->pw_uid, passwd->pw_gecos, passwd->pw_dir,
+			 passwd->pw_shell) < 0) {
 		__set_errno(ENOBUFS);
 		return -1;
 	}

Index: fgetpwent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/fgetpwent.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- fgetpwent.c	27 Jun 2003 10:43:43 -0000	1.9
+++ fgetpwent.c	10 Oct 2003 07:34:26 -0000	1.10
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * fgetpwent.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -36,29 +38,29 @@
 int fgetpwent_r (FILE *file, struct passwd *password,
 	char *buff, size_t buflen, struct passwd **result)
 {
-    int res;
-    if (file == NULL) {
-	return EINTR;
-    }
-    *result = NULL;
-    res = __getpwent_r(password, buff, buflen, fileno(file));
-    *result = password;
-    return res;
+	int res;
+	if (file == NULL) {
+		return EINTR;
+	}
+	*result = NULL;
+	res = __getpwent_r(password, buff, buflen, fileno(file));
+	*result = password;
+	return res;
 }
 
 struct passwd *fgetpwent(FILE * file)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd;
-    struct passwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &pwd;
+	}
 	UNLOCK;
-	return &pwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }

Index: lckpwdf.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/lckpwdf.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- lckpwdf.c	8 Aug 2002 07:28:33 -0000	1.3
+++ lckpwdf.c	10 Oct 2003 07:34:27 -0000	1.4
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /* Handle locking of password file.
    Copyright (C) 1996, 1998 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
@@ -49,132 +50,132 @@
 
 int lckpwdf (void)
 {
-    int flags;
-    sigset_t saved_set;         /* Saved set of caught signals.  */
-    struct sigaction saved_act; /* Saved signal action.  */
-    sigset_t new_set;           /* New set of caught signals.  */
-    struct sigaction new_act;   /* New signal action.  */
-    struct flock fl;            /* Information struct for locking.  */
-    int result;
+	int flags;
+	sigset_t saved_set;         /* Saved set of caught signals.  */
+	struct sigaction saved_act; /* Saved signal action.  */
+	sigset_t new_set;           /* New set of caught signals.  */
+	struct sigaction new_act;   /* New signal action.  */
+	struct flock fl;            /* Information struct for locking.  */
+	int result;
 
-    if (lock_fd != -1)
-	/* Still locked by own process.  */
-	return -1;
+	if (lock_fd != -1)
+		/* Still locked by own process.  */
+		return -1;
 
-    LOCK;
+	LOCK;
 
-    lock_fd = open (_PATH_PASSWD, O_WRONLY);
-    if (lock_fd == -1) {
-	/* Cannot create lock file.  */
-	UNLOCK;
-	return -1;
-    }
+	lock_fd = open (_PATH_PASSWD, O_WRONLY);
+	if (lock_fd == -1) {
+		/* Cannot create lock file.  */
+		UNLOCK;
+		return -1;
+	}
 
-    /* Make sure file gets correctly closed when process finished.  */
-    flags = fcntl (lock_fd, F_GETFD, 0);
-    if (flags == -1) {
-	/* Cannot get file flags.  */
-	close(lock_fd);
-	lock_fd = -1;
-	UNLOCK;
-	return -1;
-    }
-    flags |= FD_CLOEXEC;		/* Close on exit.  */
-    if (fcntl (lock_fd, F_SETFD, flags) < 0) {
-	/* Cannot set new flags.  */
-	close(lock_fd);
-	lock_fd = -1;
-	UNLOCK;
-	return -1;
-    }
+	/* Make sure file gets correctly closed when process finished.  */
+	flags = fcntl (lock_fd, F_GETFD, 0);
+	if (flags == -1) {
+		/* Cannot get file flags.  */
+		close(lock_fd);
+		lock_fd = -1;
+		UNLOCK;
+		return -1;
+	}
+	flags |= FD_CLOEXEC;		/* Close on exit.  */
+	if (fcntl (lock_fd, F_SETFD, flags) < 0) {
+		/* Cannot set new flags.  */
+		close(lock_fd);
+		lock_fd = -1;
+		UNLOCK;
+		return -1;
+	}
 
-    /* Now we have to get exclusive write access.  Since multiple
-       process could try this we won't stop when it first fails.
-       Instead we set a timeout for the system call.  Once the timer
-       expires it is likely that there are some problems which cannot be
-       resolved by waiting.
+	/* Now we have to get exclusive write access.  Since multiple
+	   process could try this we won't stop when it first fails.
+	   Instead we set a timeout for the system call.  Once the timer
+	   expires it is likely that there are some problems which cannot be
+	   resolved by waiting.
 
-       It is important that we don't change the signal state.  We must
-       restore the old signal behaviour.  */
-    memset (&new_act, '\0', sizeof (struct sigaction));
-    new_act.sa_handler = noop_handler;
-    sigfillset (&new_act.sa_mask);
-    new_act.sa_flags = 0ul;
+	   It is important that we don't change the signal state.  We must
+	   restore the old signal behaviour.  */
+	memset (&new_act, '\0', sizeof (struct sigaction));
+	new_act.sa_handler = noop_handler;
+	sigfillset (&new_act.sa_mask);
+	new_act.sa_flags = 0ul;
 
-    /* Install new action handler for alarm and save old.  */
-    if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
-	/* Cannot install signal handler.  */
-	close(lock_fd);
-	lock_fd = -1;
-	UNLOCK;
-	return -1;
-    }
+	/* Install new action handler for alarm and save old.  */
+	if (sigaction (SIGALRM, &new_act, &saved_act) < 0) {
+		/* Cannot install signal handler.  */
+		close(lock_fd);
+		lock_fd = -1;
+		UNLOCK;
+		return -1;
+	}
 
-    /* Now make sure the alarm signal is not blocked.  */
-    sigemptyset (&new_set);
-    sigaddset (&new_set, SIGALRM);
-    if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
-	sigaction (SIGALRM, &saved_act, NULL);
-	close(lock_fd);
-	lock_fd = -1;
-	UNLOCK;
-	return -1;
-    }
+	/* Now make sure the alarm signal is not blocked.  */
+	sigemptyset (&new_set);
+	sigaddset (&new_set, SIGALRM);
+	if (sigprocmask (SIG_UNBLOCK, &new_set, &saved_set) < 0) {
+		sigaction (SIGALRM, &saved_act, NULL);
+		close(lock_fd);
+		lock_fd = -1;
+		UNLOCK;
+		return -1;
+	}
 
-    /* Start timer.  If we cannot get the lock in the specified time we
-       get a signal.  */
-    alarm (TIMEOUT);
+	/* Start timer.  If we cannot get the lock in the specified time we
+	   get a signal.  */
+	alarm (TIMEOUT);
 
-    /* Try to get the lock.  */
-    memset (&fl, '\0', sizeof (struct flock));
-    fl.l_type = F_WRLCK;
-    fl.l_whence = SEEK_SET;
-    result = fcntl (lock_fd, F_SETLKW, &fl);
+	/* Try to get the lock.  */
+	memset (&fl, '\0', sizeof (struct flock));
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	result = fcntl (lock_fd, F_SETLKW, &fl);
 
-    /* Clear alarm.  */
-    alarm (0);
+	/* Clear alarm.  */
+	alarm (0);
 
-    /* Restore old set of handled signals.  We don't need to know
-       about the current one.*/
-    sigprocmask (SIG_SETMASK, &saved_set, NULL);
+	/* Restore old set of handled signals.  We don't need to know
+	   about the current one.*/
+	sigprocmask (SIG_SETMASK, &saved_set, NULL);
 
-    /* Restore old action handler for alarm.  We don't need to know
-       about the current one.  */
-    sigaction (SIGALRM, &saved_act, NULL);
+	/* Restore old action handler for alarm.  We don't need to know
+	   about the current one.  */
+	sigaction (SIGALRM, &saved_act, NULL);
 
-    if (result < 0) {
-	close(lock_fd);
-	lock_fd = -1;
-	UNLOCK;
-	return -1;
-    }
+	if (result < 0) {
+		close(lock_fd);
+		lock_fd = -1;
+		UNLOCK;
+		return -1;
+	}
 
-    UNLOCK;
-    return 0;
+	UNLOCK;
+	return 0;
 }
 
 
 int ulckpwdf (void)
 {
-    int result;
+	int result;
 
-    if (lock_fd == -1) {
-	/* There is no lock set.  */
-	result = -1;
-    }
-    else {
-	LOCK;
-	result = close (lock_fd);
-	/* Mark descriptor as unused.  */
-	lock_fd = -1;
-	UNLOCK;
-    }
+	if (lock_fd == -1) {
+		/* There is no lock set.  */
+		result = -1;
+	}
+	else {
+		LOCK;
+		result = close (lock_fd);
+		/* Mark descriptor as unused.  */
+		lock_fd = -1;
+		UNLOCK;
+	}
 
-    return result;
+	return result;
 }
 
 
 static void noop_handler (int sig)
 {
-    /* We simply return which makes the `fcntl' call return with an error.  */
+	/* We simply return which makes the `fcntl' call return with an error.  */
 }

--- NEW FILE: __getgrent_r.c ---
/* vi: set sw=4 ts=4: */
/*
 * __getgrent.c - This file is part of the libc-8086/grp package for ELKS,
 * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
 * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <features.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "config.h"


/*
 * This is the core group-file read function.  It behaves exactly like
 * getgrent() except that it is passed a file descriptor.  getgrent()
 * is just a wrapper for this function.
 */
int __getgrent_r (struct group *__restrict group, 
	char *__restrict line_buff, size_t buflen, int grp_fd)
{
	char *endptr, *field_begin, **members;
	int i, line_len, member_num = 0;


	if (buflen<GRP_BUFFER_SIZE) {
		return ERANGE;
	}

	/* We use the restart label to handle malformatted lines */
restart:
	/* Read the group line into the buffer for processing */
	if ((line_len = read(grp_fd, line_buff, buflen)) <= 0) {
		return EIO;
	}
	field_begin = strchr(line_buff, '\n');
	if (field_begin != NULL)
		lseek(grp_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
	else {
		/* The line is too long - skip it. :-\ */
		do {
			if ((line_len = read(grp_fd, line_buff, buflen)) <= 0) {
				return EIO;
			}
		} while (!(field_begin = strchr(line_buff, '\n')));
		lseek(grp_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
		goto restart;
	}
	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
		goto restart;
	*field_begin = '\0';

	/* We've read the line; now parse it. */
	field_begin = line_buff;
	for (i = 0; i < 3; i++) {
		switch (i) {
			case 0:
				group->gr_name = field_begin;
				break;
			case 1:
				group->gr_passwd = field_begin;
				break;
			case 2:
				group->gr_gid = (__gid_t) strtoul(field_begin, &endptr, 10);
				if (*endptr != ':')
					goto restart;
				break;
		}
		if (i < 3) {
			field_begin = strchr(field_begin, ':');
			if (field_begin == NULL)
				break;
			*field_begin++ = '\0';
		}
	}

	members = (char **) malloc(sizeof(char *));
	if (members==NULL) {
		return ENOMEM;
	}
	while(field_begin && strlen(field_begin)) {
		members[member_num++] = field_begin;
		members = (char **) realloc(members, (member_num + 1) * sizeof(char *));
		if (members==NULL) {
			return ENOMEM;
		}
		endptr = strchr(field_begin, ',');
		if (endptr == NULL) {
			/* Final entry */
			break;
		}
		*field_begin++ = '\0';
	}
	members[member_num] = NULL;
	group->gr_mem = members;
	return 0;
}

Index: __sgetspent_r.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/__sgetspent_r.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- __sgetspent_r.c	27 Jun 2003 10:19:28 -0000	1.3
+++ __sgetspent_r.c	10 Oct 2003 07:34:26 -0000	1.4
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * __sgetspent_r.c - Based on __getpwent_r.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -46,7 +48,7 @@
 	}
 
 	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
-		*line_buff == '\t')
+			*line_buff == '\t')
 		return EINVAL;
 
 	field_begin = strchr(line_buff, '\n');
@@ -57,33 +59,33 @@
 	field_begin = line_buff;
 	for (i = 0; i < 9; i++) {
 		switch (i) {
-		case 0:
-			spwd->sp_namp = field_begin;
-			break;
-		case 1:
-			spwd->sp_pwdp = field_begin;
-			break;
-		case 2:
-			lstchg_ptr = field_begin;
-			break;
-		case 3:
-			min_ptr = field_begin;
-			break;
-		case 4:
-			max_ptr = field_begin;
-			break;
-		case 5:
-			warn_ptr = field_begin;
-			break;
-		case 6:
-			inact_ptr = field_begin;
-			break;
-		case 7:
-			expire_ptr = field_begin;
-			break;
-		case 8:
-			flag_ptr = field_begin;
-			break;
+			case 0:
+				spwd->sp_namp = field_begin;
+				break;
+			case 1:
+				spwd->sp_pwdp = field_begin;
+				break;
+			case 2:
+				lstchg_ptr = field_begin;
+				break;
+			case 3:
+				min_ptr = field_begin;
+				break;
+			case 4:
+				max_ptr = field_begin;
+				break;
+			case 5:
+				warn_ptr = field_begin;
+				break;
+			case 6:
+				inact_ptr = field_begin;
+				break;
+			case 7:
+				expire_ptr = field_begin;
+				break;
+			case 8:
+				flag_ptr = field_begin;
+				break;
 		}
 		if (i < 8) {
 			field_begin = strchr(field_begin, ':');

Index: config.h
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/config.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- config.h	18 Aug 2002 18:50:51 -0000	1.6
+++ config.h	10 Oct 2003 07:34:26 -0000	1.7
@@ -28,10 +28,12 @@
 #include <shadow.h>
 
 #define PWD_BUFFER_SIZE 256
+#define GRP_BUFFER_SIZE 256
 
 
 /* These are used internally to uClibc */
-extern struct group *__getgrent(int grp_fd, char *line_buff, char **members);
+extern int __getgrent_r (struct group *__restrict group, 
+	char *__restrict line_buff, size_t buflen, int grp_fd);
 extern int __getpwent_r(struct passwd * passwd, char * line_buff, 
 	size_t buflen, int pwd_fd);
 extern int __getspent_r(struct spwd * spwd, char * line_buff, 

Index: getspuid.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getspuid.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- getspuid.c	27 Jun 2003 10:43:43 -0000	1.5
+++ getspuid.c	10 Oct 2003 07:34:27 -0000	1.6
@@ -1,5 +1,7 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getspuid.c - Based on getpwuid.c
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -37,34 +39,34 @@
 int getspuid_r (uid_t uid, struct spwd *spwd,
 	char *buff, size_t buflen, struct spwd **result)
 {
-    int ret;
-    char pwd_buff[PWD_BUFFER_SIZE];
-    struct passwd password;
+	int ret;
+	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;
+	*result = NULL;
+	ret = getpwuid_r(uid, &password, pwd_buff,  sizeof(pwd_buff), NULL);
+	if (ret != 0)
+		return ret;
 
-    ret = getspnam_r(password.pw_name, spwd, buff, buflen, result);
-    *result = spwd;
-    return ret;
+	ret = getspnam_r(password.pw_name, spwd, buff, buflen, result);
+	*result = spwd;
+	return ret;
 }
 
 struct spwd *getspuid(uid_t uid)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct spwd spwd;
-    struct spwd *result;
+	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), &result)) == 0) {
+	LOCK;
+	if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &spwd;
+	}
 	UNLOCK;
-	return &spwd;
-    }
-    UNLOCK;
-    __set_errno(ret);
-    return NULL;
+	__set_errno(ret);
+	return NULL;
 }
 

Index: grent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/grent.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- grent.c	10 Sep 2002 05:53:30 -0000	1.9
+++ grent.c	10 Oct 2003 07:34:27 -0000	1.10
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * grent.c - This file is part of the libc-8086/grp package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -28,6 +30,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <errno.h>
 #include "config.h"
 
 #ifdef __UCLIBC_HAS_THREADS__
@@ -40,50 +43,43 @@
 # define UNLOCK
 #endif      
 
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-extern pthread_mutex_t __getgrent_lock;
-# define GRENT_LOCK   pthread_mutex_lock(&__getgrent_lock)
-# define GRENT_UNLOCK pthread_mutex_unlock(&__getgrent_lock);
-#else
-# define GRENT_LOCK
-# define GRENT_UNLOCK
-#endif
-
 static int grp_fd = -1;
-static char *line_buff = NULL;
-static char **members = NULL;
 
 void setgrent(void)
 {
-    LOCK;
-    if (grp_fd != -1)
-	close(grp_fd);
-    grp_fd = open(_PATH_GROUP, O_RDONLY);
-    UNLOCK;
+	LOCK;
+	if (grp_fd != -1)
+		close(grp_fd);
+	grp_fd = open(_PATH_GROUP, O_RDONLY);
+	UNLOCK;
 }
 
 void endgrent(void)
 {
-    LOCK;
-    if (grp_fd != -1)
-	close(grp_fd);
-    grp_fd = -1;
-    UNLOCK;
+	LOCK;
+	if (grp_fd != -1)
+		close(grp_fd);
+	grp_fd = -1;
+	UNLOCK;
 }
 
 struct group *getgrent(void)
 {
-    struct group *r;
+	int ret;
+	static struct group grp;
+	static char line_buff[PWD_BUFFER_SIZE];
 
-    LOCK;
-    if (grp_fd == -1) {
+	LOCK;
+	if (grp_fd == -1) {
+		UNLOCK;
+		return NULL;
+	}
+	ret = __getgrent_r(&grp, line_buff, sizeof(line_buff), grp_fd);
+	if (ret == 0) {
+		UNLOCK;
+		return &grp;
+	}
 	UNLOCK;
+	__set_errno(ret);
 	return NULL;
-    }
-    UNLOCK;
-    GRENT_LOCK;
-    r = __getgrent(grp_fd, line_buff, members);
-    GRENT_UNLOCK;
-    return r;
 }

Index: fgetgrent.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/fgetgrent.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- fgetgrent.c	10 Sep 2002 05:53:30 -0000	1.7
+++ fgetgrent.c	10 Oct 2003 07:34:26 -0000	1.8
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * fgetgrent.c - This file is part of the libc-8086/grp package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -24,28 +26,49 @@
     
 #ifdef __UCLIBC_HAS_THREADS__
 #include <pthread.h>
-extern pthread_mutex_t __getgrent_lock;
-# define LOCK   pthread_mutex_lock(&__getgrent_lock)
-# define UNLOCK pthread_mutex_unlock(&__getgrent_lock);
+static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
+# define LOCK   pthread_mutex_lock(&mylock)
+# define UNLOCK pthread_mutex_unlock(&mylock);
 #else
 # define LOCK
 # define UNLOCK
 #endif
 
-static char *line_buff = NULL;
-static char **members = NULL;
+
+int fgetgrent_r (FILE *__restrict file, struct group *__restrict grp,
+			char *__restrict buff, size_t buflen,
+			struct group **__restrict result)
+{
+	int ret;
+	if (file == NULL) {
+		return EINTR;
+	}
+	*result = NULL;
+	flockfile(file);
+	ret = __getgrent_r(grp, buff, buflen, fileno(file));
+	funlockfile(file);
+	if (ret == 0) {
+		*result = grp;
+		return 0;
+	}
+	__set_errno(ret);
+	return ret;
+}
 
 struct group *fgetgrent(FILE * file)
 {
-    struct group *grp;
+	int ret;
+	struct group *result;
+	static struct group grp;
+	static char line_buff[PWD_BUFFER_SIZE];
 
-    if (file == NULL) {
-	__set_errno(EINTR);
+	LOCK;
+	ret=fgetgrent_r(file, &grp, line_buff, sizeof(line_buff), &result);
+	if (ret == 0) {
+		UNLOCK;
+		return result;
+	}
+	UNLOCK;
+	__set_errno(ret);
 	return NULL;
-    }
-
-    LOCK;
-    grp = __getgrent(fileno(file), line_buff, members);
-    UNLOCK;
-    return grp;
 }

--- __getgrent.c DELETED ---

Index: __getpwent_r.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/__getpwent_r.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- __getpwent_r.c	27 Jun 2003 10:19:28 -0000	1.3
+++ __getpwent_r.c	10 Oct 2003 07:34:26 -0000	1.4
@@ -1,7 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * __getpwent_r.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
- * Copyright (C) 2001 Erik Andersen <andersee at debian.org>
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -18,6 +19,7 @@
  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  * March 7, 2001 -- Reworked to be reentrant by Erik Andersen 
+ * Oct   9, 2003 -- Reworked again by Erik Andersen to be fully reentrant
  */
 
 #include <stdlib.h>
@@ -36,38 +38,35 @@
 	
 int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pwd_fd)
 {
-	char *field_begin;
-	char *endptr;
+	char *endptr, *field_begin;
 	char *gid_ptr=NULL;
 	char *uid_ptr=NULL;
-	int line_len;
-	int i;
+	int i, line_len;
 
-	if (buflen<PWD_BUFFER_SIZE)
+	if (buflen<PWD_BUFFER_SIZE) {
 		return ERANGE;
+	}
 
 	/* We use the restart label to handle malformatted lines */
 restart:
-	/* Read the passwd line into the static buffer using a minimum of
-	   syscalls. */
-	if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
+	/* Read the passwd line into the buffer for processing */
+	if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
 		return EIO;
+	}
 	field_begin = strchr(line_buff, '\n');
 	if (field_begin != NULL)
-		lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
-			  SEEK_CUR);
-	else {						/* The line is too long - skip it. :-\ */
-
+		lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)), SEEK_CUR);
+	else {
+		/* The line is too long - skip it. :-\ */
 		do {
-			if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
+			if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0) {
 				return EIO;
+			}
 		} while (!(field_begin = strchr(line_buff, '\n')));
-		lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
-			  SEEK_CUR);
+		lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1, SEEK_CUR);
 		goto restart;
 	}
-	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
-		*line_buff == '\t')
+	if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' || *line_buff == '\t')
 		goto restart;
 	*field_begin = '\0';
 
@@ -75,27 +74,27 @@
 	field_begin = line_buff;
 	for (i = 0; i < 7; i++) {
 		switch (i) {
-		case 0:
-			passwd->pw_name = field_begin;
-			break;
-		case 1:
-			passwd->pw_passwd = field_begin;
-			break;
-		case 2:
-			uid_ptr = field_begin;
-			break;
-		case 3:
-			gid_ptr = field_begin;
-			break;
-		case 4:
-			passwd->pw_gecos = field_begin;
-			break;
-		case 5:
-			passwd->pw_dir = field_begin;
-			break;
-		case 6:
-			passwd->pw_shell = field_begin;
-			break;
+			case 0:
+				passwd->pw_name = field_begin;
+				break;
+			case 1:
+				passwd->pw_passwd = field_begin;
+				break;
+			case 2:
+				uid_ptr = field_begin;
+				break;
+			case 3:
+				gid_ptr = field_begin;
+				break;
+			case 4:
+				passwd->pw_gecos = field_begin;
+				break;
+			case 5:
+				passwd->pw_dir = field_begin;
+				break;
+			case 6:
+				passwd->pw_shell = field_begin;
+				break;
 		}
 		if (i < 6) {
 			field_begin = strchr(field_begin, ':');

Index: getpwnam.c
===================================================================
RCS file: /var/cvs/uClibc/libc/pwd_grp/getpwnam.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- getpwnam.c	27 Jun 2003 10:43:43 -0000	1.10
+++ getpwnam.c	10 Oct 2003 07:34:27 -0000	1.11
@@ -1,6 +1,8 @@
+/* vi: set sw=4 ts=4: */
 /*
  * getpwnam.c - This file is part of the libc-8086/pwd package for ELKS,
  * Copyright (C) 1995, 1996 Nat Friedman <ndf at linux.mit.edu>.
+ * Copyright (C) 2001-2003 Erik Andersen <andersee at debian.org>
  * 
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Library General Public
@@ -39,46 +41,45 @@
 int getpwnam_r (const char *name, struct passwd *password,
 	char *buff, size_t buflen, struct passwd **result)
 {
-    int ret;
-    int passwd_fd;
+	int ret;
+	int passwd_fd;
 
-    *result = NULL;
+	*result = NULL;
 
-    if (name == NULL) {
-	return EINVAL;
-    }
+	if (name == NULL) {
+		return EINVAL;
+	}
 
-    if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) {
-	return ENOENT;
-    }
+	if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) {
+		return ENOENT;
+	}
 
-    while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) {
-	if (!strcmp(password->pw_name, name)) {
-	    *result=password;
-	    close(passwd_fd);
-	    *result = password;
-	    return 0;
+	while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) {
+		if (!strcmp(password->pw_name, name)) {
+			close(passwd_fd);
+			*result = password;
+			return 0;
+		}
 	}
-    }
 
-    close(passwd_fd);
-    return ret;
+	close(passwd_fd);
+	return ret;
 }
 
 struct passwd *getpwnam(const char *name)
 {
-    int ret;
-    static char line_buff[PWD_BUFFER_SIZE];
-    static struct passwd pwd;
-    struct passwd *result;
+	int ret;
+	static char line_buff[PWD_BUFFER_SIZE];
+	static struct passwd pwd;
+	struct passwd *result;
 
-    LOCK;
-    if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
+	LOCK;
+	if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
+		UNLOCK;
+		return &pwd;
+	}
+	__set_errno(ret);
 	UNLOCK;
-	return &pwd;
-    }
-    __set_errno(ret);
-    UNLOCK;
-    return NULL;
+	return NULL;
 }
 




More information about the uClibc-cvs mailing list