summaryrefslogtreecommitdiff
path: root/src/commands/groups.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/commands/groups.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/commands/groups.c b/src/commands/groups.c
index 763f294..bd2e5f9 100644
--- a/src/commands/groups.c
+++ b/src/commands/groups.c
@@ -5,27 +5,38 @@
COMMAND_EMPTY(groups) {
- uid_t uid = getuid();
+ uid_t uid;
+ int ngroups, i;
+ gid_t* groups;
+ struct passwd* pw;
+
+ uid = getuid();
- struct passwd* pw = getpwuid(uid);
+ pw = getpwuid(uid);
if(pw == NULL){
error("failed to fetch groups: %s", strerror(errno));
}
- int ngroups = 0;
+ ngroups = 0;
getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups);
- gid_t groups[ngroups];
+ groups = malloc(sizeof(gid_t) * ngroups);
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
-
- for (int i = 0; i < ngroups; i++){
+
+ for (i = 0; i < ngroups; i++){
struct group* gr = getgrgid(groups[i]);
- if(gr == NULL){
+
+ if(gr == NULL) {
+ free(groups);
error("failed to fetch groups: %s", strerror(errno));
}
+
printf("%s ",gr->gr_name);
}
+
printf("\n");
+ free(groups);
+
return EXIT_SUCCESS;
}