groups: add command
authorAlan Cox <alan@linux.intel.com>
Fri, 25 Mar 2016 19:30:16 +0000 (19:30 +0000)
committerAlan Cox <alan@linux.intel.com>
Fri, 25 Mar 2016 19:30:16 +0000 (19:30 +0000)
Applications/util/Makefile
Applications/util/groups.c [new file with mode: 0644]

index a1582e9..511fdcb 100644 (file)
@@ -22,6 +22,7 @@ SRCSNS = \
        date.c \
        dirname.c \
        false.c \
+       groups.c \
        head.c \
        init.c \
        kill.c \
diff --git a/Applications/util/groups.c b/Applications/util/groups.c
new file mode 100644 (file)
index 0000000..a3e14f1
--- /dev/null
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <grp.h>
+
+static void writesn(const char *p)
+{
+  write(1, p, strlen(p));
+  write(1, "\n", 1);
+}
+
+static void writeg(gid_t g)
+{
+  struct group *gp;
+
+  gp = getgrgid(g);
+  if (gp)
+    writesn(gp->gr_name);
+  else
+    writesn(_itoa(g));
+}
+
+int main(int argc, char *argv[])
+{
+  gid_t *g;
+  int n = getgroups(0, NULL);
+  gid_t eg = getegid();
+  int egp = 1;
+
+  /* ENOSYS means a Level 1 system so just do the right thing */
+  if (n < 0 && errno != ENOSYS) {
+    perror(argv[0]);
+    exit(1);
+  }
+  if (n > 0) {
+    g = (gid_t *)sbrk(sizeof(gid_t) * n);
+    if (g == (gid_t *)-1) {
+      perror(argv[0]);
+      exit(1);
+    }
+    if (getgroups(n, g) < 0) {
+      perror(argv[1]);
+      exit(1);
+    }
+    while(n--) {
+      /* Current gid is also in groups table */
+      if (*g == eg)
+        egp = 0;
+      writeg(*g++);
+    }
+  }
+  if (egp) {
+    writeg(eg);
+  }
+  exit(0);
+}