diff --git a/main.c b/main.c index ee9900cb..d3bef78a 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -217,6 +218,16 @@ main(int argc, char *const *argv) struct server *server = NULL; struct shutdown_context shutdown_ctx = {.term = &term, .exit_code = EXIT_FAILURE}; + char *cwd = NULL; + { + size_t buf_len = 1024; + do { + cwd = realloc(cwd, buf_len); + getcwd(cwd, buf_len); + buf_len *= 2; + } while (errno == ERANGE); + } + if ((fdm = fdm_init()) == NULL) goto out; @@ -224,9 +235,12 @@ main(int argc, char *const *argv) goto out; if (!as_server && (term = term_init( - &conf, fdm, wayl, conf.term, "foot", argc, argv, - &term_shutdown_cb, &shutdown_ctx)) == NULL) + &conf, fdm, wayl, conf.term, "foot", cwd, argc, argv, + &term_shutdown_cb, &shutdown_ctx)) == NULL) { + free(cwd); goto out; + } + free(cwd); if (as_server && (server = server_init(&conf, fdm, wayl)) == NULL) goto out; diff --git a/server.c b/server.c index 6f1244fe..e3731bfb 100644 --- a/server.c +++ b/server.c @@ -220,7 +220,7 @@ fdm_client(struct fdm *fdm, int fd, int events, void *data) client->term = term_init( server->conf, server->fdm, server->wayl, strlen(term_env) > 0 ? term_env : server->conf->term, - "footclient", argc, argv, &term_shutdown_handler, client); + "footclient", cwd, argc, argv, &term_shutdown_handler, client); if (client->term == NULL) { LOG_ERR("failed to instantiate new terminal"); diff --git a/slave.c b/slave.c index bffa48a8..857640b2 100644 --- a/slave.c +++ b/slave.c @@ -69,7 +69,7 @@ err: } pid_t -slave_spawn(int ptmx, int argc, char *const *argv, +slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, const char *term_env, const char *conf_shell) { int fork_pipe[2]; @@ -90,6 +90,8 @@ slave_spawn(int ptmx, int argc, char *const *argv, /* Child */ close(fork_pipe[0]); /* Close read end */ + chdir(cwd); + /* Restore signals */ const struct sigaction sa = {.sa_handler = SIG_DFL}; if (sigaction(SIGINT, &sa, NULL) < 0 || diff --git a/slave.h b/slave.h index f1a9848a..63fe9bff 100644 --- a/slave.h +++ b/slave.h @@ -4,4 +4,5 @@ #include pid_t slave_spawn( - int ptmx, int argc, char *const *argv, const char *term_env, const char *conf_shell); + int ptmx, int argc, const char *cwd, char *const *argv, const char *term_env, + const char *conf_shell); diff --git a/terminal.c b/terminal.c index f7f98407..eb87661c 100644 --- a/terminal.c +++ b/terminal.c @@ -465,7 +465,8 @@ initialize_fonts(struct terminal *term, const struct config *conf) struct terminal * term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, - const char *term_env, const char *foot_exe, int argc, char *const *argv, + const char *term_env, const char *foot_exe, const char *cwd, + int argc, char *const *argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data) { int ptmx = -1; @@ -601,18 +602,9 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, .shutdown_cb = shutdown_cb, .shutdown_data = shutdown_data, .foot_exe = strdup(foot_exe), - .cwd = NULL, + .cwd = strdup(cwd), }; - { - size_t buf_len = 1024; - do { - term->cwd = realloc(term->cwd, buf_len); - getcwd(term->cwd, buf_len); - buf_len *= 2; - } while (errno == ERANGE); - } - initialize_color_cube(term); if (!initialize_render_workers(term)) goto err; @@ -626,7 +618,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl, LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); /* Start the slave/client */ - if ((term->slave = slave_spawn(term->ptmx, argc, argv, term_env, conf->shell)) == -1) + if ((term->slave = slave_spawn(term->ptmx, argc, term->cwd, argv, term_env, conf->shell)) == -1) goto err; /* Initiailze the Wayland window backend */ diff --git a/terminal.h b/terminal.h index 0fe4c4dc..0db97e2b 100644 --- a/terminal.h +++ b/terminal.h @@ -322,7 +322,8 @@ struct terminal { struct config; struct terminal *term_init( const struct config *conf, struct fdm *fdm, struct wayland *wayl, - const char *term_env, const char *foot_exe, int argc, char *const *argv, + const char *term_env, const char *foot_exe, const char *cwd, + int argc, char *const *argv, void (*shutdown_cb)(void *data, int exit_code), void *shutdown_data); bool term_shutdown(struct terminal *term);