diff options
Diffstat (limited to 'src/mystdlib.c')
| -rw-r--r-- | src/mystdlib.c | 54 | 
1 files changed, 54 insertions, 0 deletions
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 @@ | |||
| 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 | |||
| 8 | #include "mystdlib.h" | ||
| 9 | |||
| 10 | MAP map_file( char *filename, int readonly ) | ||
| 11 | { | ||
| 12 | struct stat fstatus; | ||
| 13 | MAP map = (MAP)malloc( sizeof( *map )); | ||
| 14 | |||
| 15 | if( map ) | ||
| 16 | { | ||
| 17 | memset( map, 0, sizeof( *map )); | ||
| 18 | |||
| 19 | if( ( map->fh = open( filename, readonly ? O_RDONLY : O_RDWR ) ) >= 0 ) | ||
| 20 | { | ||
| 21 | fstat( map->fh, &fstatus ); | ||
| 22 | if( ( map->addr = mmap( NULL, map->size = (size_t)fstatus.st_size, | ||
| 23 | PROT_READ | ( readonly ? 0 : PROT_WRITE), MAP_NOCORE | (readonly ? 0 : MAP_SHARED), map->fh, 0) ) == MAP_FAILED ) | ||
| 24 | { | ||
| 25 | fprintf( stderr, "Mapping file '%s' failed\n", filename ); | ||
| 26 | close( map->fh ); free( map ); map = NULL; | ||
| 27 | } | ||
| 28 | } else { | ||
| 29 | fprintf( stderr, "Couldn't open file: '%s'\n", filename ); | ||
| 30 | free( map ); map = NULL; | ||
| 31 | } | ||
| 32 | } else { | ||
| 33 | fputs( "Couldn't allocate memory", stderr ); | ||
| 34 | } | ||
| 35 | |||
| 36 | return map; | ||
| 37 | } | ||
| 38 | |||
| 39 | void unmap_file ( MAP *pMap ) | ||
| 40 | { | ||
| 41 | if( !pMap || !*pMap ) return; | ||
| 42 | munmap( (*pMap)->addr, (*pMap)->size); | ||
| 43 | close( (*pMap)->fh); | ||
| 44 | free( *pMap ); *pMap = NULL; | ||
| 45 | } | ||
| 46 | |||
| 47 | int getfilesize( int fd, unsigned long *size) | ||
| 48 | { | ||
| 49 | struct stat sb; | ||
| 50 | int ret; | ||
| 51 | if( fstat( fd, &sb )) return -1; | ||
| 52 | *size = sb.st_size; | ||
| 53 | return 0; | ||
| 54 | } | ||
