diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-10-10 07:34:27 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-10-10 07:34:27 +0000 |
commit | 56104838733c81ba410ac41f767ac72267049489 (patch) | |
tree | 54057d78cdae893235b86787a735629396d857a5 /libc | |
parent | 61011662018fa98c4610c1ae826e417678cd5c80 (diff) |
Implement getgrgid_r and getgrnam_r. Rework group handling code to be fully
reentrant, since there was still a shared static value. indent stuff,
Diffstat (limited to 'libc')
-rw-r--r-- | libc/pwd_grp/Makefile | 4 | ||||
-rw-r--r-- | libc/pwd_grp/__getgrent.c | 121 | ||||
-rw-r--r-- | libc/pwd_grp/__getgrent_r.c | 114 | ||||
-rw-r--r-- | libc/pwd_grp/__getpwent_r.c | 77 | ||||
-rw-r--r-- | libc/pwd_grp/__getspent_r.c | 46 | ||||
-rw-r--r-- | libc/pwd_grp/__sgetspent_r.c | 58 | ||||
-rw-r--r-- | libc/pwd_grp/config.h | 4 | ||||
-rw-r--r-- | libc/pwd_grp/fgetgrent.c | 51 | ||||
-rw-r--r-- | libc/pwd_grp/fgetpwent.c | 40 | ||||
-rw-r--r-- | libc/pwd_grp/fgetspent.c | 40 | ||||
-rw-r--r-- | libc/pwd_grp/getgrgid.c | 60 | ||||
-rw-r--r-- | libc/pwd_grp/getgrnam.c | 63 | ||||
-rw-r--r-- | libc/pwd_grp/getpw.c | 8 | ||||
-rw-r--r-- | libc/pwd_grp/getpwnam.c | 59 | ||||
-rw-r--r-- | libc/pwd_grp/getpwuid.c | 49 | ||||
-rw-r--r-- | libc/pwd_grp/getspnam.c | 54 | ||||
-rw-r--r-- | libc/pwd_grp/getspuid.c | 44 | ||||
-rw-r--r-- | libc/pwd_grp/grent.c | 58 | ||||
-rw-r--r-- | libc/pwd_grp/initgroups.c | 67 | ||||
-rw-r--r-- | libc/pwd_grp/lckpwdf.c | 227 | ||||
-rw-r--r-- | libc/pwd_grp/putpwent.c | 8 | ||||
-rw-r--r-- | libc/pwd_grp/putspent.c | 1 | ||||
-rw-r--r-- | libc/pwd_grp/pwent.c | 60 | ||||
-rw-r--r-- | libc/pwd_grp/sgetspent.c | 34 | ||||
-rw-r--r-- | libc/pwd_grp/spent.c | 62 |
25 files changed, 738 insertions, 671 deletions
diff --git a/libc/pwd_grp/Makefile b/libc/pwd_grp/Makefile index c34142d6b..06ba857f0 100644 --- a/libc/pwd_grp/Makefile +++ b/libc/pwd_grp/Makefile @@ -25,7 +25,7 @@ TOPDIR=../../ 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 @@ $(COBJS): %.o : %.c $(OBJ): Makefile -__getgrent.c: config.h +__getgrent_r.c: config.h initgroups.c: config.h clean: diff --git a/libc/pwd_grp/__getgrent.c b/libc/pwd_grp/__getgrent.c deleted file mode 100644 index ad0b8beb5..000000000 --- a/libc/pwd_grp/__getgrent.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * __getgrent.c - This file is part of the libc-8086/grp package for ELKS, - * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>. - * - * 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 "config.h" - - -#ifdef __UCLIBC_HAS_THREADS__ -#include <pthread.h> -/* This function should always be called under lock, so we - * do not lock things in here... */ -pthread_mutex_t __getgrent_lock = PTHREAD_MUTEX_INITIALIZER; -#endif - -/* - * 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. - */ -struct group *__getgrent(int grp_fd, char *line_buff, char **members) -{ - short line_index; - short buff_size; - static struct group group; - register char *ptr; - char *field_begin; - short member_num; - char *endptr; - int line_len; - - - /* We use the restart label to handle malformatted lines */ -restart: - line_index = 0; - buff_size = 256; - - line_buff = realloc(line_buff, buff_size); - while (1) { - if ((line_len = read(grp_fd, line_buff + line_index, - buff_size - line_index)) <= 0) { - return NULL; - } - field_begin = strchr(line_buff, '\n'); - if (field_begin != NULL) { - lseek(grp_fd, - (long) (1 + field_begin - - (line_len + line_index + line_buff)), SEEK_CUR); - *field_begin = '\0'; - if (*line_buff == '#' || *line_buff == ' ' - || *line_buff == '\n' || *line_buff == '\t') - goto restart; - break; - } else { /* Allocate some more space */ - - line_index = buff_size; - buff_size += 256; - line_buff = realloc(line_buff, buff_size); - } - } - - /* Now parse the line */ - group.gr_name = line_buff; - ptr = strchr(line_buff, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; - - group.gr_passwd = ptr; - ptr = strchr(ptr, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; - - field_begin = ptr; - ptr = strchr(ptr, ':'); - if (ptr == NULL) - goto restart; - *ptr++ = '\0'; - - group.gr_gid = (__gid_t) strtoul(field_begin, &endptr, 10); - if (*endptr != '\0') - goto restart; - - member_num = 0; - field_begin = ptr; - - if (members != NULL) - free(members); - members = (char **) malloc((member_num + 1) * sizeof(char *)); - for ( ; field_begin && *field_begin != '\0'; field_begin = ptr) { - if ((ptr = strchr(field_begin, ',')) != NULL) - *ptr++ = '\0'; - members[member_num++] = field_begin; - members = (char **) realloc(members, - (member_num + 1) * sizeof(char *)); - } - members[member_num] = NULL; - - group.gr_mem = members; - return &group; -} diff --git a/libc/pwd_grp/__getgrent_r.c b/libc/pwd_grp/__getgrent_r.c new file mode 100644 index 000000000..69559bdb1 --- /dev/null +++ b/libc/pwd_grp/__getgrent_r.c @@ -0,0 +1,114 @@ +/* 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@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@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; +} diff --git a/libc/pwd_grp/__getpwent_r.c b/libc/pwd_grp/__getpwent_r.c index 5cfdd89ba..6e6de4038 100644 --- a/libc/pwd_grp/__getpwent_r.c +++ b/libc/pwd_grp/__getpwent_r.c @@ -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@linux.mit.edu>. - * Copyright (C) 2001 Erik Andersen <andersee@debian.org> + * Copyright (C) 2001-2003 Erik Andersen <andersee@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 @@ restart: 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, ':'); diff --git a/libc/pwd_grp/__getspent_r.c b/libc/pwd_grp/__getspent_r.c index 73e40f031..8ac7d40ea 100644 --- a/libc/pwd_grp/__getspent_r.c +++ b/libc/pwd_grp/__getspent_r.c @@ -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@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; } diff --git a/libc/pwd_grp/__sgetspent_r.c b/libc/pwd_grp/__sgetspent_r.c index d11cfe62e..fbd40fc62 100644 --- a/libc/pwd_grp/__sgetspent_r.c +++ b/libc/pwd_grp/__sgetspent_r.c @@ -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@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 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz } 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 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz 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, ':'); diff --git a/libc/pwd_grp/config.h b/libc/pwd_grp/config.h index 25f450d6c..fb678876b 100644 --- a/libc/pwd_grp/config.h +++ b/libc/pwd_grp/config.h @@ -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, diff --git a/libc/pwd_grp/fgetgrent.c b/libc/pwd_grp/fgetgrent.c index 2c7917601..d1a6514bd 100644 --- a/libc/pwd_grp/fgetgrent.c +++ b/libc/pwd_grp/fgetgrent.c @@ -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@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@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; } diff --git a/libc/pwd_grp/fgetpwent.c b/libc/pwd_grp/fgetpwent.c index 174629fcb..14ac73698 100644 --- a/libc/pwd_grp/fgetpwent.c +++ b/libc/pwd_grp/fgetpwent.c @@ -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@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@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 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; 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; } diff --git a/libc/pwd_grp/fgetspent.c b/libc/pwd_grp/fgetspent.c index 2ecf3a174..497db40cc 100644 --- a/libc/pwd_grp/fgetspent.c +++ b/libc/pwd_grp/fgetspent.c @@ -1,5 +1,7 @@ +/* vi: set sw=4 ts=4: */ /* * fgetspent.c - Based on fgetpwent.c + * Copyright (C) 2001-2003 Erik Andersen <andersee@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 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; 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; } diff --git a/libc/pwd_grp/getgrgid.c b/libc/pwd_grp/getgrgid.c index 3575a749b..b8f2a29f5 100644 --- a/libc/pwd_grp/getgrgid.c +++ b/libc/pwd_grp/getgrgid.c @@ -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@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@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; + int grp_fd; + if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) + return errno; - if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0) - return NULL; - - 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 + diff --git a/libc/pwd_grp/getgrnam.c b/libc/pwd_grp/getgrnam.c index 2c8d030e0..27d714407 100644 --- a/libc/pwd_grp/getgrnam.c +++ b/libc/pwd_grp/getgrnam.c @@ -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@linux.mit.edu>. + * Copyright (C) 2001-2003 Erik Andersen <andersee@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)) == |