diff options
author | Jim Meyering <jim@meyering.net> | 1993-09-08 18:23:12 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1993-09-08 18:23:12 +0000 |
commit | ae0074289cd7d70cf8fb1d96f2625b2b9bb62b29 (patch) | |
tree | d5eab2eede21baf91b94efaf06e0df67cca78887 /src/logname.c | |
parent | 219bbb0758350d94610e967f31b5f9fdb0728354 (diff) | |
download | coreutils-ae0074289cd7d70cf8fb1d96f2625b2b9bb62b29.tar.xz |
merge with 1.8.1 + partial --version and --help
Diffstat (limited to 'src/logname.c')
-rw-r--r-- | src/logname.c | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/src/logname.c b/src/logname.c index 39d213709..19fea40cb 100644 --- a/src/logname.c +++ b/src/logname.c @@ -17,27 +17,77 @@ #include <stdio.h> #include <sys/types.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 error. */ +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 () +{ + fprintf (stderr, "Usage: %s [{--help,--version}]\n", + program_name); + exit (1); +} + void main (argc, argv) int argc; char **argv; { register char *cp; + int c; + + program_name = argv[0]; - if (argc != 1) + while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF) { - fprintf (stderr, "Usage: %s\n", argv[0]); - exit (1); + switch (c) + { + case 0: + break; + + default: + usage (); + } } + if (show_version) + { + printf ("%s\n", version_string); + exit (0); + } + + if (show_help) + usage (); + + if (argc - optind != 0) + usage (); + + /* POSIX.2 requires using getlogin (or equivalent code). */ cp = getlogin (); if (cp) { puts (cp); exit (0); } + /* POSIX.2 prohibits using a fallback technique. */ fprintf (stderr,"%s: no login name\n", argv[0]); exit (1); } |