use filesystem::current_path for builtin_directoery

This commit is contained in:
Kelvin Sherlock 2016-02-11 20:50:24 -05:00
parent 034321830f
commit 7d18720162

View File

@ -375,25 +375,29 @@ int builtin_directory(Environment &env, const std::vector<std::string> &tokens,
} }
// todo -- if relative path does not exist, check {DirectoryPath} // todo -- if relative path does not exist, check {DirectoryPath}
std::string path = ToolBox::MacToUnix(argv.front()); fs::path path = ToolBox::MacToUnix(argv.front());
int ok = chdir(path.c_str()); std::error_code ec;
if (ok < 0) { current_path(path, ec);
if (ec) {
fputs("### Directory - Unable to set current directory.\n", stderr); fputs("### Directory - Unable to set current directory.\n", stderr);
perror("chdir: "); fprintf(stderr, "# %s\n", ec.message().c_str());
return 1; return 1;
} }
} }
else { else {
// pwd // pwd
char buffer[MAXPATHLEN]; std::error_code ec;
char *cp = getcwd(buffer, sizeof(buffer)); fs::path path = fs::current_path(ec);
if (!cp) { if (ec) {
perror("getcwd: ");
fputs("### Directory - Unable to get current directory.\n", stderr);
fprintf(stderr, "# %s\n", ec.message().c_str());
return 1; return 1;
} }
// todo -- pathname translation? // todo -- pathname translation?
std::string s = buffer; std::string s = path;
if (!q) s = quote(std::move(s)); if (!q) s = quote(std::move(s));
fprintf(stdout, "%s\n", s.c_str()); fprintf(stdout, "%s\n", s.c_str());
} }
@ -579,7 +583,7 @@ int builtin_which(Environment &env, const std::vector<std::string> &tokens, cons
return 2; return 2;
} }
} }
for(; ss; ++ss) { for(; ss; ++ss) {
if (p) fprintf(stderr, "checking %s\n", ss->c_str()); if (p) fprintf(stderr, "checking %s\n", ss->c_str());