--- libaitio/src/bufio.c 2016/08/11 12:25:51 1.9.8.1 +++ libaitio/src/bufio.c 2016/08/11 12:51:52 1.9.8.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: bufio.c,v 1.9.8.1 2016/08/11 12:25:51 misho Exp $ +* $Id: bufio.c,v 1.9.8.2 2016/08/11 12:51:52 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -53,7 +53,6 @@ unmap_cf(struct tagBufIO *buf) munmap(buf->buf_base, buf->buf_size); } -#ifndef __linux__ static int cf_(struct tagBufIO *buf) { @@ -71,7 +70,7 @@ cf_(struct tagBufIO *buf) return 0; } -#ifdef __NetBSD__ +#if defined(__NetBSD__) || defined(__linux__) static off_t sf_lim(struct tagBufIO *buf, off_t pos, int w) #else @@ -140,7 +139,7 @@ wf_lim(struct tagBufIO *buf, const char *dat, int siz) return siz; } -#ifdef __NetBSD__ +#if defined(__NetBSD__) || defined(__linux__) static off_t sf_inf(struct tagBufIO *buf, off_t pos, int w) #else @@ -241,7 +240,6 @@ wf_inf(struct tagBufIO *buf, const char *dat, int siz) buf->buf_offset += siz; return siz; } -#endif /* @@ -256,6 +254,9 @@ io_fmemopen(void ** __restrict base, off_t basesize) { FILE *f = NULL; struct tagBufIO *buf; +#ifdef __linux__ + cookie_io_functions_t cookie; +#endif if (!base) { io_SetErr(EINVAL, "Invalid base argument ..."); @@ -270,7 +271,6 @@ io_fmemopen(void ** __restrict base, off_t basesize) memset(buf, 0, sizeof(struct tagBufIO)); if (!*base) { -#ifndef __linux__ *base = e_malloc(basesize); if (!*base) { LOGERR; @@ -279,17 +279,13 @@ io_fmemopen(void ** __restrict base, off_t basesize) } else memset(*base, 0, basesize); - buf->buf_base = *base; - buf->buf_size = basesize; -#endif buf->buf_mode = BUFIO_MODE_INFINIT; - } else { - buf->buf_base = *base; - buf->buf_size = basesize; - + } else buf->buf_mode = BUFIO_MODE_LIMIT; - } + buf->buf_base = *base; + buf->buf_size = basesize; + #ifndef __linux__ #ifdef __NetBSD__ if (buf->buf_mode == BUFIO_MODE_INFINIT) @@ -315,26 +311,29 @@ io_fmemopen(void ** __restrict base, off_t basesize) (int (*)(void *)) cf_); #endif #else - if (buf->buf_mode == BUFIO_MODE_INFINIT) - f = open_memstream((char**) &buf->buf_base, (size_t*) &buf->buf_size); - else - f = fmemopen(buf->buf_base, buf->buf_size, "r+"); + if (buf->buf_mode == BUFIO_MODE_INFINIT) { + cookie.read = (cookie_read_function_t*) rf_lim; + cookie.write = (cookie_write_function_t*) wf_inf; + cookie.seek = (cookie_seek_function_t*) sf_inf; + cookie.close = (cookie_close_function_t*) cf_; + } else { + cookie.read = (cookie_read_function_t*) rf_lim; + cookie.write = (cookie_write_function_t*) wf_lim; + cookie.seek = (cookie_seek_function_t*) sf_lim; + cookie.close = (cookie_close_function_t*) cf_; + } + + f = fopencookie(buf, "r+", cookie); #endif if (!f) { LOGERR; -#ifndef __linux__ if (buf->buf_mode == BUFIO_MODE_INFINIT) { e_free(*base); *base = NULL; } -#endif e_free(buf); return NULL; } -#ifdef __linux__ - if (buf->buf_mode == BUFIO_MODE_INFINIT) - *base = buf->buf_base; -#endif return f; } @@ -358,6 +357,14 @@ io_fmapopen(const char *csFile, int mode, int perm, in void *base; off_t basesize; int fd = -1; +#ifdef __linux__ + cookie_io_functions_t cookie = { + .read = (cookie_read_function_t*) rf_lim, + .write = (cookie_write_function_t*) wf_lim, + .seek = (cookie_seek_function_t*) sf_lim, + .close = (cookie_close_function_t*) cf_ + }; +#endif if (csFile) { fd = open(csFile, mode, perm); @@ -419,7 +426,7 @@ io_fmapopen(const char *csFile, int mode, int perm, in (int (*)(void *)) cf_); #endif #else - f = fmemopen(buf->buf_base, buf->buf_size, "r+"); + f = fopencookie(buf, "r+", cookie); #endif if (!f) { LOGERR;