add groups and id commands

This commit is contained in:
Freya Murphy 2023-04-27 19:41:32 -04:00
parent 2cdbf9cbab
commit c531ad32bf
5 changed files with 83 additions and 2 deletions

View file

@ -4,7 +4,7 @@
A terrible busybox/gnu coreutils clone. A terrible busybox/gnu coreutils clone.
Currently the only supported commands are: Currently the only supported commands are:
`dd`, `cat`, `yes`, `echo`, `printf` `dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups`
## How to ## How to

View file

@ -7,9 +7,12 @@
#define ARGUMENTS int argc, char** argv #define ARGUMENTS int argc, char** argv
#define NEXT_ARGS argc - 1, &argv[1] #define NEXT_ARGS argc - 1, &argv[1]
#define COMMAND(name) int name (ARGUMENTS) #define COMMAND(name) int name (ARGUMENTS)
#define COMMAND_EMPTY(name) int name (void)
COMMAND(dd); COMMAND(dd);
COMMAND(cat); COMMAND(cat);
COMMAND(yes); COMMAND(yes);
COMMAND(echo); COMMAND(echo);
COMMAND(print); COMMAND(print);
COMMAND_EMPTY(groups);
COMMAND_EMPTY(user_id);

32
src/commands/groups.c Normal file
View file

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

42
src/commands/id.c Normal file
View file

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

View file

@ -20,7 +20,7 @@ int main (ARGUMENTS) {
if (argc < 2) { if (argc < 2) {
printf("usage: lazysphere [function [arguments]...]\n\n"); printf("usage: lazysphere [function [arguments]...]\n\n");
printf("currently defined functions:\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; return EXIT_SUCCESS;
} }
argc--; argc--;
@ -44,6 +44,10 @@ int main (ARGUMENTS) {
return echo(NEXT_ARGS); return echo(NEXT_ARGS);
} else if (streql(cmd, "printf")) { } else if (streql(cmd, "printf")) {
return print(NEXT_ARGS); return print(NEXT_ARGS);
} else if (streql(cmd, "groups")) {
return groups();
} else if (streql(cmd, "id")) {
return user_id();
} else { } else {
error("error: invalid command %s", cmd); error("error: invalid command %s", cmd);
} }