summaryrefslogtreecommitdiffstats
path: root/usr/kinit/xpio.c
blob: 42a98443a8290ce39b39933378225b6710d2490e (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Looping versions of pread() and pwrite()
 */

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "xpio.h"

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

	while (count) {
		rv = pread(fd, bp, count, offset);

		if (rv == 0 || (rv == -1 && errno != EINTR))
			break;

		bp	+= rv;
		count	-= rv;
		offset	+= rv;
		ctr	+= rv;
	}

	return ctr ? ctr : rv;
}

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

	while (count) {
		rv = pwrite(fd, bp, count, offset);

		if (rv == 0 || (rv == -1 && errno != EINTR))
			break;

		bp	+= rv;
		count	-= rv;
		offset	+= rv;
		ctr	+= rv;
	}

	return ctr ? ctr : rv;
}