/* whoami -- print effective userid This is the whoami utility
Copyright (C) 1989-2018 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Equivalent to 'id -un'. */
/* Written by Richard Mlynarik. */
#include <config.h> Provides system specific information
#include <stdio.h> Provides standard I/O capability
#include <sys/types.h> sys/types.h defines uid_t
#include <pwd.h> ...!includes auto-comment...
#include <getopt.h> ...!includes auto-comment...
#include "system.h" ...!includes auto-comment...
#include "die.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "long-options.h" ...!includes auto-comment...
#include "quote.h" ...!includes auto-comment...
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "whoami" Utility name macro to assist usage() output
#define AUTHORS proper_name ("Richard Mlynarik") Program authors macro used in parsing
static struct option const long_options[] = Defines long options processed in parsing (see main)
{
{NULL, 0, NULL, 0} whoami has no long options
}; The global long options structure
void
usage (int status)
{
if (status != EXIT_SUCCESS) If the failure is due to user error...
emit_try_help (); Suggest the user try --help (See system.h)
else
{
printf (_("Usage: %s [OPTION]...\n"), program_name); Print the suggested usage format
fputs (_("\ _() is defined to gettext()
Print the user name associated with the current effective user ID.\n\ Prints the utility purpose
Same as id -un.\n\ whoami is the same as id
\n\
"), stdout); Push help output to STDOUT
fputs (HELP_OPTION_DESCRIPTION, stdout); Print --help description (See system.h)
fputs (VERSION_OPTION_DESCRIPTION, stdout); Print --version description (See system.h)
emit_ancillary_info (PROGRAM_NAME); Print help extras
} The user passed --help
exit (status); Exit using provided status
} The usage function describing syntax
int
main (int argc, char **argv) main() takes the usual arguments
...the arg count
...the arg vector
{
struct passwd *pw; Local pointer to passwd info after getpwuid() call
uid_t uid; Declares a uid_t to hold geteuid() result
uid_t NO_UID = -1; Defines a failure constant to compare
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
atexit (close_stdout); Close stdout on exit (see gnulib)
parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, Parses long options...
usage, AUTHORS, (char const *) NULL); ...which are only --help and --version
if (getopt_long (argc, argv, "", long_options, NULL) != -1) If the user provides any additional options...
usage (EXIT_FAILURE); ...print usage and failure since none are valid
if (optind != argc) If the user passed some operand...
(there are no valid operands)
{
error (0, 0, _("extra operand %s"), quote (argv[optind])); Throw an error message
usage (EXIT_FAILURE); Print proper usage and exit failure
} If the user passed some operand...
errno = 0; Resets errno in prep for syscall
uid = geteuid (); Retrieve UID running this processuid_t geteuid(void)
The geteuid() function shall return the
effective user ID of the calling process.
The geteuid() function shall not modify
errno.
pw = (uid == NO_UID && errno ? NULL : getpwuid (uid)); Looks up passwd struct associated with UID
if (!pw) If no passwd exists...
die (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"), Exit with error message for failed UID
(unsigned long int) uid); Casts uid to uint described in die format
puts (pw->pw_name); Prints name associated with passwd entry
return EXIT_SUCCESS; Successful execution -- exit
} The main() function for whoami