From 4f4d04f00b768157cde2c4b457377d70c61b4692 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 14 Aug 2020 16:57:42 -0400 Subject: implement --- src/grp/endgrent.c | 6 +++++ src/grp/getgrent.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/grp/getgrgid.c | 14 ++++++++-- src/grp/getgrnam.c | 15 +++++++++-- src/grp/setgrent.c | 5 ++++ 5 files changed, 110 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/grp/endgrent.c b/src/grp/endgrent.c index f9e84d81..00e4beb1 100644 --- a/src/grp/endgrent.c +++ b/src/grp/endgrent.c @@ -1,7 +1,13 @@ #include +#include "_grp.h" +#include "stdio.h" void endgrent(void) { + if (__grp.db != NULL) { + fclose(__grp.db); + __grp.db = NULL; + } } /* diff --git a/src/grp/getgrent.c b/src/grp/getgrent.c index 228f916c..aac4e175 100644 --- a/src/grp/getgrent.c +++ b/src/grp/getgrent.c @@ -1,9 +1,82 @@ +#include "sys/types.h" #include #include "stddef.h" +#include "stdio.h" +#include "limits.h" +#include "string.h" +#include "_config.h" +#include "_grp.h" + +#ifndef LINE_MAX +#define LINE_MAX _POSIX2_LINE_MAX +#endif + +#ifndef _XOPEN_SOURCE + static +#endif struct group * getgrent(void) { - return NULL; + static char buf[LINE_MAX + 1]; + char *group, *password, *gid, *members, *nl; + int member = 0; + + /* TODO: attempt first calling _GRP_CMD */ + + if (__grp.db == NULL) { + __grp.db = fopen(_GRP_DB, "r"); + if (__grp.db == NULL) { + return NULL; + } + } + + if (fgets(buf, sizeof(buf), __grp.db) == NULL) { + endgrent(); + return NULL; + } + + group = buf; + if ((password = strchr(buf, ':')) != NULL) { + *password = '\0'; + password++; + } else { + return NULL; + } + + if ((gid = strchr(password, ':')) != NULL) { + *gid = '\0'; + gid++; + } else { + return NULL; + } + + if ((members = strchr(gid, ':')) != NULL) { + *members = '\0'; + members++; + if ((nl = strchr(members, '\n')) != NULL) { + *nl = '\0'; + } + } else { + return NULL; + } + + __grp.grp.gr_name = group; + __grp.grp.gr_gid = strtoul(gid, NULL, 10); + + while (members && *members) { + __grp.members[member++] = members; + members = strchr(members, ','); + if (members) { + *members = '\0'; + members++; + } + } + + __grp.members[member] = NULL; + + __grp.grp.gr_mem = __grp.members; + + return &__grp.grp; } /* diff --git a/src/grp/getgrgid.c b/src/grp/getgrgid.c index 0d39ffc2..af10469c 100644 --- a/src/grp/getgrgid.c +++ b/src/grp/getgrgid.c @@ -1,10 +1,20 @@ #include "sys/types.h" #include -#include "stddef.h" +#include "_grp.h" struct group * getgrgid(gid_t gid) { - (void)gid; + struct group *grp = NULL; + setgrent(); + + while ((grp = getgrent()) != NULL) { + if (grp->gr_gid == gid) { + endgrent(); + return grp; + } + } + + endgrent(); return NULL; } diff --git a/src/grp/getgrnam.c b/src/grp/getgrnam.c index c3598866..493a6b5a 100644 --- a/src/grp/getgrnam.c +++ b/src/grp/getgrnam.c @@ -1,10 +1,21 @@ #include "sys/types.h" #include -#include "stddef.h" +#include "_grp.h" +#include "string.h" struct group * getgrnam(const char * name) { - (void)name; + struct group *grp = NULL; + setgrent(); + + while ((grp = getgrent()) != NULL) { + if (strcmp(name, grp->gr_name) == 0) { + endgrent(); + return grp; + } + } + + endgrent(); return NULL; } diff --git a/src/grp/setgrent.c b/src/grp/setgrent.c index 17b5872b..4e445404 100644 --- a/src/grp/setgrent.c +++ b/src/grp/setgrent.c @@ -1,7 +1,12 @@ #include +#include "_grp.h" +#include "stdio.h" void setgrent(void) { + if (__grp.db != NULL) { + rewind(__grp.db); + } } /* -- cgit v1.2.1