From 75025ccdb79b5d6b290c630f8777c9f7bb9d257c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 23 Dec 2006 02:33:55 -0500 Subject: 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 Signed-off-by: Junio C Hamano --- write_or_die.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'write_or_die.c') diff --git a/write_or_die.c b/write_or_die.c index bfe4eeb64..8cf648602 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; -- cgit v1.2.1