lazysphere/command/whoami.c

31 lines
578 B
C
Raw Normal View History

2023-05-06 04:39:44 +00:00
#include "command.h"
#include "lslib.h"
2023-05-01 04:31:13 +00:00
#include <pwd.h>
2023-05-06 04:39:44 +00:00
#include <stdlib.h>
#include <unistd.h>
2023-05-01 04:31:13 +00:00
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-04 20:10:37 +00:00
uid_t usr;
struct passwd* passwd;
2023-05-01 22:43:32 +00:00
parse_help(argc, argv, help);
2023-05-01 04:31:13 +00:00
2023-05-04 20:10:37 +00:00
usr = getuid();
passwd = getpwuid(usr);
2023-05-01 04:31:13 +00:00
if (passwd == NULL) {
printf("\x1b[1;91myou do not exist.\n");
} else {
printf("%s\n", passwd->pw_name);
}
return EXIT_SUCCESS;
}