From a8ca1340ad1217a6512ce21586f05102c4000cc9 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Sun, 23 Oct 2011 15:08:58 +0000 Subject: [PATCH] Sync OpenBSD patchset 968: Try to resolve relative paths for loadb and saveb (first using client working directory if any then default-path or session wd). --- cmd-load-buffer.c | 16 +++++++++++++++- cmd-save-buffer.c | 17 ++++++++++++++++- tmux.c | 16 ++++++++++++++++ tmux.h | 1 + 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c index af6fea7f..373fd295 100644 --- a/cmd-load-buffer.c +++ b/cmd-load-buffer.c @@ -48,8 +48,9 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct client *c = ctx->cmdclient; + struct session *s; FILE *f; - const char *path; + const char *path, *newpath, *wd; char *pdata, *new_pdata, *cause; size_t psize; u_int limit; @@ -93,6 +94,19 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) return (1); } + if (c != NULL) + wd = c->cwd; + else if ((s = cmd_current_session(ctx, 0)) != NULL) { + wd = options_get_string(&s->options, "default-path"); + if (*wd == '\0') + wd = s->cwd; + } else + wd = NULL; + if (wd != NULL && *wd != '\0') { + newpath = get_full_path(wd, path); + if (newpath != NULL) + path = newpath; + } if ((f = fopen(path, "rb")) == NULL) { ctx->error(ctx, "%s: %s", path, strerror(errno)); return (-1); diff --git a/cmd-save-buffer.c b/cmd-save-buffer.c index 9e6096d1..9c2461e1 100644 --- a/cmd-save-buffer.c +++ b/cmd-save-buffer.c @@ -45,8 +45,9 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct client *c = ctx->cmdclient; + struct session *s; struct paste_buffer *pb; - const char *path; + const char *path, *newpath, *wd; char *cause; int buffer; mode_t mask; @@ -80,6 +81,20 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) } bufferevent_write(c->stdout_event, pb->data, pb->size); } else { + if (c != NULL) + wd = c->cwd; + else if ((s = cmd_current_session(ctx, 0)) != NULL) { + wd = options_get_string(&s->options, "default-path"); + if (*wd == '\0') + wd = s->cwd; + } else + wd = NULL; + if (wd != NULL && *wd != '\0') { + newpath = get_full_path(wd, path); + if (newpath != NULL) + path = newpath; + } + mask = umask(S_IRWXG | S_IRWXO); if (args_has(self->args, 'a')) f = fopen(path, "ab"); diff --git a/tmux.c b/tmux.c index 9852c734..76e808e0 100644 --- a/tmux.c +++ b/tmux.c @@ -127,6 +127,22 @@ areshell(const char *shell) return (0); } +const char* +get_full_path(const char *wd, const char *path) +{ + static char newpath[MAXPATHLEN]; + char oldpath[MAXPATHLEN]; + + if (getcwd(oldpath, sizeof oldpath) == NULL) + return (NULL); + if (chdir(wd) != 0) + return (NULL); + if (realpath(path, newpath) != 0) + return (NULL); + chdir(oldpath); + return (newpath); +} + void parseenvironment(void) { diff --git a/tmux.h b/tmux.h index 12d401ac..2982039e 100644 --- a/tmux.h +++ b/tmux.h @@ -1341,6 +1341,7 @@ void logfile(const char *); const char *getshell(void); int checkshell(const char *); int areshell(const char *); +const char* get_full_path(const char *, const char *); void setblocking(int, int); __dead void shell_exec(const char *, const char *);