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/pwd_grp/fgetgrent.c | |
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/pwd_grp/fgetgrent.c')
-rw-r--r-- | libc/pwd_grp/fgetgrent.c | 51 |
1 files changed, 37 insertions, 14 deletions
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; } |