From ded0e9aba41f65618cc4b329cdad753f01f9875a Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sun, 2 Nov 2003 21:35:27 +0000 Subject: Implement getgrent_r. Rework getpwent and getgrent a bit further --- libc/pwd_grp/grent.c | 32 ++++++++++++++++++++++++-------- libc/pwd_grp/pwent.c | 30 ++++++++++++++---------------- 2 files changed, 38 insertions(+), 24 deletions(-) (limited to 'libc') diff --git a/libc/pwd_grp/grent.c b/libc/pwd_grp/grent.c index e05459c2d..b2d6f8c6a 100644 --- a/libc/pwd_grp/grent.c +++ b/libc/pwd_grp/grent.c @@ -61,15 +61,15 @@ void endgrent(void) LOCK; if (grp_fd > -1) close(grp_fd); - grp_fd = -1; + grp_fd = -9; UNLOCK; } -struct group *getgrent(void) +int getgrent_r (struct group *grp, char *buff, + size_t buflen, struct group **result) { - int ret; - static struct group grp; - static char line_buff[PWD_BUFFER_SIZE]; + int ret=EINVAL; + *result = NULL; LOCK; /* Open /etc/group if it has never been opened */ @@ -78,14 +78,30 @@ struct group *getgrent(void) } if (grp_fd == -1) { UNLOCK; - return NULL; + return -1; } - ret = __getgrent_r(&grp, line_buff, sizeof(line_buff), grp_fd); + ret=__getgrent_r(grp, buff, buflen, grp_fd); if (ret == 0) { UNLOCK; - return &grp; + *result = grp; + return 0; } UNLOCK; __set_errno(ret); + return ret; +} + +struct group *getgrent(void) +{ + int ret; + struct group *result; + static struct group grp; + static char line_buff[PWD_BUFFER_SIZE]; + + ret = getgrent_r(&grp, line_buff, sizeof(line_buff), &result); + if (ret == 0) { + return &grp; + } + __set_errno(ret); return NULL; } diff --git a/libc/pwd_grp/pwent.c b/libc/pwd_grp/pwent.c index e983a73a0..557bffd65 100644 --- a/libc/pwd_grp/pwent.c +++ b/libc/pwd_grp/pwent.c @@ -62,18 +62,27 @@ void endpwent(void) LOCK; if (pw_fd > -1) close(pw_fd); - pw_fd = -1; + pw_fd = -9; UNLOCK; } int getpwent_r (struct passwd *password, char *buff, - size_t buflen, struct passwd **result) + size_t buflen, struct passwd **result) { int ret=EINVAL; - LOCK; *result = NULL; - if ((ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) { + LOCK; + /* Open /etc/passwd if not yet opened */ + if (pw_fd == -9) { + setpwent(); + } + if (pw_fd == -1) { + UNLOCK; + return -1; + } + ret=__getpwent_r(password, buff, buflen, pw_fd); + if (ret == 0) { UNLOCK; *result = password; return 0; @@ -90,21 +99,10 @@ struct passwd *getpwent(void) static struct passwd pwd; static char line_buff[PWD_BUFFER_SIZE]; - LOCK; - /* Open /etc/passwd if not yet opened */ - if (pw_fd == -9) { - setpwent(); - } - if (pw_fd == -1) { - UNLOCK; - return NULL; - } - ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); + ret = getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); if (ret == 0) { - UNLOCK; return &pwd; } - UNLOCK; __set_errno(ret); return NULL; } -- cgit v1.2.3