From c531ad32bf5670bc3f66f1d3c36b4e93a2b380c9 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Thu, 27 Apr 2023 19:41:32 -0400 Subject: add groups and id commands --- src/commands/groups.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/commands/groups.c (limited to 'src/commands/groups.c') diff --git a/src/commands/groups.c b/src/commands/groups.c new file mode 100644 index 0000000..1969edf --- /dev/null +++ b/src/commands/groups.c @@ -0,0 +1,32 @@ +#include "../command.h" + +#include +#include +#include + +COMMAND_EMPTY(groups) { + + __uid_t uid = getuid(); + + struct passwd* pw = getpwuid(uid); + if(pw == NULL){ + perror("error: failed to fetch groups: "); + } + + int ngroups = 0; + getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); + + __gid_t groups[ngroups]; + getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); + + for (int i = 0; i < ngroups; i++){ + struct group* gr = getgrgid(groups[i]); + if(gr == NULL){ + perror("error: failed to fetch groups: "); + } + printf("%s ",gr->gr_name); + } + printf("\n"); + + return EXIT_SUCCESS; +} -- cgit v1.2.3-freya