summaryrefslogtreecommitdiff
path: root/src/commands/groups.c
blob: 4aa7776e0de492b35f3146fef1b47c844216c55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "../command.h"

#include <grp.h>
#include <pwd.h>
#include <unistd.h>

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;
}