diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pwd/getpwnam.c | 87 | ||||
| -rw-r--r-- | src/pwd/getpwuid.c | 87 |
2 files changed, 17 insertions, 157 deletions
diff --git a/src/pwd/getpwnam.c b/src/pwd/getpwnam.c index 6760b6f8..b2e059c4 100644 --- a/src/pwd/getpwnam.c +++ b/src/pwd/getpwnam.c @@ -4,90 +4,21 @@ #include "stdio.h" #include "limits.h" #include "string.h" -#include "_config.h" - -#ifndef LINE_MAX -#define LINE_MAX _POSIX2_LINE_MAX -#endif +#include "_pwd.h" struct passwd * getpwnam(const char * name) { - static char buf[LINE_MAX + 1]; - static struct passwd pwd = { 0 }; - char *user, *password, *uid, *gid, *gecos, *home, *shell, *nl; - - /* TODO: attempt calling first _PWD_CMD */ - - FILE *db = fopen(_PWD_DB, "r"); - if (db == NULL) { - return NULL; - } - - while (fgets(buf, sizeof(buf), db) != NULL) { - user = buf; - if ((password = strchr(buf, ':')) != NULL) { - *password = '\0'; - password++; - } else { - continue; - } + struct passwd *pwd = NULL; + setpwent(); - if (strcmp(user, name) != 0) { - continue; + while ((pwd = getpwent()) != NULL) { + if (strcmp(name, pwd->pw_name) == 0) { + endpwent(); + return pwd; } - - if ((uid = strchr(password, ':')) != NULL) { - *uid = '\0'; - uid++; - } else { - continue; - } - - if ((gid = strchr(uid, ':')) != NULL) { - *gid = '\0'; - gid++; - } else { - continue; - } - - if ((gecos = strchr(gid, ':')) != NULL) { - *gecos = '\0'; - gecos++; - } else { - continue; - } - - if ((home = strchr(gecos, ':')) != NULL) { - *home = '\0'; - home++; - } else { - continue; - } - - if ((shell = strchr(home, ':')) != NULL) { - *shell = '\0'; - shell++; - if ((nl = strchr(shell, '\n')) != NULL) { - *nl = '\0'; - } - } else { - continue; - } - - pwd.pw_name = user; - pwd.pw_uid = strtoul(uid, NULL, 10); - pwd.pw_gid = strtoul(gid, NULL, 10); - pwd.pw_dir = home; - pwd.pw_shell = shell; - break; } - - fclose(db); - - if (strcmp(name, pwd.pw_name) == 0) { - return &pwd; - } - + + endpwent(); return NULL; } diff --git a/src/pwd/getpwuid.c b/src/pwd/getpwuid.c index fd9ce1a2..fe97bea4 100644 --- a/src/pwd/getpwuid.c +++ b/src/pwd/getpwuid.c @@ -1,91 +1,20 @@ #include "sys/types.h" #include <pwd.h> -#include "stddef.h" -#include "stdio.h" -#include "limits.h" -#include "string.h" -#include "_config.h" - -#ifndef LINE_MAX -#define LINE_MAX _POSIX2_LINE_MAX -#endif +#include "_pwd.h" struct passwd * getpwuid(uid_t uid) { - static char buf[LINE_MAX + 1]; - static struct passwd pwd = { 0 }; - char *user, *password, *pwuid, *gid, *gecos, *home, *shell, *nl; - - /* TODO: attempt calling first _PWD_CMD */ - - FILE *db = fopen(_PWD_DB, "r"); - if (db == NULL) { - return NULL; - } - - while (fgets(buf, sizeof(buf), db) != NULL) { - user = buf; - if ((password = strchr(buf, ':')) != NULL) { - *password = '\0'; - password++; - } else { - continue; - } - - if ((pwuid = strchr(password, ':')) != NULL) { - *pwuid = '\0'; - pwuid++; - } else { - continue; - } - - if ((gid = strchr(pwuid, ':')) != NULL) { - *gid = '\0'; - gid++; - } else { - continue; - } + struct passwd *pwd = NULL; + setpwent(); - if ((gecos = strchr(gid, ':')) != NULL) { - *gecos = '\0'; - gecos++; - } else { - continue; + while ((pwd = getpwent()) != NULL) { + if (pwd->pw_uid == uid) { + endpwent(); + return pwd; } - - if ((home = strchr(gecos, ':')) != NULL) { - *home = '\0'; - home++; - } else { - continue; - } - - if ((shell = strchr(home, ':')) != NULL) { - *shell = '\0'; - shell++; - if ((nl = strchr(shell, '\n')) != NULL) { - *nl = '\0'; - } - } else { - continue; - } - - pwd.pw_name = user; - pwd.pw_uid = strtoul(pwuid, NULL, 10); - pwd.pw_gid = strtoul(gid, NULL, 10); - pwd.pw_dir = home; - pwd.pw_shell = shell; - if (pwd.pw_uid == uid) { - break; - } - } - - fclose(db); - - if (pwd.pw_uid == uid) { - return &pwd; } + endpwent(); return NULL; } |
