TODO: add an item for a chmod optimization
[platform/upstream/coreutils.git] / src / printenv.c
1 /* printenv -- print all or part of environment
2    Copyright (C) 1989-1997, 1999-2005, 2007-2008 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Usage: printenv [variable...]
18
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.
22
23    Exit status:
24    0 if all variables specified were found
25    1 if not
26    2 if some other error occurred
27
28    David MacKenzie and Richard Mlynarik */
29
30 #include <config.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
34
35 #include "system.h"
36 #include "long-options.h"
37
38 /* Exit status for syntax errors, etc.  */
39 enum { PRINTENV_FAILURE = 2 };
40
41 /* The official name of this program (e.g., no `g' prefix).  */
42 #define PROGRAM_NAME "printenv"
43
44 #define AUTHORS \
45   proper_name ("David MacKenzie"), \
46   proper_name ("Richard Mlynarik")
47
48 extern char **environ;
49
50 void
51 usage (int status)
52 {
53   if (status != EXIT_SUCCESS)
54     fprintf (stderr, _("Try `%s --help' for more information.\n"),
55              program_name);
56   else
57     {
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 ();
70     }
71   exit (status);
72 }
73
74 int
75 main (int argc, char **argv)
76 {
77   char **env;
78   char *ep, *ap;
79   int i;
80   bool ok;
81
82   initialize_main (&argc, &argv);
83   set_program_name (argv[0]);
84   setlocale (LC_ALL, "");
85   bindtextdomain (PACKAGE, LOCALEDIR);
86   textdomain (PACKAGE);
87
88   initialize_exit_failure (PRINTENV_FAILURE);
89   atexit (close_stdout);
90
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);
95
96   if (optind >= argc)
97     {
98       for (env = environ; *env != NULL; ++env)
99         puts (*env);
100       ok = true;
101     }
102   else
103     {
104       int matches = 0;
105
106       for (i = optind; i < argc; ++i)
107         {
108           bool matched = false;
109
110           for (env = environ; *env; ++env)
111             {
112               ep = *env;
113               ap = argv[i];
114               while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
115                 {
116                   if (*ep == '=' && *ap == '\0')
117                     {
118                       puts (ep + 1);
119                       matched = true;
120                       break;
121                     }
122                 }
123             }
124
125           matches += matched;
126         }
127
128       ok = (matches == argc - optind);
129     }
130
131   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
132 }