OSDN Git Service

Implement getgrent_r. Rework getpwent and getgrent a bit further
authorEric Andersen <andersen@codepoet.org>
Sun, 2 Nov 2003 21:35:27 +0000 (21:35 -0000)
committerEric Andersen <andersen@codepoet.org>
Sun, 2 Nov 2003 21:35:27 +0000 (21:35 -0000)
libc/pwd_grp/grent.c
libc/pwd_grp/pwent.c

index e05459c..b2d6f8c 100644 (file)
@@ -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;
 }
index e983a73..557bffd 100644 (file)
@@ -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;
 }