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.
24 0 if all variables specified were found
26 2 if some other error occurred
28 David MacKenzie and Richard Mlynarik */
32 #include <sys/types.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"
45 proper_name ("David MacKenzie"), \
46 proper_name ("Richard Mlynarik")
48 extern char **environ
;
53 if (status
!= EXIT_SUCCESS
)
54 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
59 Usage: %s [VARIABLE]...\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\
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 ();
75 main (int argc
, char **argv
)
82 initialize_main (&argc
, &argv
);
83 set_program_name (argv
[0]);
84 setlocale (LC_ALL
, "");
85 bindtextdomain (PACKAGE
, LOCALEDIR
);
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
);
98 for (env
= environ
; *env
!= NULL
; ++env
)
106 for (i
= optind
; i
< argc
; ++i
)
108 bool matched
= false;
110 for (env
= environ
; *env
; ++env
)
114 while (*ep
!= '\0' && *ap
!= '\0' && *ep
++ == *ap
++)
116 if (*ep
== '=' && *ap
== '\0')
128 ok
= (matches
== argc
- optind
);
131 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);