diff options
Diffstat (limited to 'doc')
-rw-r--r-- | doc/whoami.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/doc/whoami.c b/doc/whoami.c new file mode 100644 index 0000000..59f9180 --- /dev/null +++ b/doc/whoami.c @@ -0,0 +1,34 @@ +/* whoami.c - a simple implementation of whoami utility */ +#include <pwd.h> +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> + +int +who_am_i (void) +{ + struct passwd *pw; + char *user = NULL; + + pw = getpwuid (geteuid ()); + if (pw) + user = pw->pw_name; + else if ((user = getenv ("USER")) == NULL) + { + fprintf (stderr, "I don't know!\n"); + return 1; + } + printf ("%s\n", user); + return 0; +} + +int +main (int argc, char **argv) +{ + if (argc > 1) + { + fprintf (stderr, "usage: whoami\n"); + return 1; + } + return who_am_i (); +} |