diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-02-22 20:33:13 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-02-22 20:33:13 +0000 |
commit | 7fb8f346a12c8c48a5fe2da61fbc86cbd9dee9c2 (patch) | |
tree | dab458976083a65ca8e55f0226b7ae1b25a67971 /usr.bin | |
parent | fc3abfcbc94d6518e68a34805a9765e5389f5b12 (diff) |
In load-buffer, read until EOF rather than using stat() and reading a fixed
size. Allows use of FIFOs and whatnot. From Tiago Cunha, idea from Fulvio
Ciriaco.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/cmd-load-buffer.c | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/usr.bin/tmux/cmd-load-buffer.c b/usr.bin/tmux/cmd-load-buffer.c index e42d5678402..47f7d441886 100644 --- a/usr.bin/tmux/cmd-load-buffer.c +++ b/usr.bin/tmux/cmd-load-buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-load-buffer.c,v 1.10 2009/11/26 22:32:00 nicm Exp $ */ +/* $OpenBSD: cmd-load-buffer.c,v 1.11 2010/02/22 20:33:12 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> @@ -16,13 +16,10 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <sys/types.h> -#include <sys/stat.h> - #include <errno.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include "tmux.h" @@ -48,11 +45,11 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_buffer_data *data = self->data; struct session *s; - struct stat sb; FILE *f; - char *pdata = NULL; + char *pdata, *new_pdata; size_t psize; u_int limit; + int ch; if ((s = cmd_find_session(ctx, data->target)) == NULL) return (-1); @@ -62,29 +59,23 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) return (-1); } - if (fstat(fileno(f), &sb) < 0) { - ctx->error(ctx, "%s: %s", data->arg, strerror(errno)); - goto error; - } - if (sb.st_size <= 0 || (uintmax_t) sb.st_size > SIZE_MAX) { - ctx->error(ctx, "%s: file empty or too large", data->arg); - goto error; - } - psize = (size_t) sb.st_size; - - /* - * We don't want to die due to memory exhaustion, hence xmalloc can't - * be used here. - */ - if ((pdata = malloc(psize)) == NULL) { - ctx->error(ctx, "malloc error: %s", strerror(errno)); - goto error; + pdata = NULL; + psize = 0; + while ((ch = getc(f)) != EOF) { + /* Do not let the server die due to memory exhaustion. */ + if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { + ctx->error(ctx, "realloc error: %s", strerror(errno)); + goto error; + } + pdata = new_pdata; + pdata[psize++] = ch; } - - if (fread(pdata, 1, psize, f) != psize) { - ctx->error(ctx, "%s: fread error", data->arg); + if (ferror(f)) { + ctx->error(ctx, "%s: read error", data->arg); goto error; } + if (pdata != NULL) + pdata[psize] = '\0'; fclose(f); |