env: don't SEGV on bare "env -"

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-11-12 14:52:47 +01:00
parent ab19ede655
commit f4fee418ae

View File

@ -43,21 +43,20 @@ static const char env_longopts[] ALIGN1 =
int env_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int env_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int env_main(int argc UNUSED_PARAM, char **argv) int env_main(int argc UNUSED_PARAM, char **argv)
{ {
char **ep; unsigned opts;
unsigned opt;
llist_t *unset_env = NULL; llist_t *unset_env = NULL;
opt_complementary = "u::"; opt_complementary = "u::";
#if ENABLE_FEATURE_ENV_LONG_OPTIONS #if ENABLE_FEATURE_ENV_LONG_OPTIONS
applet_long_options = env_longopts; applet_long_options = env_longopts;
#endif #endif
opt = getopt32(argv, "+iu:", &unset_env); opts = getopt32(argv, "+iu:", &unset_env);
argv += optind; argv += optind;
if (*argv && LONE_DASH(argv[0])) { if (argv[0] && LONE_DASH(argv[0])) {
opt |= 1; opts |= 1;
++argv; ++argv;
} }
if (opt & 1) { if (opts & 1) {
clearenv(); clearenv();
} }
while (unset_env) { while (unset_env) {
@ -84,9 +83,12 @@ int env_main(int argc UNUSED_PARAM, char **argv)
bb_simple_perror_msg_and_die(*argv); bb_simple_perror_msg_and_die(*argv);
} }
if (environ) { /* clearenv() may set environ == NULL! */
char **ep;
for (ep = environ; *ep; ep++) { for (ep = environ; *ep; ep++) {
puts(*ep); puts(*ep);
} }
}
fflush_stdout_and_exit(EXIT_SUCCESS); fflush_stdout_and_exit(EXIT_SUCCESS);
} }