master xplshn/aruu / cmd / pseudo / whoami.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <errno.h>
 4#include <pwd.h>
 5#include <stdio.h>
 6#include <unistd.h>
 7
 8#include "util.h"
 9
10static void
11usage(void)
12{
13  eprintf("usage: %s\n", argv0);
14}
15
16// ?man whoami: print effective user name
17// ?man display the effective user name of the current process
18int
19main(int argc, char *argv[])
20{
21  uid_t          uid;
22  struct passwd *pw;
23
24  argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
25
26  if (argc)
27    usage();
28
29  uid   = geteuid();
30  errno = 0;
31  if (!(pw = getpwuid(uid))) {
32    if (errno)
33      eprintf("getpwuid %d:", uid);
34    else
35      eprintf("getpwuid %d: no such user\n", uid);
36  }
37  puts(pw->pw_name);
38
39  return fshut(stdout, "<stdout>");
40}