*** empty log message ***
[coreutils.git] / src / env.c
blob59bd03839d216dc2eb2a00673aa4332f19a7c8c9
1 /* env - run a program in a modified environment
2 Copyright (C) 1986, 1991-2001 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Richard Mlynarik and David MacKenzie */
20 /* Options:
23 --ignore-environment
24 Construct a new environment from scratch; normally the
25 environment is inherited from the parent process, except as
26 modified by other options.
28 -u variable
29 --unset=variable
30 Unset variable VARIABLE (remove it from the environment).
31 If VARIABLE was not set, does nothing.
33 variable=value (an arg containing a "=" character)
34 Set the environment variable VARIABLE to value VALUE. VALUE
35 may be of zero length ("variable="). Setting a variable to a
36 zero-length value is different from unsetting it.
39 Indicate that the following argument is the program
40 to invoke. This is necessary when the program's name
41 begins with "-" or contains a "=".
43 The first remaining argument specifies a program to invoke;
44 it is searched for according to the specification of the PATH
45 environment variable. Any arguments following that are
46 passed as arguments to that program.
48 If no command name is specified following the environment
49 specifications, the resulting environment is printed.
50 This is like specifying a command name of "printenv".
52 Examples:
54 If the environment passed to "env" is
55 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
57 env - foo
58 runs "foo" in a null environment.
60 env foo
61 runs "foo" in the environment
62 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
64 env DISPLAY=gnu:0 nemacs
65 runs "nemacs" in the envionment
66 { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
68 env - LOGNAME=foo /hacks/hack bar baz
69 runs the "hack" program on arguments "bar" and "baz" in an
70 environment in which the only variable is "LOGNAME". Note that
71 the "-" option clears out the PATH variable, so one should be
72 careful to specify in which directory to find the program to
73 call.
75 env -u EDITOR LOGNAME=foo PATH=/energy -- e=mc2 bar baz
76 runs the program "/energy/e=mc2" with environment
77 { LOGNAME=foo PATH=/energy }
80 #include <config.h>
81 #include <stdio.h>
82 #include <getopt.h>
83 #include <sys/types.h>
84 #include <getopt.h>
86 #include "system.h"
87 #include "error.h"
88 #include "closeout.h"
90 /* The official name of this program (e.g., no `g' prefix). */
91 #define PROGRAM_NAME "env"
93 #define AUTHORS N_ ("Richard Mlynarik and David MacKenzie")
95 int putenv ();
97 extern char **environ;
99 /* The name by which this program was run. */
100 char *program_name;
102 static struct option const longopts[] =
104 {"ignore-environment", no_argument, NULL, 'i'},
105 {"unset", required_argument, NULL, 'u'},
106 {GETOPT_HELP_OPTION_DECL},
107 {GETOPT_VERSION_OPTION_DECL},
108 {NULL, 0, NULL, 0}
111 void
112 usage (int status)
114 if (status != 0)
115 fprintf (stderr, _("Try `%s --help' for more information.\n"),
116 program_name);
117 else
119 printf (_("\
120 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
121 program_name);
122 fputs (_("\
123 Set each NAME to VALUE in the environment and run COMMAND.\n\
125 -i, --ignore-environment start with an empty environment\n\
126 -u, --unset=NAME remove variable from the environment\n\
127 "), stdout);
128 fputs (HELP_OPTION_DESCRIPTION, stdout);
129 fputs (VERSION_OPTION_DESCRIPTION, stdout);
130 fputs (_("\
132 A mere - implies -i. If no COMMAND, print the resulting environment.\n\
133 "), stdout);
134 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
136 exit (status);
140 main (register int argc, register char **argv, char **envp)
142 char *dummy_environ[1];
143 int optc;
144 int ignore_environment = 0;
146 program_name = argv[0];
147 setlocale (LC_ALL, "");
148 bindtextdomain (PACKAGE, LOCALEDIR);
149 textdomain (PACKAGE);
151 atexit (close_stdout);
153 while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
155 switch (optc)
157 case 0:
158 break;
159 case 'i':
160 ignore_environment = 1;
161 break;
162 case 'u':
163 break;
164 case_GETOPT_HELP_CHAR;
165 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
166 default:
167 usage (2);
171 if (optind != argc && !strcmp (argv[optind], "-"))
172 ignore_environment = 1;
174 environ = dummy_environ;
175 environ[0] = NULL;
177 if (!ignore_environment)
178 for (; *envp; envp++)
179 putenv (*envp);
181 optind = 0; /* Force GNU getopt to re-initialize. */
182 while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
183 if (optc == 'u')
184 putenv (optarg); /* Requires GNU putenv. */
186 if (optind != argc && !strcmp (argv[optind], "-"))
187 ++optind;
189 while (optind < argc && strchr (argv[optind], '='))
190 putenv (argv[optind++]);
192 /* If no program is specified, print the environment and exit. */
193 if (optind == argc)
195 while (*environ)
196 puts (*environ++);
197 exit (0);
200 execvp (argv[optind], &argv[optind]);
203 int exit_status = (errno == ENOENT ? 127 : 126);
204 error (0, errno, "%s", argv[optind]);
205 exit (exit_status);