lazysphere/command/whoami.c
2023-05-15 10:57:33 -04:00

30 lines
583 B
C

#include "command.h"
#include "lslib.h"
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
static void help(void) {
printf("Usage: whoami\n\n");
printf("Print the username associated with the current effective user id\n");
exit(EXIT_SUCCESS);
}
COMMAND(whoami_main) {
uid_t usr;
struct passwd* passwd;
parse_help(argc, argv, help);
usr = getuid();
passwd = getpwuid(usr);
if (passwd == NULL) {
printf("\x1b[1;91myou do not exist.\n");
} else {
printf("%s\n", passwd->pw_name);
}
return EXIT_SUCCESS;
}