2017-06-14 12:40:53 +08:00
|
|
|
// Implementation of the cd builtin.
|
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "builtin_cd.h"
|
|
|
|
|
2019-06-11 00:27:51 +08:00
|
|
|
#include <fcntl.h>
|
2017-06-14 12:40:53 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2019-11-19 09:11:16 +08:00
|
|
|
#include <cerrno>
|
|
|
|
|
2017-06-14 12:40:53 +08:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "io.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
/// The cd builtin. Changes the current directory to the one specified or to $HOME if none is
|
|
|
|
/// specified. The directory can be relative to any directory in the CDPATH variable.
|
2020-07-19 01:25:43 +08:00
|
|
|
maybe_t<int> builtin_cd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
|
2017-06-14 12:40:53 +08:00
|
|
|
const wchar_t *cmd = argv[0];
|
|
|
|
int argc = builtin_count_args(argv);
|
2017-06-17 11:42:33 +08:00
|
|
|
help_only_cmd_opts_t opts;
|
|
|
|
|
2017-06-14 12:40:53 +08:00
|
|
|
int optind;
|
2017-06-17 11:42:33 +08:00
|
|
|
int retval = parse_help_only_cmd_opts(opts, &optind, argc, argv, parser, streams);
|
2017-06-14 12:40:53 +08:00
|
|
|
if (retval != STATUS_CMD_OK) return retval;
|
|
|
|
|
|
|
|
if (opts.print_help) {
|
2019-10-20 17:38:17 +08:00
|
|
|
builtin_print_help(parser, streams, cmd);
|
2017-06-15 03:26:05 +08:00
|
|
|
return STATUS_CMD_OK;
|
2017-06-14 12:40:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-01 05:38:35 +08:00
|
|
|
wcstring dir_in;
|
2017-06-14 12:40:53 +08:00
|
|
|
if (argv[optind]) {
|
2018-10-01 05:38:35 +08:00
|
|
|
dir_in = argv[optind];
|
2017-06-14 12:40:53 +08:00
|
|
|
} else {
|
2018-09-25 10:26:46 +08:00
|
|
|
auto maybe_dir_in = parser.vars().get(L"HOME");
|
2017-08-28 15:25:41 +08:00
|
|
|
if (maybe_dir_in.missing_or_empty()) {
|
2017-06-14 12:40:53 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Could not find home directory\n"), cmd);
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2018-10-01 05:38:35 +08:00
|
|
|
dir_in = maybe_dir_in->as_string();
|
2017-06-14 12:40:53 +08:00
|
|
|
}
|
|
|
|
|
2018-09-16 19:05:17 +08:00
|
|
|
wcstring pwd = parser.vars().get_pwd_slash();
|
|
|
|
maybe_t<wcstring> mdir = path_get_cdpath(dir_in, pwd, parser.vars());
|
|
|
|
if (!mdir) {
|
2017-06-14 12:40:53 +08:00
|
|
|
if (errno == ENOTDIR) {
|
2018-10-01 05:38:35 +08:00
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd, dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
} else if (errno == ENOENT) {
|
|
|
|
streams.err.append_format(_(L"%ls: The directory '%ls' does not exist\n"), cmd,
|
2018-10-01 05:38:35 +08:00
|
|
|
dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
} else if (errno == EROTTEN) {
|
2018-10-01 05:38:35 +08:00
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is a rotten symlink\n"), cmd, dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
} else {
|
|
|
|
streams.err.append_format(_(L"%ls: Unknown error trying to locate directory '%ls'\n"),
|
2018-10-01 05:38:35 +08:00
|
|
|
cmd, dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
if (!parser.is_interactive()) streams.err.append(parser.current_line());
|
2017-06-14 12:40:53 +08:00
|
|
|
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
2018-09-16 19:05:17 +08:00
|
|
|
const wcstring &dir = *mdir;
|
2017-06-14 12:40:53 +08:00
|
|
|
|
2018-11-18 10:02:28 +08:00
|
|
|
wcstring norm_dir = normalize_path(dir);
|
2018-09-17 09:03:13 +08:00
|
|
|
|
2019-06-11 00:27:51 +08:00
|
|
|
// We need to keep around the fd for this directory, in the parser.
|
|
|
|
autoclose_fd_t dir_fd(wopen_cloexec(norm_dir, O_RDONLY));
|
|
|
|
bool success = dir_fd.valid() && fchdir(dir_fd.fd()) == 0;
|
|
|
|
|
|
|
|
if (!success) {
|
2017-06-14 12:40:53 +08:00
|
|
|
struct stat buffer;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = wstat(dir, &buffer);
|
|
|
|
if (!status && S_ISDIR(buffer.st_mode)) {
|
2018-11-18 10:02:28 +08:00
|
|
|
streams.err.append_format(_(L"%ls: Permission denied: '%ls'\n"), cmd, dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
} else {
|
2018-11-18 10:02:28 +08:00
|
|
|
streams.err.append_format(_(L"%ls: '%ls' is not a directory\n"), cmd, dir_in.c_str());
|
2017-06-14 12:40:53 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
if (!parser.is_interactive()) {
|
2017-06-14 12:40:53 +08:00
|
|
|
streams.err.append(parser.current_line());
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-06-11 00:27:51 +08:00
|
|
|
parser.libdata().cwd_fd = std::make_shared<const autoclose_fd_t>(std::move(dir_fd));
|
2019-06-03 17:31:13 +08:00
|
|
|
std::vector<event_t> evts;
|
|
|
|
parser.vars().set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, std::move(norm_dir), &evts);
|
|
|
|
for (const auto &evt : evts) {
|
|
|
|
event_fire(parser, evt);
|
|
|
|
}
|
2017-06-14 12:40:53 +08:00
|
|
|
return STATUS_CMD_OK;
|
|
|
|
}
|