-
Notifications
You must be signed in to change notification settings - Fork 1
/
fdrw.h
60 lines (53 loc) · 1.1 KB
/
fdrw.h
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
52
53
54
55
56
57
58
59
60
/*
* (c) Copyright 2000, 2001, 2002 -- Anders Torger
*
* This program is open source. For license terms, see the LICENSE file.
*
*/
#ifndef FDRW_H_
#define FDRW_H_
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "defs.h"
static inline bool_t
readfd(int fd,
void *buf,
size_t count)
{
int n, i = 0;
do {
if ((n = read(fd, &((uint8_t *)buf)[i], count - i)) < 1) {
if (n == 0 || errno != EINTR) {
fprintf(stderr, "(%d) read from fd %d failed: %s\n",
(int)getpid(), fd, strerror(errno));
return false;
}
continue;
}
i += n;
} while (i != count);
return true;
}
static inline bool_t
writefd(int fd,
const void *buf,
size_t count)
{
int n, i = 0;
do {
if ((n = write(fd, &((const uint8_t *)buf)[i], count - i)) < 1) {
if (n == 0 || errno != EINTR) {
fprintf(stderr, "(%d) write to fd %d failed: %s\n",
(int)getpid(), fd, strerror(errno));
return false;
}
continue;
}
i += n;
} while (i != count);
return true;
}
#endif