version 1.30, 2015/08/29 09:36:46 |
version 1.31, 2015/10/31 08:13:58 |
|
|
struct client *c = cmdq->client; |
struct client *c = cmdq->client; |
struct session *s; |
struct session *s; |
struct paste_buffer *pb; |
struct paste_buffer *pb; |
const char *path, *bufname, *bufdata, *start, *end; |
const char *path, *bufname, *bufdata, *start, *end, *cwd; |
char *msg; |
const char *flags; |
|
char *msg, *file, resolved[PATH_MAX]; |
size_t size, used, msglen, bufsize; |
size_t size, used, msglen, bufsize; |
int cwd, fd; |
|
FILE *f; |
FILE *f; |
|
|
if (!args_has(args, 'b')) { |
if (!args_has(args, 'b')) { |
|
|
else if ((s = cmd_find_current(cmdq)) != NULL) |
else if ((s = cmd_find_current(cmdq)) != NULL) |
cwd = s->cwd; |
cwd = s->cwd; |
else |
else |
cwd = AT_FDCWD; |
cwd = "."; |
|
|
f = NULL; |
flags = "wb"; |
if (args_has(self->args, 'a')) { |
if (args_has(self->args, 'a')) |
fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600); |
flags = "ab"; |
if (fd != -1) |
|
f = fdopen(fd, "ab"); |
xasprintf(&file, "%s/%s", cwd, path); |
} else { |
if (realpath(file, resolved) == NULL) |
fd = openat(cwd, path, O_CREAT|O_RDWR|O_TRUNC, 0600); |
f = NULL; |
if (fd != -1) |
else |
f = fdopen(fd, "wb"); |
f = fopen(resolved, flags); |
} |
free(file); |
if (f == NULL) { |
if (f == NULL) { |
if (fd != -1) |
cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); |
close(fd); |
|
cmdq_error(cmdq, "%s: %s", path, strerror(errno)); |
|
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |
|
|
if (fwrite(bufdata, 1, bufsize, f) != bufsize) { |
if (fwrite(bufdata, 1, bufsize, f) != bufsize) { |
cmdq_error(cmdq, "%s: fwrite error", path); |
cmdq_error(cmdq, "%s: write error", resolved); |
fclose(f); |
fclose(f); |
return (CMD_RETURN_ERROR); |
return (CMD_RETURN_ERROR); |
} |
} |