Tizen 2.0 Release
[external/tizen-coreutils.git] / src / env.c
1 /* env - run a program in a modified environment
2    Copyright (C) 1986, 1991-2005, 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Richard Mlynarik and David MacKenzie */
19
20 /* Options:
21    -
22    -i
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.
27
28    -u variable
29    --unset=variable
30         Unset variable VARIABLE (remove it from the environment).
31         If VARIABLE was not set, does nothing.
32
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.
37
38    --
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 "=".
42
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.
47
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".
51
52    Examples:
53
54    If the environment passed to "env" is
55         { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
56
57    env - foo
58         runs "foo" in a null environment.
59
60    env foo
61         runs "foo" in the environment
62         { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
63
64    env DISPLAY=gnu:0 nemacs
65         runs "nemacs" in the environment
66         { LOGNAME=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
67
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.
74
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 }
78 */
79
80 #include <config.h>
81 #include <stdio.h>
82 #include <getopt.h>
83 #include <sys/types.h>
84 #include <getopt.h>
85
86 #include "system.h"
87 #include "error.h"
88
89 /* The official name of this program (e.g., no `g' prefix).  */
90 #define PROGRAM_NAME "env"
91
92 #define AUTHORS "Richard Mlynarik", "David MacKenzie"
93
94 int putenv ();
95
96 extern char **environ;
97
98 /* The name by which this program was run. */
99 char *program_name;
100
101 static struct option const longopts[] =
102 {
103   {"ignore-environment", no_argument, NULL, 'i'},
104   {"unset", required_argument, NULL, 'u'},
105   {GETOPT_HELP_OPTION_DECL},
106   {GETOPT_VERSION_OPTION_DECL},
107   {NULL, 0, NULL, 0}
108 };
109
110 void
111 usage (int status)
112 {
113   if (status != EXIT_SUCCESS)
114     fprintf (stderr, _("Try `%s --help' for more information.\n"),
115              program_name);
116   else
117     {
118       printf (_("\
119 Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]\n"),
120               program_name);
121       fputs (_("\
122 Set each NAME to VALUE in the environment and run COMMAND.\n\
123 \n\
124   -i, --ignore-environment   start with an empty environment\n\
125   -u, --unset=NAME           remove variable from the environment\n\
126 "), stdout);
127       fputs (HELP_OPTION_DESCRIPTION, stdout);
128       fputs (VERSION_OPTION_DESCRIPTION, stdout);
129       fputs (_("\
130 \n\
131 A mere - implies -i.  If no COMMAND, print the resulting environment.\n\
132 "), stdout);
133       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
134     }
135   exit (status);
136 }
137
138 int
139 main (int argc, char **argv)
140 {
141   int optc;
142   bool ignore_environment = false;
143
144   initialize_main (&argc, &argv);
145   program_name = argv[0];
146   setlocale (LC_ALL, "");
147   bindtextdomain (PACKAGE, LOCALEDIR);
148   textdomain (PACKAGE);
149
150   initialize_exit_failure (EXIT_FAIL);
151   atexit (close_stdout);
152
153   while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
154     {
155       switch (optc)
156         {
157         case 'i':
158           ignore_environment = true;
159           break;
160         case 'u':
161           break;
162         case_GETOPT_HELP_CHAR;
163         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
164         default:
165           usage (EXIT_FAIL);
166         }
167     }
168
169   if (optind < argc && STREQ (argv[optind], "-"))
170     ignore_environment = true;
171
172   if (ignore_environment)
173     {
174       static char *dummy_environ[] = { NULL };
175       environ = dummy_environ;
176     }
177
178   optind = 0;                   /* Force GNU getopt to re-initialize. */
179   while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
180     if (optc == 'u')
181       putenv (optarg);          /* Requires GNU putenv. */
182
183   if (optind < argc && STREQ (argv[optind], "-"))
184     ++optind;
185
186   while (optind < argc && strchr (argv[optind], '='))
187     putenv (argv[optind++]);
188
189   /* If no program is specified, print the environment and exit. */
190   if (argc <= optind)
191     {
192       char *const *e = environ;
193       while (*e)
194         puts (*e++);
195       exit (EXIT_SUCCESS);
196     }
197
198   execvp (argv[optind], &argv[optind]);
199
200   {
201     int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
202     error (0, errno, "%s", argv[optind]);
203     exit (exit_status);
204   }
205 }