[PATCH 5/7] Add strbuf_read_file().
From: Kristian Høgsberg <hidden>
Date: 2016-06-15 22:43:35
Subsystem:
the rest · Maintainer:
Linus Torvalds
Signed-off-by: Kristian Høgsberg <redacted> --- builtin-tag.c | 10 +++------- strbuf.c | 15 +++++++++++++++ strbuf.h | 1 + 3 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/builtin-tag.c b/builtin-tag.c
index 68c9a20..c97673f 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c@@ -53,13 +53,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 d919047..438606d 100644
--- a/strbuf.c
+++ b/strbuf.c@@ -110,6 +110,21 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) return sb->len - oldlen; } +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; +} + void read_line(struct strbuf *sb, FILE *fp, int term) { int ch; if (feof(fp)) {
diff --git a/strbuf.h b/strbuf.h
index 5960637..3a523ab 100644
--- a/strbuf.h
+++ b/strbuf.h@@ -95,6 +95,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 void read_line(struct strbuf *, FILE *, int);
--
1.5.3.1.993.gbf388-dirty