-
Notifications
You must be signed in to change notification settings - Fork 2
/
safe_io.c
46 lines (41 loc) · 1.34 KB
/
safe_io.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* safe_io.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/30 21:25:54 by jkong #+# #+# */
/* Updated: 2022/06/15 21:18:22 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "safe_io.h"
int read_safe(int fd, void *buf, size_t len)
{
size_t off;
ssize_t res;
off = 0;
while (off < len)
{
res = read(fd, buf + off, len - off);
if (res < 0)
exit(EXIT_FAILURE);
if (res == 0)
return (0);
off += res;
}
return (1);
}
void write_safe(int fd, const void *buf, size_t len)
{
size_t off;
ssize_t res;
off = 0;
while (off < len)
{
res = write(fd, buf + off, len - off);
if (res < 0)
exit(EXIT_FAILURE);
off += res;
}
}