diff options
Diffstat (limited to 'sntp/libopts/text_mmap.c')
-rw-r--r-- | sntp/libopts/text_mmap.c | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/sntp/libopts/text_mmap.c b/sntp/libopts/text_mmap.c index ced2977c5b79..01aa7d821255 100644 --- a/sntp/libopts/text_mmap.c +++ b/sntp/libopts/text_mmap.c @@ -1,7 +1,27 @@ -/* - * $Id: text_mmap.c,v 4.15 2006/11/27 01:52:23 bkorb Exp $ +/** + * \file text_mmap.c + * + * Time-stamp: "2010-07-17 10:15:32 bkorb" + * + * This file is part of AutoOpts, a companion to AutoGen. + * AutoOpts is free software. + * AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved + * + * AutoOpts is available under any one of two licenses. The license + * in use must be one of these two and the choice is under the control + * of the user of the license. + * + * The GNU Lesser General Public License, version 3 or later + * See the files "COPYING.lgplv3" and "COPYING.gplv3" + * + * The Modified Berkeley Software Distribution License + * See the file "COPYING.mbsd" + * + * These files have the following md5sums: * - * Time-stamp: "2006-09-10 14:50:04 bkorb" + * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 + * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 + * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd */ #ifndef MAP_ANONYMOUS @@ -70,16 +90,16 @@ * #include <mylib.h> * tmap_info_t mi; * int no_nul; - * void* data = text_mmap( "file", PROT_WRITE, MAP_PRIVATE, &mi ); + * void* data = text_mmap("file", PROT_WRITE, MAP_PRIVATE, &mi); * if (data == MAP_FAILED) return; * no_nul = (mi.txt_size == mi.txt_full_size); * << use the data >> - * text_munmap( &mi ); + * text_munmap(&mi); =*/ void* -text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) +text_mmap(char const* pzFile, int prot, int flags, tmap_info_t* pMI) { - memset( pMI, 0, sizeof(*pMI) ); + memset(pMI, 0, sizeof(*pMI)); #ifdef HAVE_MMAP pMI->txt_zero_fd = -1; #endif @@ -90,12 +110,12 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) */ { struct stat sb; - if (stat( pzFile, &sb ) != 0) { + if (stat(pzFile, &sb) != 0) { pMI->txt_errno = errno; return MAP_FAILED_PTR; } - if (! S_ISREG( sb.st_mode )) { + if (! S_ISREG(sb.st_mode)) { pMI->txt_errno = errno = EINVAL; return MAP_FAILED_PTR; } @@ -126,7 +146,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) if (((flags & MAP_SHARED) == 0) && (prot & PROT_WRITE)) o_flag |= O_EXCL; - pMI->txt_fd = open( pzFile, o_flag ); + pMI->txt_fd = open(pzFile, o_flag); } if (pMI->txt_fd == AO_INVALID_FD) { @@ -187,7 +207,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) pMI->txt_errno = errno; #elif defined(HAVE_DEV_ZERO) - pMI->txt_zero_fd = open( "/dev/zero", O_RDONLY ); + pMI->txt_zero_fd = open("/dev/zero", O_RDONLY); if (pMI->txt_zero_fd == AO_INVALID_FD) { pMI->txt_errno = errno; @@ -202,7 +222,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) return pMI->txt_data; pMI->txt_errno = errno; - close( pMI->txt_zero_fd ); + close(pMI->txt_zero_fd); pMI->txt_zero_fd = -1; } #endif @@ -211,8 +231,8 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) } { - void* p = AGALOC( pMI->txt_size+1, "file text" ); - memcpy( p, pMI->txt_data, pMI->txt_size ); + void* p = AGALOC(pMI->txt_size+1, "file text"); + memcpy(p, pMI->txt_data, pMI->txt_size); ((char*)p)[pMI->txt_size] = NUL; munmap(pMI->txt_data, pMI->txt_size ); pMI->txt_data = p; @@ -222,7 +242,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) #else /* * * * * * no HAVE_MMAP * * * * * */ - pMI->txt_data = AGALOC( pMI->txt_size+1, "file text" ); + pMI->txt_data = AGALOC(pMI->txt_size+1, "file text"); if (pMI->txt_data == NULL) { pMI->txt_errno = ENOMEM; goto fail_return; @@ -233,12 +253,12 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) char* pz = pMI->txt_data; while (sz > 0) { - ssize_t rdct = read( pMI->txt_fd, pz, sz ); + ssize_t rdct = read(pMI->txt_fd, pz, sz); if (rdct <= 0) { pMI->txt_errno = errno; - fprintf( stderr, zFSErrReadFile, - errno, strerror( errno ), pzFile ); - free( pMI->txt_data ); + fprintf(stderr, zFSErrReadFile, + errno, strerror(errno), pzFile); + free(pMI->txt_data); goto fail_return; } @@ -261,7 +281,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) fail_return: if (pMI->txt_fd >= 0) { - close( pMI->txt_fd ); + close(pMI->txt_fd); pMI->txt_fd = -1; } errno = pMI->txt_errno; @@ -290,7 +310,7 @@ text_mmap( char const* pzFile, int prot, int flags, tmap_info_t* pMI ) * err: Any error code issued by munmap(2) or close(2) is possible. =*/ int -text_munmap( tmap_info_t* pMI ) +text_munmap(tmap_info_t* pMI) { #ifdef HAVE_MMAP int res = 0; @@ -306,26 +326,26 @@ text_munmap( tmap_info_t* pMI ) if (lseek(pMI->txt_fd, (size_t)0, SEEK_SET) != 0) goto error_return; - res = (write( pMI->txt_fd, pMI->txt_data, pMI->txt_size ) < 0) + res = (write(pMI->txt_fd, pMI->txt_data, pMI->txt_size) < 0) ? errno : 0; } - AGFREE( pMI->txt_data ); + AGFREE(pMI->txt_data); errno = res; } else { - res = munmap( pMI->txt_data, pMI->txt_full_size ); + res = munmap(pMI->txt_data, pMI->txt_full_size); } if (res != 0) goto error_return; - res = close( pMI->txt_fd ); + res = close(pMI->txt_fd); if (res != 0) goto error_return; pMI->txt_fd = -1; errno = 0; if (pMI->txt_zero_fd != -1) { - res = close( pMI->txt_zero_fd ); + res = close(pMI->txt_zero_fd); pMI->txt_zero_fd = -1; } @@ -341,14 +361,14 @@ text_munmap( tmap_info_t* pMI ) * THEN rewrite the data. */ if ( FILE_WRITABLE(pMI->txt_prot, pMI->txt_flags) - && (lseek( pMI->txt_fd, 0, SEEK_SET ) >= 0) ) { - write( pMI->txt_fd, pMI->txt_data, pMI->txt_size ); + && (lseek(pMI->txt_fd, 0, SEEK_SET) >= 0) ) { + write(pMI->txt_fd, pMI->txt_data, pMI->txt_size); } - close( pMI->txt_fd ); + close(pMI->txt_fd); pMI->txt_fd = -1; pMI->txt_errno = errno; - free( pMI->txt_data ); + free(pMI->txt_data); return pMI->txt_errno; #endif /* HAVE_MMAP */ |