lazysphere/src/commands/whoami.c

23 lines
494 B
C
Raw Normal View History

2023-05-01 04:31:13 +00:00
#include "../command.h"
#include <pwd.h>
2023-05-01 22:43:32 +00:00
static void help(void) {
2023-05-01 04:31:13 +00:00
printf("Usage: whoami\n\n");
printf("Print the username associated with the current effective user id\n");
exit(EXIT_SUCCESS);
}
COMMAND(whoami) {
2023-05-01 22:43:32 +00:00
parse_help(argc, argv, help);
2023-05-01 04:31:13 +00:00
uid_t usr = getuid();
struct passwd* passwd = getpwuid(usr);
if (passwd == NULL) {
printf("\x1b[1;91myou do not exist.\n");
} else {
printf("%s\n", passwd->pw_name);
}
return EXIT_SUCCESS;
}