From d8f2c10b7108fff6b7e437291093a1cadc15ab9f Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sat, 6 May 2023 00:39:44 -0400 Subject: refactor --- command/whoami.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 command/whoami.c (limited to 'command/whoami.c') diff --git a/command/whoami.c b/command/whoami.c new file mode 100644 index 0000000..5823b8f --- /dev/null +++ b/command/whoami.c @@ -0,0 +1,30 @@ +#include "command.h" +#include "lslib.h" + +#include +#include +#include + +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) { + + 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; +} -- cgit v1.2.3-freya