From c2011c5def9154c9a48f5e7e17d48d840aad675d Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Thu, 29 Apr 2004 19:42:16 +0000 Subject: Start --- src/mystdlib.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/mystdlib.c (limited to 'src/mystdlib.c') diff --git a/src/mystdlib.c b/src/mystdlib.c new file mode 100644 index 0000000..2deda22 --- /dev/null +++ b/src/mystdlib.c @@ -0,0 +1,54 @@ +#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), MAP_NOCORE | (readonly ? 0 : 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