1
2
3
4
5
6
7
8
9#include "libbb.h"
10
11ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count)
12{
13 ssize_t n;
14
15 do {
16 n = read(fd, buf, count);
17 } while (n < 0 && errno == EINTR);
18
19 return n;
20}
21
22
23
24
25
26
27
28ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
29{
30 ssize_t cc;
31 ssize_t total;
32
33 total = 0;
34
35 while (len) {
36 cc = safe_read(fd, buf, len);
37
38 if (cc < 0) {
39 if (total) {
40
41
42 return total;
43 }
44 return cc;
45 }
46 if (cc == 0)
47 break;
48 buf = ((char *)buf) + cc;
49 total += cc;
50 len -= cc;
51 }
52
53 return total;
54}
55
56ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size)
57{
58
59 size = full_read(fd, buf, size);
60
61 close(fd);
62
63 return size;
64}
65
66ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
67{
68 int fd = open(filename, O_RDONLY);
69 if (fd < 0)
70 return fd;
71 return read_close(fd, buf, size);
72}
73