lazysphere/src/commands/groups.c

32 lines
678 B
C
Raw Normal View History

2023-04-27 23:41:32 +00:00
#include "../command.h"
#include <grp.h>
#include <pwd.h>
COMMAND_EMPTY(groups) {
2023-04-28 04:39:56 +00:00
uid_t uid = getuid();
2023-04-27 23:41:32 +00:00
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);
2023-04-28 04:39:56 +00:00
gid_t groups[ngroups];
2023-04-27 23:41:32 +00:00
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;
}