Fix handling of '-' option and way that variables are added to the

environment from Jonas Holmberg <jonas.holmberg@axis.com>.  Fix
handling of command options by adding + to getopt string.
This commit is contained in:
Matt Kraai 2001-05-11 14:26:29 +00:00
parent f3e79ba6e3
commit 5b44f48afc
2 changed files with 28 additions and 12 deletions

View File

@ -35,14 +35,13 @@ extern int env_main(int argc, char** argv)
{ {
char **ep, *p; char **ep, *p;
char *cleanenv[1]; char *cleanenv[1];
int ignore_environment = 0;
int ch; int ch;
while ((ch = getopt(argc, argv, "-iu:")) != -1) while ((ch = getopt(argc, argv, "+iu:")) != -1) {
switch(ch) { switch(ch) {
case '-':
case 'i': case 'i':
environ = cleanenv; ignore_environment = 1;
cleanenv[0] = NULL;
break; break;
case 'u': case 'u':
unsetenv(optarg); unsetenv(optarg);
@ -50,15 +49,24 @@ extern int env_main(int argc, char** argv)
default: default:
show_usage(); show_usage();
} }
}
if (optind != argc && !strcmp(argv[optind], "-")) {
ignore_environment = 1;
argv++;
}
if (ignore_environment) {
environ = cleanenv;
cleanenv[0] = NULL;
}
for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
setenv(*argv, ++p, 1); putenv(*argv);
if (*argv) { if (*argv) {
execvp(*argv, argv); execvp(*argv, argv);
perror_msg_and_die("%s", *argv); perror_msg_and_die("%s", *argv);
} }
for (ep = environ; *ep; ep++) for (ep = environ; *ep; ep++)
printf("%s\n", *ep); printf("%s\n", *ep);
exit(EXIT_SUCCESS); return 0;
} }
/* /*

20
env.c
View File

@ -35,14 +35,13 @@ extern int env_main(int argc, char** argv)
{ {
char **ep, *p; char **ep, *p;
char *cleanenv[1]; char *cleanenv[1];
int ignore_environment = 0;
int ch; int ch;
while ((ch = getopt(argc, argv, "-iu:")) != -1) while ((ch = getopt(argc, argv, "+iu:")) != -1) {
switch(ch) { switch(ch) {
case '-':
case 'i': case 'i':
environ = cleanenv; ignore_environment = 1;
cleanenv[0] = NULL;
break; break;
case 'u': case 'u':
unsetenv(optarg); unsetenv(optarg);
@ -50,15 +49,24 @@ extern int env_main(int argc, char** argv)
default: default:
show_usage(); show_usage();
} }
}
if (optind != argc && !strcmp(argv[optind], "-")) {
ignore_environment = 1;
argv++;
}
if (ignore_environment) {
environ = cleanenv;
cleanenv[0] = NULL;
}
for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
setenv(*argv, ++p, 1); putenv(*argv);
if (*argv) { if (*argv) {
execvp(*argv, argv); execvp(*argv, argv);
perror_msg_and_die("%s", *argv); perror_msg_and_die("%s", *argv);
} }
for (ep = environ; *ep; ep++) for (ep = environ; *ep; ep++)
printf("%s\n", *ep); printf("%s\n", *ep);
exit(EXIT_SUCCESS); return 0;
} }
/* /*