summaryrefslogtreecommitdiff
path: root/src/grp/getgrent.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/grp/getgrent.c')
-rw-r--r--src/grp/getgrent.c75
1 files changed, 74 insertions, 1 deletions
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 <grp.h>
#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;
}
/*