Line data Source code
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 "David MacKenzie", "Richard Mlynarik"
45 :
46 : /* The name this program was run with. */
47 : char *program_name;
48 :
49 : extern char **environ;
50 :
51 : void
52 8 : usage (int status)
53 : {
54 8 : if (status != EXIT_SUCCESS)
55 6 : fprintf (stderr, _("Try `%s --help' for more information.\n"),
56 : program_name);
57 : else
58 : {
59 2 : printf (_("\
60 : Usage: %s [VARIABLE]...\n\
61 : or: %s OPTION\n\
62 : If no environment VARIABLE specified, print them all.\n\
63 : \n\
64 : "),
65 : program_name, program_name);
66 2 : fputs (HELP_OPTION_DESCRIPTION, stdout);
67 2 : fputs (VERSION_OPTION_DESCRIPTION, stdout);
68 2 : printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
69 2 : emit_bug_reporting_address ();
70 : }
71 8 : exit (status);
72 : }
73 :
74 : int
75 21 : 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 21 : program_name = argv[0];
84 21 : setlocale (LC_ALL, "");
85 : bindtextdomain (PACKAGE, LOCALEDIR);
86 : textdomain (PACKAGE);
87 :
88 21 : initialize_exit_failure (PRINTENV_FAILURE);
89 21 : atexit (close_stdout);
90 :
91 21 : parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
92 : usage, AUTHORS, (char const *) NULL);
93 18 : if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
94 6 : usage (PRINTENV_FAILURE);
95 :
96 12 : if (optind >= argc)
97 : {
98 52 : for (env = environ; *env != NULL; ++env)
99 50 : puts (*env);
100 2 : ok = true;
101 : }
102 : else
103 : {
104 10 : int matches = 0;
105 :
106 27 : for (i = optind; i < argc; ++i)
107 : {
108 17 : bool matched = false;
109 :
110 442 : for (env = environ; *env; ++env)
111 : {
112 425 : ep = *env;
113 425 : ap = argv[i];
114 866 : while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
115 : {
116 17 : if (*ep == '=' && *ap == '\0')
117 : {
118 1 : puts (ep + 1);
119 1 : matched = true;
120 1 : break;
121 : }
122 : }
123 : }
124 :
125 17 : matches += matched;
126 : }
127 :
128 10 : ok = (matches == argc - optind);
129 : }
130 :
131 12 : exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
132 : }
|