From 15aaf67be87b832aa03eaf12089cd56d806f7e83 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Fri, 25 Mar 2016 19:30:16 +0000 Subject: [PATCH] groups: add command --- Applications/util/Makefile | 1 + Applications/util/groups.c | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Applications/util/groups.c diff --git a/Applications/util/Makefile b/Applications/util/Makefile index a1582e98..511fdcbb 100644 --- a/Applications/util/Makefile +++ b/Applications/util/Makefile @@ -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 index 00000000..a3e14f1a --- /dev/null +++ b/Applications/util/groups.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +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); +} -- 2.34.1