-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
55 lines (44 loc) · 969 Bytes
/
utils.cpp
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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include "utils.h"
void msg(const char *msg)
{
fprintf(stderr, "%s\n", msg);
}
void die(const char *msg)
{
int err = errno;
fprintf(stderr, "[%d] %s\n", err, msg);
abort();
}
// The read_full() function reads from the kernel until it gets exactly n bytes.
int32_t read_full(int fd, char *buf, size_t n)
{
while (n > 0)
{
ssize_t rv = read(fd, buf, n);
if (rv <= 0)
return -1; // error, or unexpected EOF
assert((size_t)rv <= n);
n -= (size_t)rv;
buf += rv;
}
return 0;
}
int32_t write_full(int fd, char *buf, size_t n)
{
while (n > 0)
{
ssize_t rv = write(fd, buf, n);
if (rv <= 0)
return -1; // error, or unexpected EOF
assert((size_t)rv <= n);
n -= (size_t)rv;
buf += rv;
}
return 0;
}