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