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
)
36 } while (total
!= len
);
38 return total
? total
: err
;
42 os_read_file(const char *filename
)
44 /* Note that this also serves as a slight margin to avoid a 2x grow when
45 * the file is just a few bytes larger when we read it than when we
47 * The string's NULL terminator is also included in here.
51 int fd
= open(filename
, O_RDONLY
);
53 /* errno set by open() */
57 /* Pre-allocate a buffer at least the size of the file if we can read
61 if (fstat(fd
, &stat
) == 0)
64 char *buf
= malloc(len
);
72 size_t offset
= 0, remaining
= len
- 1;
73 while ((read
= readN(fd
, buf
+ offset
, remaining
)) == remaining
) {
74 char *newbuf
= realloc(buf
, 2 * len
);
85 remaining
= len
- offset
- 1;
101 os_read_file(const char *filename
)