diff --git a/readme.md b/readme.md index ef7e7ca..eb605d8 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ A terrible busybox/gnu coreutils clone. Currently the only supported commands are: -`dd`, `cat`, `yes`, `echo`, `printf` +`dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups` ## How to diff --git a/src/command.h b/src/command.h index d1161c4..354f2ee 100644 --- a/src/command.h +++ b/src/command.h @@ -7,9 +7,12 @@ #define ARGUMENTS int argc, char** argv #define NEXT_ARGS argc - 1, &argv[1] #define COMMAND(name) int name (ARGUMENTS) +#define COMMAND_EMPTY(name) int name (void) COMMAND(dd); COMMAND(cat); COMMAND(yes); COMMAND(echo); COMMAND(print); +COMMAND_EMPTY(groups); +COMMAND_EMPTY(user_id); 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 +#include +#include + +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 +#include +#include + +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; +} diff --git a/src/main.c b/src/main.c index 98c0e81..28961c9 100644 --- a/src/main.c +++ b/src/main.c @@ -20,7 +20,7 @@ int main (ARGUMENTS) { if (argc < 2) { printf("usage: lazysphere [function [arguments]...]\n\n"); printf("currently defined functions:\n"); - printf("\tdd, cat, yes, echo, printf\n"); + printf("\tdd, cat, yes, echo, printf, id, groups\n"); return EXIT_SUCCESS; } argc--; @@ -44,6 +44,10 @@ int main (ARGUMENTS) { return echo(NEXT_ARGS); } else if (streql(cmd, "printf")) { return print(NEXT_ARGS); + } else if (streql(cmd, "groups")) { + return groups(); + } else if (streql(cmd, "id")) { + return user_id(); } else { error("error: invalid command %s", cmd); }