diff options
Diffstat (limited to 'coreutils/env.c')
-rw-r--r-- | coreutils/env.c | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/coreutils/env.c b/coreutils/env.c index 8bb690b72..db13b3aed 100644 --- a/coreutils/env.c +++ b/coreutils/env.c | |||
@@ -24,50 +24,66 @@ | |||
24 | * Modified for BusyBox by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> | 24 | * Modified for BusyBox by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
25 | */ | 25 | */ |
26 | 26 | ||
27 | /* BB_AUDIT SUSv3 compliant */ | ||
28 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/env.html */ | ||
29 | |||
30 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) | ||
31 | * | ||
32 | * Fixed bug involving exit return codes if execvp fails. Also added | ||
33 | * output error checking. | ||
34 | */ | ||
35 | |||
27 | #include <stdio.h> | 36 | #include <stdio.h> |
28 | #include <string.h> | 37 | #include <string.h> |
29 | #include <getopt.h> | ||
30 | #include <stdlib.h> | 38 | #include <stdlib.h> |
39 | #include <errno.h> | ||
31 | #include <unistd.h> | 40 | #include <unistd.h> |
32 | #include "busybox.h" | 41 | #include "busybox.h" |
33 | 42 | ||
34 | extern int env_main(int argc, char** argv) | 43 | extern int env_main(int argc, char** argv) |
35 | { | 44 | { |
36 | char **ep, *p; | 45 | char **ep, *p; |
37 | char *cleanenv[1]; | 46 | char *cleanenv[1] = { NULL }; |
38 | int ignore_environment = 0; | ||
39 | int ch; | 47 | int ch; |
40 | 48 | ||
41 | while ((ch = getopt(argc, argv, "+iu:")) != -1) { | 49 | while ((ch = getopt(argc, argv, "iu:")) > 0) { |
42 | switch(ch) { | 50 | switch(ch) { |
43 | case 'i': | 51 | case 'i': |
44 | ignore_environment = 1; | 52 | environ = cleanenv; |
45 | break; | 53 | break; |
46 | case 'u': | 54 | case 'u': |
47 | unsetenv(optarg); | 55 | unsetenv(optarg); |
48 | break; | 56 | break; |
49 | default: | 57 | default: |
50 | show_usage(); | 58 | bb_show_usage(); |
51 | } | 59 | } |
52 | } | 60 | } |
53 | if (optind != argc && !strcmp(argv[optind], "-")) { | 61 | |
54 | ignore_environment = 1; | 62 | argv += optind; |
55 | argv++; | 63 | |
56 | } | 64 | if (*argv && (argv[0][0] == '-') && !argv[0][1]) { |
57 | if (ignore_environment) { | ||
58 | environ = cleanenv; | 65 | environ = cleanenv; |
59 | cleanenv[0] = NULL; | 66 | ++argv; |
60 | } | 67 | } |
61 | for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) | 68 | |
62 | if (putenv(*argv) < 0) | 69 | while (*argv && ((p = strchr(*argv, '=')) != NULL)) { |
63 | perror_msg_and_die("%s", *argv); | 70 | if (putenv(*argv) < 0) { |
71 | bb_perror_msg_and_die("putenv"); | ||
72 | } | ||
73 | ++argv; | ||
74 | } | ||
75 | |||
64 | if (*argv) { | 76 | if (*argv) { |
65 | execvp(*argv, argv); | 77 | execvp(*argv, argv); |
66 | perror_msg_and_die("%s", *argv); | 78 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
79 | return (errno == ENOENT) ? 127 : 126; /* SUSv3-mandated exit codes. */ | ||
67 | } | 80 | } |
68 | for (ep = environ; *ep; ep++) | 81 | |
82 | for (ep = environ; *ep; ep++) { | ||
69 | puts(*ep); | 83 | puts(*ep); |
70 | return 0; | 84 | } |
85 | |||
86 | bb_fflush_stdout_and_exit(0); | ||
71 | } | 87 | } |
72 | 88 | ||
73 | /* | 89 | /* |