summaryrefslogtreecommitdiff
path: root/src/export/mystdlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/export/mystdlib.c')
-rw-r--r--src/export/mystdlib.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/export/mystdlib.c b/src/export/mystdlib.c
new file mode 100644
index 0000000..17f123b
--- /dev/null
+++ b/src/export/mystdlib.c
@@ -0,0 +1,56 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <sys/mman.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9
10#include "mystdlib.h"
11
12MAP map_file( char *filename, int readonly )
13{
14 struct stat fstatus;
15 MAP map = (MAP)malloc( sizeof( *map ));
16
17 if( map )
18 {
19 memset( map, 0, sizeof( *map ));
20
21 if( ( map->fh = open( filename, readonly ? O_RDONLY : O_RDWR ) ) >= 0 )
22 {
23 fstat( map->fh, &fstatus );
24 if( ( map->addr = mmap( NULL, map->size = (size_t)fstatus.st_size,
25 PROT_READ | ( readonly ? 0 : PROT_WRITE), (readonly ? MAP_PRIVATE : MAP_SHARED), map->fh, 0) ) == MAP_FAILED )
26 {
27 fprintf( stderr, "Mapping file '%s' failed\n", filename );
28 close( map->fh ); free( map ); map = NULL;
29 }
30 } else {
31 fprintf( stderr, "Couldn't open file: '%s'\n", filename );
32 free( map ); map = NULL;
33 }
34 } else {
35 fputs( "Couldn't allocate memory", stderr );
36 }
37
38 return map;
39}
40
41void unmap_file ( MAP *pMap )
42{
43 if( !pMap || !*pMap ) return;
44 munmap( (*pMap)->addr, (*pMap)->size);
45 close( (*pMap)->fh);
46 free( *pMap ); *pMap = NULL;
47}
48
49int getfilesize( int fd, unsigned long *size)
50{
51 struct stat sb;
52 int ret;
53 if( fstat( fd, &sb )) return -1;
54 *size = sb.st_size;
55 return 0;
56}