summaryrefslogtreecommitdiff
path: root/src/commands/groups.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-05-04 16:10:37 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-05-04 16:10:37 -0400
commitb1364be7e271c5a080e29efcda209a190a82d6d9 (patch)
treefc64d1546e59b5ed1c2c204612b6181bc401c27f /src/commands/groups.c
parentgrep (diff)
downloadlazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.tar.gz
lazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.tar.bz2
lazysphere-b1364be7e271c5a080e29efcda209a190a82d6d9.zip
ansii c
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;
}