1 /* env 1.1 - Set environment for command Author: Kees J. Bot
12 int main(int argc
, char **argv
)
17 extern char **environ
;
20 while (i
< argc
&& argv
[i
][0] == '-') {
21 char *opt
= argv
[i
++] + 1;
23 if (opt
[0] == '-' && opt
[1] == 0) break; /* -- */
25 if (opt
[0] == 0) iflag
= 1; /* - */
27 while (*opt
!= 0) switch (*opt
++) {
29 iflag
= 1; /* Clear environment. */
31 case 'a': /* Specify arg 0 separately. */
36 "Usage: env [-ia] [name=value] ... [utility [argument ...]]\n");
41 /* Clear the environment if -i. */
42 if (iflag
) *environ
= nil
;
44 /* Set the new environment strings. */
45 while (i
< argc
&& strchr(argv
[i
], '=') != nil
) {
46 if (putenv(argv
[i
]) != 0) {
47 fprintf(stderr
, "env: Setting '%s' failed: %s\n",
48 argv
[i
], strerror(errno
));
54 /* Environment settings and command may be separated with '--'.
55 * This is for compatibility with other envs, we don't advertise it.
57 if (i
< argc
&& strcmp(argv
[i
], "--") == 0) i
++;
60 /* No utility given; print environment. */
63 for (ep
= environ
; *ep
!= nil
; ep
++) {
64 if (puts(*ep
) == EOF
) {
65 fprintf(stderr
, "env: %s\n", strerror(errno
));
77 (void) execvp(util
, args
);
79 fprintf(stderr
, "env: Can't execute %s: %s\n",
81 return err
== ENOENT
? 127 : 126;