-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsafer.c
More file actions
34 lines (32 loc) · 681 Bytes
/
safer.c
File metadata and controls
34 lines (32 loc) · 681 Bytes
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
#include <unistd.h>
#include <errno.h>
int safer_write(int f, const char* b, int n) {
int acc = 0;
while(n) {
int r = write(f, b, n);
if(!r) return acc;
if(r==-1) {
if (errno==EINTR || errno==EAGAIN) continue;
return -1;
}
n-=r;
b+=r;
acc+=r;
}
return acc;
}
int safer_read(int f, char* b, int n) {
int acc = 0;
while(n) {
int r = read(f, b, n);
if(!r) return acc;
if(r==-1) {
if (errno==EINTR || errno==EAGAIN) continue;
return -1;
}
n-=r;
b+=r;
acc+=r;
}
return acc;
}