summaryrefslogtreecommitdiff
path: root/util/sbase/whoami.c
blob: c991ac297b6f013174e5498e0b2e1fd1c8ed9caa (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
28
29
30
31
32
33
34
35
36
37
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>

#include "util.h"

static void
usage(void)
{
	eprintf("usage: %s\n", argv0);
}

int
main(int argc, char *argv[])
{
	uid_t uid;
	struct passwd *pw;

	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;

	if (argc)
		usage();

	uid = geteuid();
	errno = 0;
	if (!(pw = getpwuid(uid))) {
		if (errno)
			eprintf("getpwuid %d:", uid);
		else
			eprintf("getpwuid %d: no such user\n", uid);
	}
	puts(pw->pw_name);

	return fshut(stdout, "<stdout>");
}