summaryrefslogtreecommitdiff
path: root/wrapper.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2014-11-30 15:24:45 +0700
committerJunio C Hamano <gitster@pobox.com>2014-12-01 11:00:16 -0800
commit316e53e68c7c4d89876524f7f010685ebfe7be1e (patch)
tree919750cb4945e50fa490f3bf2fa1a3e6b5f88f4a /wrapper.c
parent31e26ebcb512ad234d9725120fb4f5670ff75625 (diff)
downloadgit-316e53e68c7c4d89876524f7f010685ebfe7be1e.tar.gz
wrapper.c: wrapper to open a file, fprintf then close
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/wrapper.c b/wrapper.c
index 007ec0d8ea..a2dff8e5be 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -550,3 +550,34 @@ char *xgetcwd(void)
die_errno(_("unable to get current working directory"));
return strbuf_detach(&sb, NULL);
}
+
+int write_file(const char *path, int fatal, const char *fmt, ...)
+{
+ struct strbuf sb = STRBUF_INIT;
+ va_list params;
+ int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
+ if (fd < 0) {
+ if (fatal)
+ die_errno(_("could not open %s for writing"), path);
+ return -1;
+ }
+ va_start(params, fmt);
+ strbuf_vaddf(&sb, fmt, params);
+ va_end(params);
+ if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
+ int err = errno;
+ close(fd);
+ strbuf_release(&sb);
+ errno = err;
+ if (fatal)
+ die_errno(_("could not write to %s"), path);
+ return -1;
+ }
+ strbuf_release(&sb);
+ if (close(fd)) {
+ if (fatal)
+ die_errno(_("could not close %s"), path);
+ return -1;
+ }
+ return 0;
+}