packio-simple

view src/memmap.c @ 2:4767e7769c32

packio-simple has more chances to ever get done
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Aug 2015 05:14:29 +0300
parents
children
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include "memmap.h"
6 #if defined(__unix__) || defined(__APPLE__)
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
12 int map_file(struct mmfile *mm, const char *fname)
13 {
14 int fd;
15 struct stat st;
17 if((fd = open(fname, O_RDONLY)) == -1) {
18 fprintf(stderr, "map_file: failed to open %s: %s\n", fname, strerror(errno));
19 return -1;
20 }
21 fstat(fd, &st);
23 if((mm->ptr = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == (void*)-1) {
24 close(fd);
25 fprintf(stderr, "map_file: failed to map file %s: %s\n", fname, strerror(errno));
26 return -1;
27 }
28 mm->size = st.st_size;
30 close(fd);
31 return 0;
32 }
34 void unmap_file(struct mmfile *mm)
35 {
36 munmap(mm->ptr, mm->size);
37 }
39 #else
40 /* implement on win32 */
41 #endif