diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-14 10:58:28 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-14 10:58:28 -0400 |
commit | 76d858fe4799e765a4eeef0b4a0bbd193249ff98 (patch) | |
tree | a07da2fd03b6c5b91756152d351db4f97b8c8dd7 /src/pwd | |
parent | 0d6f9fc053f693523667b49cc2152305e48ee98e (diff) |
implement
Diffstat (limited to 'src/pwd')
-rw-r--r-- | src/pwd/getpwnam.c | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/src/pwd/getpwnam.c b/src/pwd/getpwnam.c index 5a255caf..f5b1b979 100644 --- a/src/pwd/getpwnam.c +++ b/src/pwd/getpwnam.c @@ -1,10 +1,93 @@ #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 struct passwd * getpwnam(const char * name) { - (void)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; + } + + if (strcmp(user, name) != 0) { + continue; + } + + 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(gid, 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; + } + return NULL; } |