2 * Copyright 2019 Intel Corporation
3 * SPDX-License-Identifier: MIT
11 #if defined(__linux__)
19 readN(int fd
, char *buf
, size_t len
)
24 ssize_t ret
= read(fd
, buf
+ total
, len
- total
);
29 if (ret
== -EINTR
|| ret
== -EAGAIN
)
38 } while (total
!= len
);
40 return total
? total
: err
;
44 os_read_file(const char *filename
)
46 /* Note that this also serves as a slight margin to avoid a 2x grow when
47 * the file is just a few bytes larger when we read it than when we
49 * The string's NULL terminator is also included in here.
53 int fd
= open(filename
, O_RDONLY
);
55 /* errno set by open() */
59 /* Pre-allocate a buffer at least the size of the file if we can read
63 if (fstat(fd
, &stat
) == 0)
66 char *buf
= malloc(len
);
74 size_t offset
= 0, remaining
= len
- 1;
75 while ((read
= readN(fd
, buf
+ offset
, remaining
)) == remaining
) {
76 char *newbuf
= realloc(buf
, 2 * len
);
87 remaining
= len
- offset
- 1;
103 os_read_file(const char *filename
)