From a9390b9fcefb18c4ccdb521086a051bc9112e03d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Mon, 17 Sep 2007 20:06:46 -0400 Subject: [PATCH] Add strbuf_read_file(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristian Høgsberg Signed-off-by: Junio C Hamano --- builtin-tag.c | 11 +++-------- strbuf.c | 15 +++++++++++++++ strbuf.h | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/builtin-tag.c b/builtin-tag.c index 82ebda11b..fcbf9bbf1 100644 --- a/builtin-tag.c +++ b/builtin-tag.c @@ -22,7 +22,6 @@ static void launch_editor(const char *path, struct strbuf *buffer) const char *editor, *terminal; struct child_process child; const char *args[3]; - int fd; editor = getenv("GIT_EDITOR"); if (!editor && editor_program) @@ -52,13 +51,9 @@ static void launch_editor(const char *path, struct strbuf *buffer) if (run_command(&child)) die("There was a problem with the editor %s.", editor); - fd = open(path, O_RDONLY); - if (fd < 0) - die("could not open '%s': %s", path, strerror(errno)); - if (strbuf_read(buffer, fd, 0) < 0) { - die("could not read message file '%s': %s", path, strerror(errno)); - } - close(fd); + if (strbuf_read_file(buffer, path) < 0) + die("could not read message file '%s': %s", + path, strerror(errno)); } struct tag_filter { diff --git a/strbuf.c b/strbuf.c index d5e92ee17..d1e338bfb 100644 --- a/strbuf.c +++ b/strbuf.c @@ -177,3 +177,18 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term) sb->buf[sb->len] = '\0'; return 0; } + +int strbuf_read_file(struct strbuf *sb, const char *path) +{ + int fd, len; + + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + len = strbuf_read(sb, fd, 0); + close(fd); + if (len < 0) + return -1; + + return len; +} diff --git a/strbuf.h b/strbuf.h index fd6838933..d4d9e5663 100644 --- a/strbuf.h +++ b/strbuf.h @@ -108,6 +108,7 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...); extern size_t strbuf_fread(struct strbuf *, size_t, FILE *); /* XXX: if read fails, any partial read is undone */ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint); +extern int strbuf_read_file(struct strbuf *sb, const char *path); extern int strbuf_getline(struct strbuf *, FILE *, int); -- 2.26.2