version 7.3
[coreutils.git] / src / env.c
blobfb9d7932d4499567db77453e7c1c3cfe09d5bcd0
1 /* env - run a program in a modified environment
2 Copyright (C) 1986, 1991-2005, 2007-2008 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 /* Richard Mlynarik and David MacKenzie */
19 /* Options:
22 --ignore-environment
23 Construct a new environment from scratch; normally the
24 environment is inherited from the parent process, except as
25 modified by other options.
27 -u variable
28 --unset=variable
29 Unset variable VARIABLE (remove it from the environment).
30 If VARIABLE was not set, does nothing.
32 variable=value (an arg containing a "=" character)
33 Set the environment variable VARIABLE to value VALUE. VALUE
34 may be of zero length ("variable="). Setting a variable to a
35 zero-length value is different from unsetting it.
38 Indicate that the following argument is the program
39 to invoke. This is necessary when the program's name
40 begins with "-" or contains a "=".
42 The first remaining argument specifies a program to invoke;
43 it is searched for according to the specification of the PATH
44 environment variable. Any arguments following that are
45 passed as arguments to that program.
47 If no command name is specified following the environment
48 specifications, the resulting environment is printed.
49 This is like specifying a command name of "printenv".
51 Examples:
53 If the environment passed to "env" is
54 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
56 env - foo
57 runs "foo" in a null environment.
59 env foo
60 runs "foo" in the environment
61 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
63 env DISPLAY=gnu:0 nemacs
64 runs "nemacs" in the environment
65 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
67 env - LOGNAME=foo /hacks/hack bar baz
68 runs the "hack" program on arguments "bar" and "baz" in an
69 environment in which the only variable is "LOGNAME". Note that
70 the "-" option clears out the PATH variable, so one should be
71 careful to specify in which directory to find the program to
72 call.
74 env -u EDITOR LOGNAME=foo PATH=/energy -- e=mc2 bar baz
75 runs the program "/energy/e=mc2" with environment
76 { LOGNAME=foo PATH=/energy }
79 #include <config.h>
80 #include <stdio.h>
81 #include <sys/types.h>
82 #include <getopt.h>
84 #include "system.h"
85 #include "error.h"
87 /* The official name of this program (e.g., no `g' prefix). */
88 #define PROGRAM_NAME "env"
90 #define AUTHORS \
91 proper_name ("Richard Mlynarik"), \
92 proper_name ("David MacKenzie")
94 extern char **environ;
96 static struct option const longopts[] =
98 {"ignore-environment", no_argument, NULL, 'i'},
99 {"unset", required_argument, NULL, 'u'},
100 {GETOPT_HELP_OPTION_DECL},
101 {GETOPT_VERSION_OPTION_DECL},
102 {NULL, 0, NULL, 0}
105 void
106 usage (int status)
108 if (status != EXIT_SUCCESS)
109 fprintf (stderr, _("Try `%s --help' for more information.\n"),
110 program_name);
111 else
113 printf (_("\
114 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
115 program_name);
116 fputs (_("\
117 Set each NAME to VALUE in the environment and run COMMAND.\n\
119 -i, --ignore-environment start with an empty environment\n\
120 -u, --unset=NAME remove variable from the environment\n\
121 "), stdout);
122 fputs (HELP_OPTION_DESCRIPTION, stdout);
123 fputs (VERSION_OPTION_DESCRIPTION, stdout);
124 fputs (_("\
126 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
127 "), stdout);
128 emit_bug_reporting_address ();
130 exit (status);
134 main (int argc, char **argv)
136 int optc;
137 bool ignore_environment = false;
139 initialize_main (&argc, &argv);
140 set_program_name (argv[0]);
141 setlocale (LC_ALL, "");
142 bindtextdomain (PACKAGE, LOCALEDIR);
143 textdomain (PACKAGE);
145 initialize_exit_failure (EXIT_FAILURE);
146 atexit (close_stdout);
148 while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
150 switch (optc)
152 case 'i':
153 ignore_environment = true;
154 break;
155 case 'u':
156 break;
157 case_GETOPT_HELP_CHAR;
158 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
159 default:
160 usage (EXIT_FAILURE);
164 if (optind < argc && STREQ (argv[optind], "-"))
165 ignore_environment = true;
167 if (ignore_environment)
169 static char *dummy_environ[] = { NULL };
170 environ = dummy_environ;
173 optind = 0; /* Force GNU getopt to re-initialize. */
174 while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
175 if (optc == 'u')
176 putenv (optarg); /* Requires GNU putenv. */
178 if (optind < argc && STREQ (argv[optind], "-"))
179 ++optind;
181 while (optind < argc && strchr (argv[optind], '='))
182 putenv (argv[optind++]);
184 /* If no program is specified, print the environment and exit. */
185 if (argc <= optind)
187 char *const *e = environ;
188 while (*e)
189 puts (*e++);
190 exit (EXIT_SUCCESS);
193 execvp (argv[optind], &argv[optind]);
196 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
197 error (0, errno, "%s", argv[optind]);
198 exit (exit_status);