summaryrefslogtreecommitdiffstats
path: root/xpwrite.c
blob: c6fa77aaf16b71b8ff78f332df96859d80e19cc3 (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 pwrite() */
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

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

	while (count) {
		rv = pwrite(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;
}