Define ENOTSUP, not ENOSYS. Needed on OpenBSD 3.9.
[coreutils/ericb.git] / src / whoami.c
blob0e3a97d59db01cb30d3e15dc8c442be03f5923aa
1 /* whoami -- print effective userid
3 Copyright (C) 89,90, 1991-1997, 1999-2002, 2004, 2005, 2007 Free Software
4 Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Equivalent to `id -un'. */
20 /* Written by Richard Mlynarik. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <pwd.h>
26 #include <getopt.h>
28 #include "system.h"
29 #include "error.h"
30 #include "long-options.h"
31 #include "quote.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "whoami"
36 #define AUTHORS "Richard Mlynarik"
38 /* The name this program was run with. */
39 char *program_name;
41 void
42 usage (int status)
44 if (status != EXIT_SUCCESS)
45 fprintf (stderr, _("Try `%s --help' for more information.\n"),
46 program_name);
47 else
49 printf (_("Usage: %s [OPTION]...\n"), program_name);
50 fputs (_("\
51 Print the user name associated with the current effective user ID.\n\
52 Same as id -un.\n\
53 \n\
54 "), stdout);
55 fputs (HELP_OPTION_DESCRIPTION, stdout);
56 fputs (VERSION_OPTION_DESCRIPTION, stdout);
57 emit_bug_reporting_address ();
59 exit (status);
62 int
63 main (int argc, char **argv)
65 struct passwd *pw;
66 uid_t uid;
68 initialize_main (&argc, &argv);
69 program_name = argv[0];
70 setlocale (LC_ALL, "");
71 bindtextdomain (PACKAGE, LOCALEDIR);
72 textdomain (PACKAGE);
74 atexit (close_stdout);
76 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
77 usage, AUTHORS, (char const *) NULL);
78 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
79 usage (EXIT_FAILURE);
81 if (optind != argc)
83 error (0, 0, _("extra operand %s"), quote (argv[optind]));
84 usage (EXIT_FAILURE);
87 uid = geteuid ();
88 pw = getpwuid (uid);
89 if (pw)
91 puts (pw->pw_name);
92 exit (EXIT_SUCCESS);
94 fprintf (stderr, _("%s: cannot find name for user ID %lu\n"),
95 program_name, (unsigned long int) uid);
96 exit (EXIT_FAILURE);