summaryrefslogtreecommitdiff
path: root/src/printenv.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1994-02-01 14:11:50 +0000
committerJim Meyering <jim@meyering.net>1994-02-01 14:11:50 +0000
commit75a474834b90ba6f7e9a083025f03af41979b876 (patch)
tree1604a3eeb2506026007046d5ad6fea7cb4277b6c /src/printenv.c
parent525ecdfbe16bda16df47d6644a090ed1fb7c0b8b (diff)
downloadcoreutils-75a474834b90ba6f7e9a083025f03af41979b876.tar.xz
.
Diffstat (limited to 'src/printenv.c')
-rw-r--r--src/printenv.c119
1 files changed, 103 insertions, 16 deletions
diff --git a/src/printenv.c b/src/printenv.c
index ac4511cc5..b5e3c8940 100644
--- a/src/printenv.c
+++ b/src/printenv.c
@@ -1,5 +1,5 @@
/* printenv -- print all or part of environment
- Copyright (C) 1989, 1991 Free Software Foundation.
+ 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
@@ -27,12 +27,65 @@
David MacKenzie and 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 <getopt.h>
+
+#include "version.h"
#include "system.h"
+void error ();
+
+/* 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}
+};
+
extern char **environ;
+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]... [VARIABLE]...\n", program_name);
+ printf ("\
+\n\
+ --help display this help and exit\n\
+ --version output version information and exit\n\
+\n\
+If no VARIABLE, print them all.\n\
+");
+ }
+ exit (status);
+}
+
void
main (argc, argv)
int argc;
@@ -42,28 +95,62 @@ main (argc, argv)
char *ep, *ap;
int i;
int matches = 0;
+ int c;
+ int exit_status;
+
+ program_name = argv[0];
- if (argc == 1)
+ while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
{
- for (env = environ; *env; ++env)
- puts (*env);
+ switch (c)
+ {
+ case 0:
+ break;
+
+ default:
+ usage (1);
+ }
+ }
+
+ if (show_version)
+ {
+ printf ("%s\n", version_string);
exit (0);
}
- for (i = 1; i < argc; ++i)
+ if (show_help)
+ usage (0);
+
+ if (optind == argc)
+ {
+ for (env = environ; *env != NULL; ++env)
+ puts (*env);
+ exit_status = 0;
+ }
+ else
{
- for (env = environ; *env; ++env)
+ for (i = optind; i < argc; ++i)
{
- ep = *env;
- ap = argv[i];
- while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
- if (*ep == '=' && *ap == '\0')
- {
- puts (ep + 1);
- ++matches;
- break;
- }
+ for (env = environ; *env; ++env)
+ {
+ ep = *env;
+ ap = argv[i];
+ while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
+ {
+ if (*ep == '=' && *ap == '\0')
+ {
+ puts (ep + 1);
+ ++matches;
+ break;
+ }
+ }
+ }
}
+ exit_status = (matches != argc - optind);
}
- exit (matches != argc - 1);
+
+ if (ferror (stdout) || fclose (stdout) == EOF)
+ error (2, errno, "standard output");
+
+ exit (exit_status);
}