/****************************************** * mmap example with apr * Sylvain Marechal * 25/10/2005 *****************************************/ #include "apr_general.h" #include "apr_mmap.h" /********************************************* * mmap ********************************************/ void test_mmap( apr_pool_t *pool ) { apr_status_t ret; apr_mmap_t * mm; apr_off_t offset = 0; apr_size_t size = 4000; /*~ page size */ void * addr = 0; apr_file_t * fd; apr_fileperms_t perm = APR_OS_DEFAULT; apr_size_t cb; char buf[4000]= {0}; /* open a file and put some data into it * Note: It seems file can't grow, so set the size now * (4000 bytes into it) */ if( apr_file_open( &fd, "./coucou.txt", APR_READ | APR_WRITE | APR_CREATE, perm, pool ) != APR_SUCCESS ) { printf( "test_mmap(): Can't open/create file\n" ); exit(1); } cb = sizeof(buf); strcpy( buf, "coucou" ); apr_file_write( fd, buf, &cb ); /* mmap the file */ ret = apr_mmap_create( &mm, fd, offset, size, APR_MMAP_READ | APR_MMAP_WRITE, pool ); if( ret != APR_SUCCESS ) { char buferror[256] = {0}; apr_strerror( ret, buferror, sizeof(buferror) -1); printf( "test_mmap(): apr_mmap_create() failed (ret=%d, '%s')..\n", ret, buferror ); exit(1); } /* read data mmapped */ ret = apr_mmap_offset( &addr, mm, offset ); printf( "addr: '%s'\n", (char *)addr ); /* write some data in addr */ strcat( (char *)addr, " les amis - blablabla" ); /* close the mmap and the file */ apr_mmap_delete( mm ); apr_file_close( fd ); /* reopen the file to read data */ apr_file_open( &fd, "./coucou.txt", APR_READ, perm, pool ); cb = sizeof(buf); apr_file_read( fd, buf, &cb ); printf( "data readen (%d bytes): '%s'\n", cb, buf ); apr_file_close( fd ); /* Delete file */ apr_file_remove( "./coucou.txt", pool ); } /********************************************* * main ********************************************/ int main( int argc, char * argv[] ) { apr_pool_t *pool; if (apr_initialize() != APR_SUCCESS) { printf( "Could not initialize\n"); exit(-1); } if (apr_pool_create(&pool, NULL) != APR_SUCCESS) { printf( "Could not allocate pool\n"); exit( -1); } test_mmap( pool ); apr_pool_destroy( pool ); return 0; }