Skip to content

Commit e48c9c2

Browse files
author
tcunha
committed
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).
1 parent f0b9086 commit e48c9c2

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

cmd-load-buffer.c

+15-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
4848
{
4949
struct args *args = self->args;
5050
struct client *c = ctx->cmdclient;
51+
struct session *s;
5152
FILE *f;
52-
const char *path;
53+
const char *path, *newpath, *wd;
5354
char *pdata, *new_pdata, *cause;
5455
size_t psize;
5556
u_int limit;
@@ -93,6 +94,19 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
9394
return (1);
9495
}
9596

97+
if (c != NULL)
98+
wd = c->cwd;
99+
else if ((s = cmd_current_session(ctx, 0)) != NULL) {
100+
wd = options_get_string(&s->options, "default-path");
101+
if (*wd == '\0')
102+
wd = s->cwd;
103+
} else
104+
wd = NULL;
105+
if (wd != NULL && *wd != '\0') {
106+
newpath = get_full_path(wd, path);
107+
if (newpath != NULL)
108+
path = newpath;
109+
}
96110
if ((f = fopen(path, "rb")) == NULL) {
97111
ctx->error(ctx, "%s: %s", path, strerror(errno));
98112
return (-1);

cmd-save-buffer.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
4545
{
4646
struct args *args = self->args;
4747
struct client *c = ctx->cmdclient;
48+
struct session *s;
4849
struct paste_buffer *pb;
49-
const char *path;
50+
const char *path, *newpath, *wd;
5051
char *cause;
5152
int buffer;
5253
mode_t mask;
@@ -80,6 +81,20 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
8081
}
8182
bufferevent_write(c->stdout_event, pb->data, pb->size);
8283
} else {
84+
if (c != NULL)
85+
wd = c->cwd;
86+
else if ((s = cmd_current_session(ctx, 0)) != NULL) {
87+
wd = options_get_string(&s->options, "default-path");
88+
if (*wd == '\0')
89+
wd = s->cwd;
90+
} else
91+
wd = NULL;
92+
if (wd != NULL && *wd != '\0') {
93+
newpath = get_full_path(wd, path);
94+
if (newpath != NULL)
95+
path = newpath;
96+
}
97+
8398
mask = umask(S_IRWXG | S_IRWXO);
8499
if (args_has(self->args, 'a'))
85100
f = fopen(path, "ab");

tmux.c

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ areshell(const char *shell)
127127
return (0);
128128
}
129129

130+
const char*
131+
get_full_path(const char *wd, const char *path)
132+
{
133+
static char newpath[MAXPATHLEN];
134+
char oldpath[MAXPATHLEN];
135+
136+
if (getcwd(oldpath, sizeof oldpath) == NULL)
137+
return (NULL);
138+
if (chdir(wd) != 0)
139+
return (NULL);
140+
if (realpath(path, newpath) != 0)
141+
return (NULL);
142+
chdir(oldpath);
143+
return (newpath);
144+
}
145+
130146
void
131147
parseenvironment(void)
132148
{

tmux.h

+1
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,7 @@ void logfile(const char *);
13411341
const char *getshell(void);
13421342
int checkshell(const char *);
13431343
int areshell(const char *);
1344+
const char* get_full_path(const char *, const char *);
13441345
void setblocking(int, int);
13451346
__dead void shell_exec(const char *, const char *);
13461347

0 commit comments

Comments
 (0)