Files
semi-libc/src/time/__map_file.c
T
Rich Felker 594c827a22 support kernels with no SYS_open syscall, only SYS_openat
open is handled specially because it is used from so many places, in
so many variants (2 or 3 arguments, setting errno or not, and
cancellable or not). trying to do it as a function would not only
increase bloat, but would also risk subtle breakage.

this is the first step towards supporting "new" archs where linux
lacks "old" syscalls.
2014-05-24 22:54:05 -04:00

20 lines
536 B
C

#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "syscall.h"
void *__mmap(void *, size_t, int, int, int, off_t);
const char unsigned *__map_file(const char *pathname, size_t *size)
{
struct stat st;
const unsigned char *map = MAP_FAILED;
int fd = __sys_open(pathname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (fd < 0) return 0;
if (!__syscall(SYS_fstat, fd, &st))
map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
__syscall(SYS_close, fd);
*size = st.st_size;
return map == MAP_FAILED ? 0 : map;
}