summaryrefslogtreecommitdiff
path: root/src/commands/whoami.c
blob: 7fd7c85693267a1e3cb477ce9b14ebc4230990e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "../command.h"

#include <pwd.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) {

    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;
}