lazysphere/src/commands/whoami.c

23 lines
511 B
C
Raw Normal View History

2023-05-01 04:31:13 +00:00
#include "../command.h"
#include <pwd.h>
static void help() {
printf("Usage: whoami\n\n");
printf("Print the username associated with the current effective user id\n");
exit(EXIT_SUCCESS);
}
COMMAND(whoami) {
if (argc > 0 && streql("--help", argv[0])) help();
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;
}