1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-07-25 18:19:00 +02:00
Files
coreutils/src/whoami.c
T

95 lines
2.5 KiB
C
Raw Normal View History

1992-11-01 05:44:29 +00:00
/* whoami -- print effective userid
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
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
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
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"
#include "error.h"
1999-03-06 15:28:54 +00:00
#include "long-options.h"
#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"
#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
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
1994-02-01 14:11:50 +00:00
program_name);
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\
1994-02-01 14:11:50 +00:00
\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
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]);
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,
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)
{
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
}
fprintf (stderr, _("%s: cannot find name for user ID %lu\n"),
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
}