diff options
| author | erdgeist <> | 2007-04-19 19:54:07 +0000 |
|---|---|---|
| committer | erdgeist <> | 2007-04-19 19:54:07 +0000 |
| commit | cf193c66d988637d9ddfb0acee82608a04f96402 (patch) | |
| tree | 05d007e28ce0526ff150e67eecba782ea57e832d /mystdlib.c | |
Kickoff
Diffstat (limited to 'mystdlib.c')
| -rw-r--r-- | mystdlib.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/mystdlib.c b/mystdlib.c new file mode 100644 index 0000000..b53beca --- /dev/null +++ b/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 <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | #include "mystdlib.h" | ||
| 11 | |||
| 12 | MAP 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 ? 0 : 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 | |||
| 41 | void 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 | |||
| 49 | int 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 | } | ||
