File: xpio.c

package info (click to toggle)
klibc 2.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,204 kB
  • sloc: ansic: 48,657; asm: 2,204; perl: 693; makefile: 220; sh: 177
file content (51 lines) | stat: -rw-r--r-- 766 bytes parent folder | download | duplicates (10)
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;
}