1
0
mirror of https://github.com/tmux/tmux.git synced 2025-03-25 15:28:49 +00:00

Do not use PWD unless it actually matches the real working directory.

This commit is contained in:
nicm 2018-11-22 10:36:40 +00:00
parent 749f67b7d8
commit 3a7b9d5735
3 changed files with 29 additions and 7 deletions

View File

@ -222,7 +222,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
const char *ttynam, *cwd; const char *ttynam, *cwd;
pid_t ppid; pid_t ppid;
enum msgtype msg; enum msgtype msg;
char *cause, path[PATH_MAX]; char *cause;
struct termios tio, saved_tio; struct termios tio, saved_tio;
size_t size; size_t size;
@ -277,9 +277,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL); client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
/* Save these before pledge(). */ /* Save these before pledge(). */
if ((cwd = getenv("PWD")) == NULL && if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
(cwd = getcwd(path, sizeof path)) == NULL &&
(cwd = find_home()) == NULL)
cwd = "/"; cwd = "/";
if ((ttynam = ttyname(STDIN_FILENO)) == NULL) if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
ttynam = ""; ttynam = "";

29
tmux.c
View File

@ -166,6 +166,31 @@ setblocking(int fd, int state)
} }
} }
const char *
find_cwd(void)
{
char resolved1[PATH_MAX], resolved2[PATH_MAX];
static char cwd[PATH_MAX];
const char *pwd;
if (getcwd(cwd, sizeof cwd) == NULL)
return (NULL);
if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
return (cwd);
/*
* We want to use PWD so that symbolic links are maintained,
* but only if it matches the actual working directory.
*/
if (realpath(pwd, resolved1) == NULL)
return (cwd);
if (realpath(cwd, resolved2) == NULL)
return (cwd);
if (strcmp(resolved1, resolved2) != 0)
return (cwd);
return (pwd);
}
const char * const char *
find_home(void) find_home(void)
{ {
@ -191,7 +216,6 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *path, *label, *cause, **var; char *path, *label, *cause, **var;
char tmp[PATH_MAX];
const char *s, *shell, *cwd; const char *s, *shell, *cwd;
int opt, flags, keys; int opt, flags, keys;
const struct options_table_entry *oe; const struct options_table_entry *oe;
@ -293,8 +317,7 @@ main(int argc, char **argv)
global_environ = environ_create(); global_environ = environ_create();
for (var = environ; *var != NULL; var++) for (var = environ; *var != NULL; var++)
environ_put(global_environ, *var); environ_put(global_environ, *var);
if ((cwd = getenv("PWD")) == NULL && if ((cwd = find_cwd()) != NULL)
(cwd = getcwd(tmp, sizeof tmp)) != NULL)
environ_set(global_environ, "PWD", "%s", cwd); environ_set(global_environ, "PWD", "%s", cwd);
global_options = options_create(NULL); global_options = options_create(NULL);

1
tmux.h
View File

@ -1506,6 +1506,7 @@ extern int ptm_fd;
extern const char *shell_command; extern const char *shell_command;
int areshell(const char *); int areshell(const char *);
void setblocking(int, int); void setblocking(int, int);
const char *find_cwd(void);
const char *find_home(void); const char *find_home(void);
/* proc.c */ /* proc.c */