diff options
-rw-r--r-- | src/pwd/endpwent.c | 6 | ||||
-rw-r--r-- | src/pwd/getpwent.c | 85 | ||||
-rw-r--r-- | src/pwd/setpwent.c | 5 |
3 files changed, 95 insertions, 1 deletions
diff --git a/src/pwd/endpwent.c b/src/pwd/endpwent.c index 73f2139c..df6af455 100644 --- a/src/pwd/endpwent.c +++ b/src/pwd/endpwent.c @@ -1,7 +1,13 @@ #include <pwd.h> +#include "_pwd.h" +#include "stdio.h" void endpwent(void) { + if (__pwd.db != NULL) { + fclose(__pwd.db); + __pwd.db = NULL; + } } /* diff --git a/src/pwd/getpwent.c b/src/pwd/getpwent.c index ed47bda1..6022d54d 100644 --- a/src/pwd/getpwent.c +++ b/src/pwd/getpwent.c @@ -1,9 +1,92 @@ +#include "sys/types.h" #include <pwd.h> #include "stddef.h" +#include "stdio.h" +#include "limits.h" +#include "string.h" +#include "_config.h" +#include "_pwd.h" + +#ifndef LINE_MAX +#define LINE_MAX _POSIX2_LINE_MAX +#endif + +#ifndef _XOPEN_SOURCE + static +#endif struct passwd * getpwent(void) { - return NULL; + static char buf[LINE_MAX + 1]; + char *user, *password, *uid, *gid, *gecos, *home, *shell, *nl; + + /* TODO: attempt first calling _PWD_CMD */ + + if (__pwd.db == NULL) { + __pwd.db = fopen(_PWD_DB, "r"); + if (__pwd.db == NULL) { + return NULL; + } + } + + if (fgets(buf, sizeof(buf), __pwd.db) == NULL) { + endpwent(); + return NULL; + } + + user = buf; + if ((password = strchr(buf, ':')) != NULL) { + *password = '\0'; + password++; + } else { + return NULL; + } + + if ((uid = strchr(password, ':')) != NULL) { + *uid = '\0'; + uid++; + } else { + return NULL; + } + + if ((gid = strchr(uid, ':')) != NULL) { + *gid = '\0'; + gid++; + } else { + return NULL; + } + + if ((gecos = strchr(gid, ':')) != NULL) { + *gecos = '\0'; + gecos++; + } else { + return NULL; + } + + if ((home = strchr(gecos, ':')) != NULL) { + *home = '\0'; + home++; + } else { + return NULL; + } + + if ((shell = strchr(home, ':')) != NULL) { + *shell = '\0'; + shell++; + if ((nl = strchr(shell, '\n')) != NULL) { + *nl = '\0'; + } + } else { + return NULL; + } + + __pwd.pwd.pw_name = user; + __pwd.pwd.pw_uid = strtoul(uid, NULL, 10); + __pwd.pwd.pw_gid = strtoul(gid, NULL, 10); + __pwd.pwd.pw_dir = home; + __pwd.pwd.pw_shell = shell; + + return &__pwd.pwd; } /* diff --git a/src/pwd/setpwent.c b/src/pwd/setpwent.c index ddee4014..0e52d9c0 100644 --- a/src/pwd/setpwent.c +++ b/src/pwd/setpwent.c @@ -1,7 +1,12 @@ #include <pwd.h> +#include "_pwd.h" +#include "stdio.h" void setpwent(void) { + if (__pwd.db != NULL) { + rewind(__pwd.db); + } } /* |