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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <config.h> #include <stdio.h> #include <sys/types.h> #include <pwd.h>
#include "system.h" #include "die.h" #include "error.h" #include "long-options.h" #include "quote.h"
#define PROGRAM_NAME "whoami"
#define AUTHORS proper_name ("Richard Mlynarik")
void usage (int status) { if (status != EXIT_SUCCESS) emit_try_help (); else { printf (_("Usage: %s [OPTION]...\n"), program_name); fputs (_("\ Print the user name associated with the current effective user ID.\n\ Same as id -un.\n\ \n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); emit_ancillary_info (PROGRAM_NAME); } exit (status); }
int main (int argc, char **argv) { struct passwd *pw; uid_t uid; uid_t NO_UID = -1;
initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE);
atexit (close_stdout);
parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, true, usage, AUTHORS, (char const *) NULL);
if (optind != argc) { error (0, 0, _("extra operand %s"), quote (argv[optind])); usage (EXIT_FAILURE); }
errno = 0; uid = geteuid (); pw = (uid == NO_UID && errno ? NULL : getpwuid (uid)); if (!pw) die (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"), (unsigned long int) uid); puts (pw->pw_name); return EXIT_SUCCESS; }
|