diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c --- a/usr.bin/env/env.c +++ b/usr.bin/env/env.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { - char *altpath, **ep, *p, **parg, term; + char *altpath, *altwd, **ep, *p, **parg, term; char *cleanenv[1]; char *login_class, *login_name; struct passwd *pw; @@ -70,6 +70,7 @@ int rtrn; altpath = NULL; + altwd = NULL; login_class = NULL; login_name = NULL; pw = NULL; @@ -77,7 +78,7 @@ login_as_user = false; want_clear = 0; term = '\n'; - while ((ch = getopt(argc, argv, "-0iL:P:S:U:u:v")) != -1) + while ((ch = getopt(argc, argv, "-0C:iL:P:S:U:u:v")) != -1) switch(ch) { case '-': case 'i': @@ -86,6 +87,9 @@ case '0': term = '\0'; break; + case 'C': + altwd = optarg; + break; case 'U': login_as_user = true; /* FALLTHROUGH */ @@ -93,7 +97,7 @@ login_name = optarg; break; case 'P': - altpath = strdup(optarg); + altpath = optarg; break; case 'S': /* @@ -186,6 +190,9 @@ if (*argv) { if (term == '\0') errx(EXIT_CANCELED, "cannot specify command with -0"); + if (altwd && chdir(altwd) != 0) + err(EXIT_CANCELED, "cannot change directory to '%s'", + altwd); if (altpath) search_paths(altpath, argv); if (env_verbosity) { @@ -209,7 +216,7 @@ usage(void) { (void)fprintf(stderr, - "usage: env [-0iv] [-L|-U user[/class]] [-P utilpath] [-S string] [-u name]\n" - " [name=value ...] [utility [argument ...]]\n"); + "usage: env [-0iv] [-C workdir] [-L|-U user[/class]] [-P utilpath] [-S string]\n" + " [-u name] [name=value ...] [utility [argument ...]]\n"); exit(1); } diff --git a/usr.bin/env/tests/env_test.sh b/usr.bin/env/tests/env_test.sh --- a/usr.bin/env/tests/env_test.sh +++ b/usr.bin/env/tests/env_test.sh @@ -108,6 +108,24 @@ env PATH="${PATH}:${PWD}" command "magic=words" } +atf_test_case chdir +chdir_head() +{ + atf_set "descr" "Change working directory" +} +chdir_body() +{ + local subdir="dir.$$" + atf_check -o inline:"${PWD}\n" \ + env pwd + atf_check -s exit:125 \ + -e match:"cannot change directory to '${subdir}':" \ + env -C "${subdir}" pwd + atf_check mkdir "${subdir}" + atf_check -o inline:"${PWD}/${subdir}\n" \ + env -C "${subdir}" pwd +} + atf_init_test_cases() { atf_add_test_case basic @@ -117,4 +135,5 @@ atf_add_test_case false atf_add_test_case altpath atf_add_test_case equal + atf_add_test_case chdir }