From 046857dfb88f05e6b310fe9ef07b9f2d3ac5922d Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Thu, 20 Feb 2014 22:42:56 +0100 Subject: Restructure project, make names more clear --- src/export/mystdlib.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/export/mystdlib.c (limited to 'src/export/mystdlib.c') 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mystdlib.h" + +MAP map_file( char *filename, int readonly ) +{ + struct stat fstatus; + MAP map = (MAP)malloc( sizeof( *map )); + + if( map ) + { + memset( map, 0, sizeof( *map )); + + if( ( map->fh = open( filename, readonly ? O_RDONLY : O_RDWR ) ) >= 0 ) + { + fstat( map->fh, &fstatus ); + if( ( map->addr = mmap( NULL, map->size = (size_t)fstatus.st_size, + PROT_READ | ( readonly ? 0 : PROT_WRITE), (readonly ? MAP_PRIVATE : MAP_SHARED), map->fh, 0) ) == MAP_FAILED ) + { + fprintf( stderr, "Mapping file '%s' failed\n", filename ); + close( map->fh ); free( map ); map = NULL; + } + } else { + fprintf( stderr, "Couldn't open file: '%s'\n", filename ); + free( map ); map = NULL; + } + } else { + fputs( "Couldn't allocate memory", stderr ); + } + + return map; +} + +void unmap_file ( MAP *pMap ) +{ + if( !pMap || !*pMap ) return; + munmap( (*pMap)->addr, (*pMap)->size); + close( (*pMap)->fh); + free( *pMap ); *pMap = NULL; +} + +int getfilesize( int fd, unsigned long *size) +{ + struct stat sb; + int ret; + if( fstat( fd, &sb )) return -1; + *size = sb.st_size; + return 0; +} -- cgit v1.2.3