1 /* printenv -- print all or part of environment
2 Copyright (C) 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Usage: printenv [variable...]
20 If no arguments are given, print the entire environment.
21 If one or more variable names are given, print the value of
22 each one that is set, and nothing for ones that are not set.
25 0 if all variables specified were found
28 David MacKenzie and Richard Mlynarik */
32 #include <sys/types.h>
38 /* The name this program was run with. */
41 /* If nonzero, display usage information and exit. */
44 /* If nonzero, print the version on standard output and exit. */
45 static int show_version;
47 static struct option const long_options[] =
49 {"help", no_argument, &show_help, 1},
50 {"version", no_argument, &show_version, 1},
54 extern char **environ;
60 fprintf (stderr, _("Try `%s --help' for more information.\n"),
64 printf (_("Usage: %s [OPTION]... [VARIABLE]...\n"), program_name);
66 If no environment VARIABLE specified, print them all.\n\
68 --help display this help and exit\n\
69 --version output version information and exit\n"));
75 main (int argc, char **argv)
84 program_name = argv[0];
85 setlocale (LC_ALL, "");
86 bindtextdomain (PACKAGE, LOCALEDIR);
89 while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
103 printf ("printenv - %s\n", PACKAGE_VERSION);
112 for (env = environ; *env != NULL; ++env)
118 for (i = optind; i < argc; ++i)
120 for (env = environ; *env; ++env)
124 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
126 if (*ep == '=' && *ap == '\0')
135 exit_status = (matches != argc - optind);
138 if (ferror (stdout) || fclose (stdout) == EOF)
139 error (2, errno, _("standard output"));