diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-27 19:41:32 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-27 19:41:32 -0400 |
commit | c531ad32bf5670bc3f66f1d3c36b4e93a2b380c9 (patch) | |
tree | dff3054500b2e932dbe5550828a387c0f2047671 /src/commands | |
parent | remove tabs (diff) | |
download | lazysphere-c531ad32bf5670bc3f66f1d3c36b4e93a2b380c9.tar.gz lazysphere-c531ad32bf5670bc3f66f1d3c36b4e93a2b380c9.tar.bz2 lazysphere-c531ad32bf5670bc3f66f1d3c36b4e93a2b380c9.zip |
add groups and id commands
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/groups.c | 32 | ||||
-rw-r--r-- | src/commands/id.c | 42 |
2 files changed, 74 insertions, 0 deletions
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 <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; +} diff --git a/src/commands/id.c b/src/commands/id.c new file mode 100644 index 0000000..ab974b1 --- /dev/null +++ b/src/commands/id.c @@ -0,0 +1,42 @@ +#include "../command.h" + +#include <grp.h> +#include <pwd.h> +#include <unistd.h> + +COMMAND_EMPTY(user_id) { + + __uid_t uid = getuid(); + __gid_t gid = getgid(); + + 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); + + struct group* ugr = getgrgid(gid); + printf("uid=%d(%s) gid=%d(%s) ", + uid, ugr->gr_name, gid, ugr->gr_name); + + if (ngroups > 0) { + printf("groups="); + } + + for (int i = 0; i < ngroups; i++){ + struct group* gr = getgrgid(groups[i]); + if(gr == NULL){ + perror("error: failed to fetch groups: "); + } + printf("%d(%s)", gr->gr_gid, gr->gr_name); + if (i + 1 < ngroups) putchar(','); + } + printf("\b\n"); + + return EXIT_SUCCESS; +} |