1992-11-01 05:44:29 +00:00
|
|
|
/* whoami -- print effective userid
|
2004-01-21 23:50:20 +00:00
|
|
|
|
2008-05-19 16:49:35 +02:00
|
|
|
Copyright (C) 89,90, 1991-1997, 1999-2002, 2004, 2005, 2007-2008
|
|
|
|
|
Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2007-07-23 14:35:58 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
1992-11-01 05:44:29 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-23 14:35:58 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
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
|
2007-07-23 14:35:58 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
/* Equivalent to `id -un'. */
|
|
|
|
|
/* Written by Richard Mlynarik. */
|
|
|
|
|
|
1994-02-01 14:11:50 +00:00
|
|
|
#include <config.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <pwd.h>
|
1994-02-01 14:11:50 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include "system.h"
|
2004-06-21 15:03:35 +00:00
|
|
|
#include "error.h"
|
1999-03-06 15:28:54 +00:00
|
|
|
#include "long-options.h"
|
2004-06-21 15:03:35 +00:00
|
|
|
#include "quote.h"
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1999-03-31 04:16:08 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "whoami"
|
|
|
|
|
|
2008-05-19 16:24:27 +02:00
|
|
|
#define AUTHORS proper_name ("Richard Mlynarik")
|
1999-03-31 04:16:08 +00:00
|
|
|
|
1999-01-25 14:33:38 +00:00
|
|
|
void
|
1996-01-06 11:44:05 +00:00
|
|
|
usage (int status)
|
1994-02-01 14:11:50 +00:00
|
|
|
{
|
2004-01-21 23:50:20 +00:00
|
|
|
if (status != EXIT_SUCCESS)
|
1995-08-08 04:37:34 +00:00
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
1994-02-01 14:11:50 +00:00
|
|
|
program_name);
|
|
|
|
|
else
|
|
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
printf (_("Usage: %s [OPTION]...\n"), program_name);
|
2001-12-15 20:46:30 +00:00
|
|
|
fputs (_("\
|
2005-04-12 05:44:47 +00:00
|
|
|
Print the user name associated with the current effective user ID.\n\
|
1995-05-15 04:53:56 +00:00
|
|
|
Same as id -un.\n\
|
1994-02-01 14:11:50 +00:00
|
|
|
\n\
|
2001-12-15 20:46:30 +00:00
|
|
|
"), stdout);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2007-03-28 08:50:29 +02:00
|
|
|
emit_bug_reporting_address ();
|
1994-02-01 14:11:50 +00:00
|
|
|
}
|
|
|
|
|
exit (status);
|
|
|
|
|
}
|
|
|
|
|
|
1996-04-24 03:23:54 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
2005-03-06 16:19:44 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
|
uid_t uid;
|
1994-02-01 14:11:50 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
2008-06-03 08:34:09 +02:00
|
|
|
set_program_name (argv[0]);
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
1994-02-01 14:11:50 +00:00
|
|
|
|
2000-05-07 14:46:33 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
2007-08-30 17:11:59 +02:00
|
|
|
parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
|
2003-11-05 03:53:19 +00:00
|
|
|
usage, AUTHORS, (char const *) NULL);
|
2004-11-17 00:56:25 +00:00
|
|
|
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
|
2004-09-21 22:09:09 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1994-02-01 14:11:50 +00:00
|
|
|
if (optind != argc)
|
2004-06-21 15:03:35 +00:00
|
|
|
{
|
|
|
|
|
error (0, 0, _("extra operand %s"), quote (argv[optind]));
|
|
|
|
|
usage (EXIT_FAILURE);
|
|
|
|
|
}
|
1994-02-01 14:11:50 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
uid = geteuid ();
|
|
|
|
|
pw = getpwuid (uid);
|
|
|
|
|
if (pw)
|
|
|
|
|
{
|
|
|
|
|
puts (pw->pw_name);
|
2002-08-31 08:52:10 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
2005-04-12 05:44:47 +00:00
|
|
|
fprintf (stderr, _("%s: cannot find name for user ID %lu\n"),
|
2004-08-03 23:38:40 +00:00
|
|
|
program_name, (unsigned long int) uid);
|
2002-08-31 08:52:10 +00:00
|
|
|
exit (EXIT_FAILURE);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|