whoami.c - sbase - suckless unix tools
(HTM) git clone git://git.suckless.org/sbase
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
whoami.c (582B)
---
1 /* See LICENSE file for copyright and license details. */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <pwd.h>
6
7 #include "util.h"
8
9 static void
10 usage(void)
11 {
12 eprintf("usage: %s\n", argv0);
13 }
14
15 int
16 main(int argc, char *argv[])
17 {
18 uid_t uid;
19 struct passwd *pw;
20
21 argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
22
23 if (argc)
24 usage();
25
26 uid = geteuid();
27 errno = 0;
28 if (!(pw = getpwuid(uid))) {
29 if (errno)
30 eprintf("getpwuid %d:", uid);
31 else
32 eprintf("getpwuid %d: no such user\n", uid);
33 }
34 puts(pw->pw_name);
35
36 return fshut(stdout, "<stdout>");
37 }