summaryrefslogtreecommitdiffstats
path: root/xwrite.c
blob: 80a754a4c99cca0caeff5cf0c07843dabc7cc7ef (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
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

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

	while (count) {
		rv = write(fd, p, count);
		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;
	}

	return out;
}