diff options
author | Jim Meyering <jim@meyering.net> | 1994-02-01 14:11:50 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1994-02-01 14:11:50 +0000 |
commit | 75a474834b90ba6f7e9a083025f03af41979b876 (patch) | |
tree | 1604a3eeb2506026007046d5ad6fea7cb4277b6c /src/whoami.c | |
parent | 525ecdfbe16bda16df47d6644a090ed1fb7c0b8b (diff) | |
download | coreutils-75a474834b90ba6f7e9a083025f03af41979b876.tar.xz |
.
Diffstat (limited to 'src/whoami.c')
-rw-r--r-- | src/whoami.c | 84 |
1 files changed, 79 insertions, 5 deletions
diff --git a/src/whoami.c b/src/whoami.c index 4a12406f1..833ae1421 100644 --- a/src/whoami.c +++ b/src/whoami.c @@ -1,5 +1,5 @@ /* whoami -- print effective userid - Copyright (C) 1989, 1991 Free Software Foundation, Inc. + Copyright (C) 89, 90, 91, 92, 93, 1994 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 @@ -18,11 +18,62 @@ /* Equivalent to `id -un'. */ /* Written by Richard Mlynarik. */ +#ifdef HAVE_CONFIG_H +#if defined (CONFIG_BROKETS) +/* We use <config.h> instead of "config.h" so that a compilation + using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h + (which it would do because it found this file in $srcdir). */ +#include <config.h> +#else +#include "config.h" +#endif +#endif + #include <stdio.h> #include <sys/types.h> #include <pwd.h> +#include <getopt.h> + +#include "version.h" #include "system.h" +/* The name this program was run with. */ +char *program_name; + +/* If non-zero, display usage information and exit. */ +static int show_help; + +/* If non-zero, print the version on standard output and exit. */ +static int show_version; + +static struct option const long_options[] = +{ + {"help", no_argument, &show_help, 1}, + {"version", no_argument, &show_version, 1}, + {0, 0, 0, 0} +}; + +static void +usage (status) + int status; +{ + if (status != 0) + fprintf (stderr, "Try `%s --help' for more information.\n", + program_name); + else + { + printf ("Usage: %s [OPTION]...\n", program_name); + printf ("\ +\n\ + --help display this help and exit\n\ + --version output version information and exit\n\ +\n\ +Same as id -un.\n\ +"); + } + exit (status); +} + void main (argc, argv) int argc; @@ -30,13 +81,35 @@ main (argc, argv) { register struct passwd *pw; register uid_t uid; + int c; + + program_name = argv[0]; + + while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF) + { + switch (c) + { + case 0: + break; + + default: + usage (1); + } + } - if (argc != 1) + if (show_version) { - fprintf (stderr, "Usage: %s\n", argv[0]); - exit (1); + printf ("%s\n", version_string); + exit (0); } + if (show_help) + usage (0); + + + if (optind != argc) + usage (1); + uid = geteuid (); pw = getpwuid (uid); if (pw) @@ -44,6 +117,7 @@ main (argc, argv) puts (pw->pw_name); exit (0); } - fprintf (stderr,"%s: cannot find username for UID %u\n", argv[0], uid); + fprintf (stderr, "%s: cannot find username for UID %u\n", + program_name, (unsigned) uid); exit (1); } |