From c3d20eba96e9c26f775f3abf5ca6edcbd8c23963 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Fri, 2 Jul 1999 18:03:43 +0000 Subject: Reentrant versions added by Joel. Signficant formatting cleanup. --- c/src/exec/libcsupport/src/getgrent.c | 136 +++++++++++++++++------------ c/src/exec/libcsupport/src/getpwent.c | 155 ++++++++++++++++++++-------------- c/src/lib/libc/Makefile.in | 2 +- c/src/lib/libc/getgrent.c | 136 +++++++++++++++++------------ c/src/lib/libc/getpwent.c | 155 ++++++++++++++++++++-------------- 5 files changed, 353 insertions(+), 231 deletions(-) (limited to 'c') diff --git a/c/src/exec/libcsupport/src/getgrent.c b/c/src/exec/libcsupport/src/getgrent.c index 080187d178..d0a92a0b80 100644 --- a/c/src/exec/libcsupport/src/getgrent.c +++ b/c/src/exec/libcsupport/src/getgrent.c @@ -11,7 +11,7 @@ #include #include -static struct group gr_group; /* password structure */ +static struct group gr_group; /* password structure */ static FILE *group_fp; static char groupname[8]; @@ -19,70 +19,102 @@ static char password[1024]; static char groups[1024]; static char *gr_mem[16] = { } ; -struct group * -getgrnam (name) - const char *name; +int getgrnam_r( + const char *name, + struct group *grp, + char *buffer, + size_t bufsize, + struct group **result +) { FILE *fp; - char buf[1024]; - - if ((fp = fopen ("/etc/group", "r")) == NULL) - { - return NULL; - } - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); - gr_group.gr_name = groupname; - gr_group.gr_passwd = password; - gr_group.gr_mem = gr_mem ; - - if (!strcmp (groupname, name)) - { - fclose (fp); - return &gr_group; - } + if ((fp = fopen ("/etc/group", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%s\n", + groupname, password, &grp->gr_gid, + groups); + grp->gr_name = groupname; + grp->gr_passwd = password; + grp->gr_mem = gr_mem ; + + if (!strcmp (groupname, name)) { + fclose (fp); + *result = grp; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct group *getgrnam( + const char *name +) +{ + char buf[1024]; + struct group *g; + + if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) ) + return NULL; + + return g; } -struct group * -getgrgid (gid_t gid) +int getgrgid_r( + gid_t gid, + struct group *grp, + char *buffer, + size_t bufsize, + struct group **result +) { FILE *fp; - char buf[1024]; - if ((fp = fopen ("/etc/group", "r")) == NULL) - { - return NULL; - } + if ((fp = fopen ("/etc/group", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%s\n", + groupname, password, &gr_group.gr_gid, + groups); + gr_group.gr_name = groupname; + gr_group.gr_passwd = password; + gr_group.gr_mem = gr_mem ; + - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); - gr_group.gr_name = groupname; - gr_group.gr_passwd = password; - gr_group.gr_mem = gr_mem ; - - - if (gid == gr_group.gr_gid) - { - fclose (fp); - return &gr_group; - } + if (gid == gr_group.gr_gid) { + fclose (fp); + *result = grp; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct group *getgrgid ( + gid_t gid +) +{ + char buf[1024]; + struct group *g; + + if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) ) + return NULL; + + return g; } -struct group * -getgrent () +struct group *getgrent( void ) { char buf[1024]; @@ -93,8 +125,8 @@ getgrent () return NULL; sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); + groupname, password, &gr_group.gr_gid, + groups); gr_group.gr_name = groupname; gr_group.gr_passwd = password; gr_group.gr_mem = gr_mem ; diff --git a/c/src/exec/libcsupport/src/getpwent.c b/c/src/exec/libcsupport/src/getpwent.c index 23a35f49bd..daa5d226f1 100644 --- a/c/src/exec/libcsupport/src/getpwent.c +++ b/c/src/exec/libcsupport/src/getpwent.c @@ -7,7 +7,7 @@ #include #include -static struct passwd pw_passwd; /* password structure */ +static struct passwd pw_passwd; /* password structure */ static FILE *passwd_fp; static char logname[8]; @@ -17,78 +17,109 @@ static char gecos[1024]; static char dir[1024]; static char shell[1024]; -struct passwd * -getpwnam (name) - const char *name; +int getpwnam_r( + const char *name, + struct passwd *pwd, + char *buffer, + size_t bufsize, + struct passwd **result +) { FILE *fp; - int uid, gid; - char buf[1024]; - - if ((fp = fopen ("/etc/passwd", "r")) == NULL) - { - return NULL; - } - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", - logname, password, &pw_passwd.pw_uid, - &pw_passwd.pw_gid, comment, gecos, - dir, shell); - pw_passwd.pw_name = logname; - pw_passwd.pw_passwd = password; - pw_passwd.pw_comment = comment; - pw_passwd.pw_gecos = gecos; - pw_passwd.pw_dir = dir; - pw_passwd.pw_shell = shell; - - if (!strcmp (logname, name)) - { - fclose (fp); - return &pw_passwd; - } + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", + logname, password, &pwd->pw_uid, + &pwd->pw_gid, comment, gecos, + dir, shell); + pwd->pw_name = logname; + pwd->pw_passwd = password; + pwd->pw_comment = comment; + pwd->pw_gecos = gecos; + pwd->pw_dir = dir; + pwd->pw_shell = shell; + + if (!strcmp (logname, name)) { + fclose (fp); + *result = pwd; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; } -struct passwd * -getpwuid (uid_t uid) +struct passwd *getpwnam( + const char *name +) { - FILE *fp; - char buf[1024]; + char buf[1024]; + struct passwd *p; - if ((fp = fopen ("/etc/passwd", "r")) == NULL) - { - return NULL; - } + if ( getpwnam_r( name, &pw_passwd, buf, 1024, &p ) ) + return NULL; + + return p; +} - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", - logname, password, &pw_passwd.pw_uid, - &pw_passwd.pw_gid, comment, gecos, - dir, shell); - pw_passwd.pw_name = logname; - pw_passwd.pw_passwd = password; - pw_passwd.pw_comment = comment; - pw_passwd.pw_gecos = gecos; - pw_passwd.pw_dir = dir; - pw_passwd.pw_shell = shell; - - if (uid == pw_passwd.pw_uid) - { - fclose (fp); - return &pw_passwd; - } +int getpwuid_r( + uid_t uid, + struct passwd *pwd, + char *buffer, + size_t bufsize, + struct passwd **result +) +{ + FILE *fp; + + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", + logname, password, &pw_passwd.pw_uid, + &pw_passwd.pw_gid, comment, gecos, + dir, shell); + pwd->pw_name = logname; + pwd->pw_passwd = password; + pwd->pw_comment = comment; + pwd->pw_gecos = gecos; + pwd->pw_dir = dir; + pwd->pw_shell = shell; + + if (uid == pwd->pw_uid) { + fclose (fp); + *result = pwd; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct passwd *getpwuid( + uid_t uid +) +{ + char buf[1024]; + struct passwd *p; + + if ( getpwuid_r( uid, &pw_passwd, buf, 1024, &p ) ) + return NULL; + + return p; } -struct passwd * -getpwent () +struct passwd *getpwent() { char buf[1024]; @@ -112,8 +143,7 @@ getpwent () return &pw_passwd; } -void -setpwent () +void setpwent( void ) { if (passwd_fp != NULL) fclose (passwd_fp); @@ -121,8 +151,7 @@ setpwent () passwd_fp = fopen ("/etc/passwd", "r"); } -void -endpwent () +void endpwent( void ) { if (passwd_fp != NULL) fclose (passwd_fp); diff --git a/c/src/lib/libc/Makefile.in b/c/src/lib/libc/Makefile.in index 3b5dc10273..31c36ecf4d 100644 --- a/c/src/lib/libc/Makefile.in +++ b/c/src/lib/libc/Makefile.in @@ -47,7 +47,7 @@ MALLOC_PIECES=\ malloc __brk __sbrk PASSWORD_GROUP_PIECES=\ - getpwent getgwent + getpwent getgrent LIBC_GLUE_PIECES=\ __gettod __times \ diff --git a/c/src/lib/libc/getgrent.c b/c/src/lib/libc/getgrent.c index 080187d178..d0a92a0b80 100644 --- a/c/src/lib/libc/getgrent.c +++ b/c/src/lib/libc/getgrent.c @@ -11,7 +11,7 @@ #include #include -static struct group gr_group; /* password structure */ +static struct group gr_group; /* password structure */ static FILE *group_fp; static char groupname[8]; @@ -19,70 +19,102 @@ static char password[1024]; static char groups[1024]; static char *gr_mem[16] = { } ; -struct group * -getgrnam (name) - const char *name; +int getgrnam_r( + const char *name, + struct group *grp, + char *buffer, + size_t bufsize, + struct group **result +) { FILE *fp; - char buf[1024]; - - if ((fp = fopen ("/etc/group", "r")) == NULL) - { - return NULL; - } - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); - gr_group.gr_name = groupname; - gr_group.gr_passwd = password; - gr_group.gr_mem = gr_mem ; - - if (!strcmp (groupname, name)) - { - fclose (fp); - return &gr_group; - } + if ((fp = fopen ("/etc/group", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%s\n", + groupname, password, &grp->gr_gid, + groups); + grp->gr_name = groupname; + grp->gr_passwd = password; + grp->gr_mem = gr_mem ; + + if (!strcmp (groupname, name)) { + fclose (fp); + *result = grp; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct group *getgrnam( + const char *name +) +{ + char buf[1024]; + struct group *g; + + if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) ) + return NULL; + + return g; } -struct group * -getgrgid (gid_t gid) +int getgrgid_r( + gid_t gid, + struct group *grp, + char *buffer, + size_t bufsize, + struct group **result +) { FILE *fp; - char buf[1024]; - if ((fp = fopen ("/etc/group", "r")) == NULL) - { - return NULL; - } + if ((fp = fopen ("/etc/group", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%s\n", + groupname, password, &gr_group.gr_gid, + groups); + gr_group.gr_name = groupname; + gr_group.gr_passwd = password; + gr_group.gr_mem = gr_mem ; + - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); - gr_group.gr_name = groupname; - gr_group.gr_passwd = password; - gr_group.gr_mem = gr_mem ; - - - if (gid == gr_group.gr_gid) - { - fclose (fp); - return &gr_group; - } + if (gid == gr_group.gr_gid) { + fclose (fp); + *result = grp; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct group *getgrgid ( + gid_t gid +) +{ + char buf[1024]; + struct group *g; + + if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) ) + return NULL; + + return g; } -struct group * -getgrent () +struct group *getgrent( void ) { char buf[1024]; @@ -93,8 +125,8 @@ getgrent () return NULL; sscanf (buf, "%[^:]:%[^:]:%d:%s\n", - groupname, password, &gr_group.gr_gid, - groups); + groupname, password, &gr_group.gr_gid, + groups); gr_group.gr_name = groupname; gr_group.gr_passwd = password; gr_group.gr_mem = gr_mem ; diff --git a/c/src/lib/libc/getpwent.c b/c/src/lib/libc/getpwent.c index 23a35f49bd..daa5d226f1 100644 --- a/c/src/lib/libc/getpwent.c +++ b/c/src/lib/libc/getpwent.c @@ -7,7 +7,7 @@ #include #include -static struct passwd pw_passwd; /* password structure */ +static struct passwd pw_passwd; /* password structure */ static FILE *passwd_fp; static char logname[8]; @@ -17,78 +17,109 @@ static char gecos[1024]; static char dir[1024]; static char shell[1024]; -struct passwd * -getpwnam (name) - const char *name; +int getpwnam_r( + const char *name, + struct passwd *pwd, + char *buffer, + size_t bufsize, + struct passwd **result +) { FILE *fp; - int uid, gid; - char buf[1024]; - - if ((fp = fopen ("/etc/passwd", "r")) == NULL) - { - return NULL; - } - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", - logname, password, &pw_passwd.pw_uid, - &pw_passwd.pw_gid, comment, gecos, - dir, shell); - pw_passwd.pw_name = logname; - pw_passwd.pw_passwd = password; - pw_passwd.pw_comment = comment; - pw_passwd.pw_gecos = gecos; - pw_passwd.pw_dir = dir; - pw_passwd.pw_shell = shell; - - if (!strcmp (logname, name)) - { - fclose (fp); - return &pw_passwd; - } + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", + logname, password, &pwd->pw_uid, + &pwd->pw_gid, comment, gecos, + dir, shell); + pwd->pw_name = logname; + pwd->pw_passwd = password; + pwd->pw_comment = comment; + pwd->pw_gecos = gecos; + pwd->pw_dir = dir; + pwd->pw_shell = shell; + + if (!strcmp (logname, name)) { + fclose (fp); + *result = pwd; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; } -struct passwd * -getpwuid (uid_t uid) +struct passwd *getpwnam( + const char *name +) { - FILE *fp; - char buf[1024]; + char buf[1024]; + struct passwd *p; - if ((fp = fopen ("/etc/passwd", "r")) == NULL) - { - return NULL; - } + if ( getpwnam_r( name, &pw_passwd, buf, 1024, &p ) ) + return NULL; + + return p; +} - while (fgets (buf, sizeof (buf), fp)) - { - sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", - logname, password, &pw_passwd.pw_uid, - &pw_passwd.pw_gid, comment, gecos, - dir, shell); - pw_passwd.pw_name = logname; - pw_passwd.pw_passwd = password; - pw_passwd.pw_comment = comment; - pw_passwd.pw_gecos = gecos; - pw_passwd.pw_dir = dir; - pw_passwd.pw_shell = shell; - - if (uid == pw_passwd.pw_uid) - { - fclose (fp); - return &pw_passwd; - } +int getpwuid_r( + uid_t uid, + struct passwd *pwd, + char *buffer, + size_t bufsize, + struct passwd **result +) +{ + FILE *fp; + + if ((fp = fopen ("/etc/passwd", "r")) == NULL) { + errno = EINVAL; + return -1; + } + + while (fgets (buffer, bufsize, fp)) { + sscanf (buffer, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n", + logname, password, &pw_passwd.pw_uid, + &pw_passwd.pw_gid, comment, gecos, + dir, shell); + pwd->pw_name = logname; + pwd->pw_passwd = password; + pwd->pw_comment = comment; + pwd->pw_gecos = gecos; + pwd->pw_dir = dir; + pwd->pw_shell = shell; + + if (uid == pwd->pw_uid) { + fclose (fp); + *result = pwd; + return 0; } + } fclose (fp); - return NULL; + errno = EINVAL; + return -1; +} + +struct passwd *getpwuid( + uid_t uid +) +{ + char buf[1024]; + struct passwd *p; + + if ( getpwuid_r( uid, &pw_passwd, buf, 1024, &p ) ) + return NULL; + + return p; } -struct passwd * -getpwent () +struct passwd *getpwent() { char buf[1024]; @@ -112,8 +143,7 @@ getpwent () return &pw_passwd; } -void -setpwent () +void setpwent( void ) { if (passwd_fp != NULL) fclose (passwd_fp); @@ -121,8 +151,7 @@ setpwent () passwd_fp = fopen ("/etc/passwd", "r"); } -void -endpwent () +void endpwent( void ) { if (passwd_fp != NULL) fclose (passwd_fp); -- cgit v1.2.3