1 /* env - manipulate environment and execute a program in that environment
2 Copyright (C) 1986 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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* If first argument is "-", then a new environment is constructed
21 from scratch; otherwise the environment is inherited from the parent
22 process, except as modified by other options.
24 So, "env - foo" will invoke the "foo" program in a null environment,
25 whereas "env foo" would invoke "foo" in the same environment as that
26 passed to "env" itself.
28 Subsequent arguments are interpreted as follows:
30 * "variable=value" (i.e., an arg containing a "=" character)
31 means to set the specified environment variable to that value.
32 `value' may be of zero length ("variable="). Note that setting
33 a variable to a zero-length value is different from unsetting it.
35 * "-u variable" or "-unset variable"
36 means to unset that variable.
37 If that variable isn't set, does nothing.
39 * "-s variable value" or "-set variable value"
40 same as "variable=value".
43 are used to indicate that the following argument is the program
44 to invoke. This is only necessary when the program's name
45 begins with "-" or contains a "=".
48 The first remaining argument specifies a program to invoke
49 (it is searched for according to the specification of the PATH
50 environment variable) and any arguments following that are
51 passed as arguments to that program.
53 If no program-name is specified following the environment
54 specifications, the resulting environment is printed.
55 This is like specifying a program-name of "printenv".
58 If the environment passed to "env" is
59 { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
61 * "env DISPLAY=gnu:0 nemacs"
62 calls "nemacs" in the envionment
63 { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
65 * "env - USER=foo /hacks/hack bar baz"
66 calls the "hack" program on arguments "bar" and "baz"
67 in an environment in which the only variable is "USER".
68 Note that the "-" option clears out the PATH variable,
69 so one should be careful to specify in which directory
70 to find the program to call.
72 * "env -u EDITOR USER=foo PATH=/energy -- e=mc2 bar baz"
73 The program "/energy/e=mc2" is called with environment
74 { USER=foo PATH=/energy }
79 #include "../src/config.h"
86 char *xmalloc (), *xrealloc ();
89 extern char **environ
;
99 main (argc
, argv
, envp
)
101 register char **argv
;
111 nenv
= (char **) xmalloc (nenv_size
* sizeof (char *));
114 /* "-" flag means to not inherit parent's environment */
115 if (argc
&& !strcmp (*argv
, "-"))
121 /* Else pass on existing env vars. */
122 for (; *envp
; envp
++)
124 tem
= myindex (*envp
, '=');
128 setenv (*envp
, tem
+ 1);
134 tem
= myindex (*argv
, '=');
136 /* If arg contains a "=" it specifies to set a variable */
139 setenv (*argv
, tem
+ 1);
146 /* Remaining args are program name and args to pass it */
150 fatal ("no argument for `%s' option", *argv
);
151 if (!strcmp (*argv
, "-u")
152 || !strcmp (*argv
, "-unset"))
153 /* Unset a variable */
157 setenv (*argv
, (char *) 0);
161 else if (!strcmp (*argv
, "-s") ||
162 !strcmp (*argv
, "-set"))
169 fatal ("no value specified for variable \"%s\"", tem
);
176 else if (!strcmp (*argv
, "-") || !strcmp (*argv
, "--"))
184 fatal ("unrecognized option `%s'", *argv
);
188 /* If no program specified print the environment and exit */
192 printf ("%s\n", *nenv
++);
197 extern int errno
, sys_nerr
;
198 extern char *sys_errlist
[];
201 (void) execvp (*argv
, argv
);
203 fprintf (stderr
, "%s: cannot execute `%s'", progname
, *argv
);
204 if (errno
< sys_nerr
)
205 fprintf (stderr
, ": %s\n", sys_errlist
[errno
]);
208 exit (errno
!= 0 ? errno
: 1);
214 register char *var
, *val
;
217 int len
= strlen (var
);
220 register char *tem
= myindex (var
, '=');
222 fatal ("environment variable names can not contain `=': %s", var
);
223 else if (*var
== '\000')
224 fatal ("zero-length environment variable name specified");
227 for (e
= nenv
; *e
; e
++)
228 if (!strncmp (var
, *e
, len
) && (*e
)[len
] == '=')
241 return; /* Nothing to unset */
244 if (len
+ 1 >= nenv_size
)
247 nenv
= (char **) xrealloc (nenv
, nenv_size
* sizeof (char *));
252 val
= concat (var
, "=", val
);
256 *(e
+ 1) = (char *) 0;
262 fatal (msg
, arg1
, arg2
)
263 char *msg
, *arg1
, *arg2
;
265 fprintf (stderr
, "%s: ", progname
);
266 fprintf (stderr
, msg
, arg1
, arg2
);
272 extern char *malloc (), *realloc ();
277 fatal ("virtual memory exhausted");
284 register char *value
;
285 value
= (char *) malloc (size
);
296 register char *value
;
297 value
= (char *) realloc (ptr
, size
);
303 /* Return a newly-allocated string whose contents concatenate
304 those of S1, S2, S3. */
310 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
311 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
314 strcpy (result
+ len1
, s2
);
315 strcpy (result
+ len1
+ len2
, s3
);
316 result
[len1
+ len2
+ len3
] = 0;
321 /* Return a pointer to the first occurrence in STR of C,
322 or 0 if C does not occur. */