__map_file.c raw

   1  #include <sys/mman.h>
   2  #include <fcntl.h>
   3  #include <sys/stat.h>
   4  #include "syscall.h"
   5  #include "kstat.h"
   6  
   7  const char unsigned *__map_file(const char *pathname, size_t *size)
   8  {
   9  	struct kstat st;
  10  	const unsigned char *map = MAP_FAILED;
  11  	int fd = sys_open(pathname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
  12  	if (fd < 0) return 0;
  13  	if (!syscall(SYS_fstat, fd, &st)) {
  14  		map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  15  		*size = st.st_size;
  16  	}
  17  	__syscall(SYS_close, fd);
  18  	return map == MAP_FAILED ? 0 : map;
  19  }
  20