hotplug2: patches from OpenWRT/svn
[tomato.git] / release / src / router / hotplug2 / filemap_utils.c
blobff13803c1db931d3839881457c386cc1d2e8f796
1 /*****************************************************************************\
2 * _ _ _ _ ___ *
3 * | || | ___ | |_ _ __ | | _ _ __ _ |_ ) *
4 * | __ |/ _ \| _|| '_ \| || || |/ _` | / / *
5 * |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___| *
6 * |_| |___/ *
7 \*****************************************************************************/
9 #include <string.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
17 #include "filemap_utils.h"
19 /**
20 * Basic open/mmap wrapper to make things simpler.
22 * @1 Filename of the mmaped file
23 * @2 Pointer to filemap structure
25 * Returns: 0 if success, 1 otherwise
27 int map_file(const char *filename, struct filemap_t *filemap) {
28 struct stat statbuf;
30 filemap->fd = open(filename, O_RDONLY);
31 if (filemap->fd == -1) {
32 return 1;
35 if (fstat(filemap->fd, &statbuf)) {
36 close(filemap->fd);
37 return 1;
40 filemap->size = statbuf.st_size;
42 filemap->map = mmap(0, filemap->size, PROT_READ, MAP_SHARED, filemap->fd, 0);
43 if (filemap->map == MAP_FAILED) {
44 close(filemap->fd);
45 return 1;
48 return 0;
51 /**
52 * Basic close/munmap wrapper.
54 * @1 Pointer to filemap structure
56 * Returns: always 0
58 int unmap_file(struct filemap_t *filemap) {
59 munmap(filemap->map, filemap->size);
60 close(filemap->fd);
62 return 0;