global: convert indentation-TABs to spaces
[coreutils.git] / src / printenv.c
blob69e2a6639b7dab659657cd313015de193162c736
1 /* printenv -- print all or part of environment
2 Copyright (C) 1989-1997, 1999-2005, 2007-2009 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Usage: printenv [variable...]
19 If no arguments are given, print the entire environment.
20 If one or more variable names are given, print the value of
21 each one that is set, and nothing for ones that are not set.
23 Exit status:
24 0 if all variables specified were found
25 1 if not
26 2 if some other error occurred
28 David MacKenzie and Richard Mlynarik */
30 #include <config.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
35 #include "system.h"
36 #include "long-options.h"
38 /* Exit status for syntax errors, etc. */
39 enum { PRINTENV_FAILURE = 2 };
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "printenv"
44 #define AUTHORS \
45 proper_name ("David MacKenzie"), \
46 proper_name ("Richard Mlynarik")
48 extern char **environ;
50 void
51 usage (int status)
53 if (status != EXIT_SUCCESS)
54 fprintf (stderr, _("Try `%s --help' for more information.\n"),
55 program_name);
56 else
58 printf (_("\
59 Usage: %s [VARIABLE]...\n\
60 or: %s OPTION\n\
61 Print the values of the specified environment VARIABLE(s).\n\
62 If no VARIABLE is specified, print name and value pairs for them all.\n\
63 \n\
64 "),
65 program_name, program_name);
66 fputs (HELP_OPTION_DESCRIPTION, stdout);
67 fputs (VERSION_OPTION_DESCRIPTION, stdout);
68 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
69 emit_bug_reporting_address ();
71 exit (status);
74 int
75 main (int argc, char **argv)
77 char **env;
78 char *ep, *ap;
79 int i;
80 bool ok;
82 initialize_main (&argc, &argv);
83 set_program_name (argv[0]);
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 initialize_exit_failure (PRINTENV_FAILURE);
89 atexit (close_stdout);
91 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
92 usage, AUTHORS, (char const *) NULL);
93 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
94 usage (PRINTENV_FAILURE);
96 if (optind >= argc)
98 for (env = environ; *env != NULL; ++env)
99 puts (*env);
100 ok = true;
102 else
104 int matches = 0;
106 for (i = optind; i < argc; ++i)
108 bool matched = false;
110 for (env = environ; *env; ++env)
112 ep = *env;
113 ap = argv[i];
114 while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
116 if (*ep == '=' && *ap == '\0')
118 puts (ep + 1);
119 matched = true;
120 break;
125 matches += matched;
128 ok = (matches == argc - optind);
131 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);