Add strbuf_read_file().
Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Kristian Høgsberg committed
Sep 17, 2007 at 20:06 UTC
a9390b9fcefb18c4ccdb521086a051bc9112e03d
3 files changed
+19
-8
builtin-tag.c
+3
-8
@@ -22,7 +22,6 @@ static void launch_editor(const char *path, struct strbuf *buffer)
22
const char *editor, *terminal;
23
struct child_process child;
24
const char *args[3];
25
- int fd;
25
26
editor = getenv("GIT_EDITOR");
27
if (!editor && editor_program)
@@ -52,13 +51,9 @@ static void launch_editor(const char *path, struct strbuf *buffer)
51
if (run_command(&child))
52
die("There was a problem with the editor %s.", editor);
53
55
- fd = open(path, O_RDONLY);
56
- if (fd < 0)
57
- die("could not open '%s': %s", path, strerror(errno));
58
- if (strbuf_read(buffer, fd, 0) < 0) {
59
- die("could not read message file '%s': %s", path, strerror(errno));
60
- }
61
- close(fd);
54
+ if (strbuf_read_file(buffer, path) < 0)
55
+ die("could not read message file '%s': %s",
56
+ path, strerror(errno));
57
}
58
59
struct tag_filter {
strbuf.c
+15
@@ -177,3 +177,18 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
177
sb->buf[sb->len] = '\0';
178
return 0;
179
}
180
+
181
+int strbuf_read_file(struct strbuf *sb, const char *path)
182
+{
183
+ int fd, len;
184
+
185
+ fd = open(path, O_RDONLY);
186
+ if (fd < 0)
187
+ return -1;
188
+ len = strbuf_read(sb, fd, 0);
189
+ close(fd);
190
+ if (len < 0)
191
+ return -1;
192
+
193
+ return len;
194
+}
strbuf.h
+1
@@ -108,6 +108,7 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
108
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
109
/* XXX: if read fails, any partial read is undone */
110
extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
111
+extern int strbuf_read_file(struct strbuf *sb, const char *path);
112
113
extern int strbuf_getline(struct strbuf *, FILE *, int);
114