summaryrefslogtreecommitdiffstats
path: root/xpread.c
blob: 6fa9cfc7dad6892a95186d76097360306bd705b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#define _XOPEN_SOURCE 500	/* Needed for pread() */
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
{
	char *p = buf;
	ssize_t out = 0;
	ssize_t rv;

	while (count) {
		rv = pread(fd, p, count, offset);
		if (rv == -1) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			return out ? out : -1; /* Error */
		} else if (rv == 0) {
			return out; /* EOF */
		}

		p += rv;
		out += rv;
		count -= rv;
		offset += rv;
	}

	return out;
}