summaryrefslogtreecommitdiff
path: root/write_or_die.c
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2006-12-23 07:33:55 (GMT)
committerJunio C Hamano <junkio@cox.net>2006-12-29 19:36:44 (GMT)
commit75025ccdb79b5d6b290c630f8777c9f7bb9d257c (patch)
treef69f3070024cef8c6c4c436517783f3b49959b70 /write_or_die.c
parent2dc3a234094afc61c367be3e6018722cd9d92ea9 (diff)
downloadgit-75025ccdb79b5d6b290c630f8777c9f7bb9d257c.zip
git-75025ccdb79b5d6b290c630f8777c9f7bb9d257c.tar.gz
git-75025ccdb79b5d6b290c630f8777c9f7bb9d257c.tar.bz2
Create read_or_die utility routine.
Like write_or_die read_or_die reads the entire length requested or it kills the current process with a die call. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'write_or_die.c')
-rw-r--r--write_or_die.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/write_or_die.c b/write_or_die.c
index bfe4eeb..8cf6486 100644
--- a/write_or_die.c
+++ b/write_or_die.c
@@ -1,5 +1,21 @@
#include "cache.h"
+void read_or_die(int fd, void *buf, size_t count)
+{
+ char *p = buf;
+ ssize_t loaded;
+
+ while (count > 0) {
+ loaded = xread(fd, p, count);
+ if (loaded == 0)
+ die("unexpected end of file");
+ else if (loaded < 0)
+ die("read error (%s)", strerror(errno));
+ count -= loaded;
+ p += loaded;
+ }
+}
+
void write_or_die(int fd, const void *buf, size_t count)
{
const char *p = buf;