Index: Makefile.inc =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/Makefile.inc,v retrieving revision 1.40 diff -u -p -u -r1.40 Makefile.inc --- Makefile.inc 22 Dec 2010 16:59:10 -0000 1.40 +++ Makefile.inc 16 Mar 2012 19:16:19 -0000 @@ -45,6 +45,7 @@ MLINKS+=fread.3 fwrite.3 MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \ fseek.3 ftello.3 fseek.3 rewind.3 MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3 +MLINKS+=funopen.3 funopen2.3 funopen.3 fropen2.3 funopen.3 fwopen2.3 MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 getchar.3 \ getc.3 getchar_unlocked.3 getc.3 getw.3 MLINKS+=getdelim.3 getline.3 Index: fclose.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fclose.c,v retrieving revision 1.18 diff -u -p -u -r1.18 fclose.c --- fclose.c 15 Mar 2012 18:22:30 -0000 1.18 +++ fclose.c 16 Mar 2012 19:16:19 -0000 @@ -61,7 +61,7 @@ fclose(FILE *fp) } FLOCKFILE(fp); WCIO_FREE(fp); - r = fp->_flags & __SWR ? __sflush(fp) : 0; + r = (fp->_flags & __SWR) ? FPFLUSH(fp) : 0; if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) r = EOF; if (fp->_flags & __SMBF) Index: fdopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fdopen.c,v retrieving revision 1.16 diff -u -p -u -r1.16 fdopen.c --- fdopen.c 15 Mar 2012 18:22:30 -0000 1.16 +++ fdopen.c 16 Mar 2012 19:16:19 -0000 @@ -116,6 +116,7 @@ fdopen(int fd, const char *mode) fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; + fp->_flush = __sflush; fp->_close = __sclose; return fp; } Index: fflush.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fflush.c,v retrieving revision 1.17 diff -u -p -u -r1.17 fflush.c --- fflush.c 15 Mar 2012 18:22:30 -0000 1.17 +++ fflush.c 16 Mar 2012 19:16:19 -0000 @@ -60,7 +60,7 @@ fflush(FILE *fp) if (fp == NULL) { rwlock_rdlock(&__sfp_lock); - r = _fwalk(__sflush); + r = _fwalk(__sfpflush); rwlock_unlock(&__sfp_lock); return r; } @@ -70,17 +70,18 @@ fflush(FILE *fp) errno = EBADF; r = EOF; } else { - r = __sflush(fp); + r = FPFLUSH(fp); } FUNLOCKFILE(fp); return r; } int -__sflush(FILE *fp) +__sfpflush(FILE *fp) { unsigned char *p; - int n, t; + size_t n; + ssize_t t; _DIAGASSERT(fp != NULL); @@ -92,8 +93,8 @@ __sflush(FILE *fp) return 0; ptrdiff_t tp = fp->_p - p; - _DIAGASSERT(__type_fit(int, tp)); - n = (int)tp; /* write this much */ + _DIAGASSERT(__type_fit(ssize_t, tp)); + n = (ssize_t)tp; /* write this much */ /* * Set these immediately to avoid problems with longjmp and to allow Index: findfp.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/findfp.c,v retrieving revision 1.27 diff -u -p -u -r1.27 findfp.c --- findfp.c 15 Mar 2012 18:22:30 -0000 1.27 +++ findfp.c 16 Mar 2012 19:16:19 -0000 @@ -56,13 +56,29 @@ int __sdidinit; #define NDYNAMIC 10 /* add ten more whenever necessary */ -#define std(flags, file) \ -/* p r w flags file bf lfbsize cookie close */ \ - { NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \ -/* read seek write ext up */ \ - __sread, __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \ -/* ur ubuf, nbuf lb blksize offset */ \ - 0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (off_t)0 } +#define std(flags, file) { \ + ._p = NULL, \ + ._r = 0, \ + ._w = 0, \ + ._flags = (flags), \ + ._file = (file), \ + ._bf = { ._base = NULL, ._size = 0 }, \ + ._lbfsize = 0, \ + ._cookie = __sF + (file), \ + ._close = __sclose, \ + ._read = __sread, \ + ._seek = __sseek, \ + ._write = __swrite, \ + ._ext = { ._base = (void *)(__sFext + (file)), ._size = 0 }, \ + ._up = NULL, \ + ._ur = 0, \ + ._ubuf = { [0] = '\0', [1] = '\0', [2] = '\0' }, \ + ._nbuf = { [0] = '\0' }, \ + ._flush = __sflush, \ + ._lb_unused = { '\0' }, \ + ._blksize = 0, \ + ._offset = (off_t)0, \ +} /* the usual - (stdin + stdout + stderr) */ static FILE usual[FOPEN_MAX - 3]; @@ -99,8 +115,8 @@ moreglue(int n) struct __sfileext *pext; static FILE empty; - g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) - + n * sizeof(struct __sfileext)); + g = malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE) + + n * sizeof(struct __sfileext)); if (g == NULL) return NULL; p = (FILE *)ALIGN((u_long)(g + 1)); Index: fmemopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fmemopen.c,v retrieving revision 1.6 diff -u -p -u -r1.6 fmemopen.c --- fmemopen.c 22 Jan 2012 18:36:17 -0000 1.6 +++ fmemopen.c 16 Mar 2012 19:16:19 -0000 @@ -46,11 +46,11 @@ struct fmemopen_cookie { char *head, *tail, *cur, *eob; }; -static int -fmemopen_read(void *cookie, char *buf, int nbytes) +static ssize_t +fmemopen_read(void *cookie, void *buf, size_t nbytes) { struct fmemopen_cookie *p; - char *s; + char *s, *b = buf; _DIAGASSERT(cookie != NULL); _DIAGASSERT(buf != NULL && nbytes > 0); @@ -60,17 +60,18 @@ fmemopen_read(void *cookie, char *buf, i do { if (p->cur == p->tail) break; - *buf++ = *p->cur++; + *b++ = *p->cur++; } while (--nbytes > 0); - return (int)(p->cur - s); + return (ssize_t)(p->cur - s); } -static int -fmemopen_write(void *cookie, const char *buf, int nbytes) +static ssize_t +fmemopen_write(void *cookie, const void *buf, size_t nbytes) { struct fmemopen_cookie *p; char *s; + const char *b = buf; _DIAGASSERT(cookie != NULL); _DIAGASSERT(buf != NULL && nbytes > 0); @@ -81,20 +82,34 @@ fmemopen_write(void *cookie, const char s = p->cur; do { if (p->cur == p->tail - 1) { - if (*buf == '\0') { + if (*b == '\0') { *p->cur++ = '\0'; goto ok; } break; } - *p->cur++ = *buf++; + *p->cur++ = *b++; } while (--nbytes > 0); *p->cur = '\0'; ok: if (p->cur > p->eob) p->eob = p->cur; - return (int)(p->cur - s); + return (ssize_t)(p->cur - s); +} + +static int +fmemopen_flush(void *cookie) +{ + struct fmemopen_cookie *p; + + _DIAGASSERT(cookie != NULL); + + p = (struct fmemopen_cookie *)cookie; + if (p->cur >= p->tail) + return -1; + *p->cur = '\0'; + return 0; } static off_t @@ -203,9 +218,10 @@ fmemopen(void * __restrict buf, size_t s cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head; fp->_flags = flags; - fp->_write = (flags & __SRD) ? NULL : &fmemopen_write; - fp->_read = (flags & __SWR) ? NULL : &fmemopen_read; - fp->_seek = &fmemopen_seek; + fp->_write = (flags & __SRD) ? NULL : fmemopen_write; + fp->_read = (flags & __SWR) ? NULL : fmemopen_read; + fp->_seek = fmemopen_seek; + fp->_flush = fmemopen_flush; fp->_cookie = (void *)cookie; return fp; Index: fopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fopen.c,v retrieving revision 1.15 diff -u -p -u -r1.15 fopen.c --- fopen.c 15 Mar 2012 18:22:30 -0000 1.15 +++ fopen.c 16 Mar 2012 19:16:19 -0000 @@ -98,6 +98,7 @@ fopen(const char *file, const char *mode fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; + fp->_flush = __sflush; fp->_close = __sclose; /* Index: freopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/freopen.c,v retrieving revision 1.18 diff -u -p -u -r1.18 freopen.c --- freopen.c 15 Mar 2012 18:22:30 -0000 1.18 +++ freopen.c 16 Mar 2012 19:16:19 -0000 @@ -93,7 +93,7 @@ freopen(const char *file, const char *mo } else { /* flush the stream; ANSI doesn't require this. */ if (fp->_flags & __SWR) - (void) __sflush(fp); + (void)FPFLUSH(fp); /* if close is NULL, closing is a no-op, hence pointless */ isopen = fp->_close != NULL; if ((wantfd = __sfileno(fp)) == -1 && isopen) { @@ -187,6 +187,7 @@ freopen(const char *file, const char *mo fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; + fp->_flush = __sflush; fp->_close = __sclose; /* Index: fseeko.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fseeko.c,v retrieving revision 1.11 diff -u -p -u -r1.11 fseeko.c --- fseeko.c 15 Mar 2012 18:22:30 -0000 1.11 +++ fseeko.c 16 Mar 2012 19:16:19 -0000 @@ -97,7 +97,7 @@ fseeko(FILE *fp, off_t offset, int whenc * we have to first find the current stream offset a la * ftell (see ftell for details). */ - __sflush(fp); /* may adjust seek offset on append stream */ + (void)FPFLUSH(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) curoff = fp->_offset; else { @@ -248,8 +248,7 @@ fseeko(FILE *fp, off_t offset, int whenc * do it. Allow the seek function to change fp->_bf._base. */ dumb: - if (__sflush(fp) || - (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) { + if (FPFLUSH(fp) || (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) { FUNLOCKFILE(fp); return -1; } Index: ftell.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/ftell.c,v retrieving revision 1.19 diff -u -p -u -r1.19 ftell.c --- ftell.c 15 Mar 2012 18:22:30 -0000 1.19 +++ ftell.c 16 Mar 2012 19:16:19 -0000 @@ -68,7 +68,7 @@ ftell(FILE *fp) * Find offset of underlying I/O object, then * adjust for buffered bytes. */ - __sflush(fp); /* may adjust seek offset on append stream */ + (void)FPFLUSH(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) pos = fp->_offset; else { Index: ftello.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/ftello.c,v retrieving revision 1.6 diff -u -p -u -r1.6 ftello.c --- ftello.c 15 Mar 2012 18:22:30 -0000 1.6 +++ ftello.c 16 Mar 2012 19:16:19 -0000 @@ -69,7 +69,7 @@ ftello(FILE *fp) * Find offset of underlying I/O object, then * adjust for buffered bytes. */ - __sflush(fp); /* may adjust seek offset on append stream */ + (void)FPFLUSH(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) pos = fp->_offset; else { Index: funopen.3 =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/funopen.3,v retrieving revision 1.18 diff -u -p -u -r1.18 funopen.3 --- funopen.3 22 Jan 2012 19:13:48 -0000 1.18 +++ funopen.3 16 Mar 2012 19:16:19 -0000 @@ -36,8 +36,11 @@ .Os .Sh NAME .Nm funopen , +.Nm funopen2 , .Nm fropen , -.Nm fwopen +.Nm fropen2 , +.Nm fwopen , +.Nm fwopen2 .Nd open a stream .Sh LIBRARY .Lb libc @@ -45,10 +48,15 @@ .In stdio.h .Ft FILE * .Fn funopen "void *cookie" "int (*readfn)(void *, char *, int)" "int (*writefn)(void *, const char *, int)" "off_t (*seekfn)(void *, off_t, int)" "int (*closefn)(void *)" +.Fn funopen2 "void *cookie" "ssize_t (*readfn)(void *, void *, size_t)" "ssize_t (*writefn)(void *, const void *, size_t)" "off_t (*seekfn)(void *, off_t, int)" "int (*flushfn)(void *)" "int (*closefn)(void *)" .Ft FILE * .Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)" .Ft FILE * +.Fn fropen2 "void *cookie" "ssize_t (*readfn)(void *, void *, size_t)" +.Ft FILE * .Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)" +.Ft FILE * +.Fn fwopen2 "void *cookie" "ssize_t (*writefn)(void *, const void *, size_t)" .Sh DESCRIPTION The .Fn funopen @@ -68,6 +76,14 @@ These functions will be used to read, write, seek and close the new stream. .Pp +The +.Fn funopen2 +function provides sightly different read and write signatures, which match +better the corresponding system calls, plus the ability to override the +streams default flushing function. +If a flushing function is not provided, then the default stream flushing +function is used. +.Pp In general, omitting a function means that any attempt to perform the associated operation on the resulting stream will fail. If the close function is omitted, closing the stream will flush @@ -161,6 +177,10 @@ The .Fn funopen functions first appeared in .Bx 4.4 . +.The +.Fn funopen2 +functions first appeared in +.Nx 7 . .Sh CAVEATS All three functions are specific to .Nx Index: funopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/funopen.c,v retrieving revision 1.12 diff -u -p -u -r1.12 funopen.c --- funopen.c 15 Mar 2012 18:22:30 -0000 1.12 +++ funopen.c 16 Mar 2012 19:16:19 -0000 @@ -43,14 +43,17 @@ __RCSID("$NetBSD: funopen.c,v 1.12 2012/ #include #include +#include +#include #include "reentrant.h" #include "local.h" FILE * -funopen(const void *cookie, - int (*readfn)(void *, char *, int), - int (*writefn)(void *, const char *, int), +funopen2(const void *cookie, + ssize_t (*readfn)(void *, void *, size_t), + ssize_t (*writefn)(void *, const void *, size_t), off_t (*seekfn)(void *, off_t, int), + int (*flushfn)(void *), int (*closefn)(void *)) { FILE *fp; @@ -76,6 +79,89 @@ funopen(const void *cookie, fp->_read = readfn; fp->_write = writefn; fp->_seek = seekfn; + fp->_flush = flushfn; fp->_close = closefn; return fp; } + +typedef struct { + void *cookie; + int (*readfn)(void *, char *, int); + int (*writefn)(void *, const char *, int); + off_t (*seekfn)(void *, off_t, int); + int (*closefn)(void *); +} dookie_t; + +static ssize_t +creadfn(void *dookie, void *buf, size_t len) +{ + dookie_t *d = dookie; + if (len > INT_MAX) + len = INT_MAX; + return (*d->readfn)(d->cookie, buf, (int)len); +} + +static ssize_t +cwritefn(void *dookie, const void *buf, size_t len) +{ + dookie_t *d = dookie; + ssize_t nr; + size_t l = len; + const char *b = buf; + + while (l) { + size_t nw = l > INT_MAX ? INT_MAX : l; + nr = (*d->writefn)(d->cookie, buf, (int)nw); + if (nr == -1) { + if (len == l) + return -1; + else + return len - l; + } + b += nr; + l -= nr; + } + return len; +} + +static off_t +cseekfn(void *dookie, off_t off, int whence) +{ + dookie_t *d = dookie; + return (*d->seekfn)(d->cookie, off, whence); +} + +static int +cclosefn(void *dookie) +{ + dookie_t *d = dookie; + void *c = d->cookie; + int (*cf)(void *) = d->closefn; + free(dookie); + return (*cf)(c); +} + +FILE * +funopen(const void *cookie, + int (*readfn)(void *, char *, int), + int (*writefn)(void *, const char *, int), + off_t (*seekfn)(void *, off_t, int), + int (*closefn)(void *)) +{ + dookie_t *d; + FILE *fp; + + if ((d = malloc(sizeof(*d))) == NULL) + return NULL; + + d->cookie = __UNCONST(cookie); + d->readfn = readfn; + d->writefn = writefn; + d->seekfn = seekfn; + d->closefn = closefn; + fp = funopen2(d, creadfn, cwritefn, cseekfn, NULL, cclosefn); + if (fp != NULL) + return fp; + free(d); + return NULL; + } Index: fvwrite.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fvwrite.c,v retrieving revision 1.24 diff -u -p -u -r1.24 fvwrite.c --- fvwrite.c 15 Mar 2012 18:22:30 -0000 1.24 +++ fvwrite.c 16 Mar 2012 19:16:19 -0000 @@ -63,14 +63,15 @@ __sfvwrite(FILE *fp, struct __suio *uio) size_t len; char *p; struct __siov *iov; - int w, s; + int s; + ssize_t w; char *nl; size_t nlknown, nldist; _DIAGASSERT(fp != NULL); _DIAGASSERT(uio != NULL); - if ((int)uio->uio_resid < 0) { + if ((ssize_t)uio->uio_resid < 0) { errno = EINVAL; return EOF; } @@ -102,8 +103,7 @@ __sfvwrite(FILE *fp, struct __suio *uio) */ do { GETIOV(;); - w = (*fp->_write)(fp->_cookie, p, - (int)MIN(len, BUFSIZ)); + w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ)); if (w <= 0) goto err; p += w; @@ -146,11 +146,11 @@ __sfvwrite(FILE *fp, struct __suio *uio) w = fp->_w; if (fp->_flags & __SSTR) { if (len < (size_t)w) - w = (int)len; + w = len; COPY(w); /* copy MIN(fp->_w,len), */ - fp->_w -= w; + fp->_w -= (int)w; fp->_p += w; - w = (int)len; /* but pretend copied all */ + w = len; /* but pretend copied all */ } else if (fp->_p > fp->_bf._base && len > (size_t)w) { /* fill and flush */ COPY(w); @@ -160,14 +160,14 @@ __sfvwrite(FILE *fp, struct __suio *uio) goto err; } else if (len >= (size_t)(w = fp->_bf._size)) { /* write directly */ - w = (*fp->_write)(fp->_cookie, p, w); + w = (*fp->_write)(fp->_cookie, p, (size_t)w); if (w <= 0) goto err; } else { /* fill and done */ - w = (int)len; + w = len; COPY(w); - fp->_w -= w; + fp->_w -= (int)w; fp->_p += w; } p += w; @@ -199,13 +199,13 @@ __sfvwrite(FILE *fp, struct __suio *uio) if (fflush(fp)) goto err; } else if (s >= (w = fp->_bf._size)) { - w = (*fp->_write)(fp->_cookie, p, w); + w = (*fp->_write)(fp->_cookie, p, (size_t)w); if (w <= 0) goto err; } else { w = s; COPY(w); - fp->_w -= w; + fp->_w -= (int)w; fp->_p += w; } if ((nldist -= w) == 0) { Index: local.h =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/local.h,v retrieving revision 1.33 diff -u -p -u -r1.33 local.h --- local.h 15 Mar 2012 13:23:10 -0000 1.33 +++ local.h 16 Mar 2012 19:16:20 -0000 @@ -46,13 +46,14 @@ * in particular, macros and private variables. */ -extern int __sflush(FILE *); +extern int __sfpflush(FILE *); extern FILE *__sfp(void); extern void __sfpinit(FILE *); extern int __srefill(FILE *); -extern int __sread(void *, char *, int); -extern int __swrite(void *, char const *, int); +extern ssize_t __sread(void *, void *, size_t); +extern ssize_t __swrite(void *, const void *, size_t); extern off_t __sseek(void *, off_t, int); +extern int __sflush(void *); extern int __sclose(void *); extern void __sinit(void); extern void _cleanup(void); @@ -112,6 +113,9 @@ extern int __vfwscanf_unlocked(FILE * _ _EXT(fp)->_fgetstr_len = 0; \ } +#define FPFLUSH(fp) \ + ((fp)->_flush ? (*(fp)->_flush)((fp)->_cookie) : __sfpflush(fp)) + extern void __flockfile_internal(FILE *, int); extern void __funlockfile_internal(FILE *, int); Index: refill.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/refill.c,v retrieving revision 1.15 diff -u -p -u -r1.15 refill.c --- refill.c 15 Mar 2012 18:22:30 -0000 1.15 +++ refill.c 16 Mar 2012 19:16:20 -0000 @@ -59,7 +59,7 @@ lflush(FILE *fp) _DIAGASSERT(fp != NULL); if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR)) - return __sflush(fp); + return FPFLUSH(fp); return 0; } @@ -92,7 +92,7 @@ __srefill(FILE *fp) } /* switch to reading */ if (fp->_flags & __SWR) { - if (__sflush(fp)) + if (FPFLUSH(fp)) return EOF; fp->_flags &= ~__SWR; fp->_w = 0; @@ -129,7 +129,8 @@ __srefill(FILE *fp) rwlock_unlock(&__sfp_lock); } fp->_p = fp->_bf._base; - fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size); + fp->_r = (int)(*fp->_read)(fp->_cookie, (char *)fp->_p, + (size_t)fp->_bf._size); fp->_flags &= ~__SMOD; /* buffer contents are again pristine */ if (fp->_r <= 0) { if (fp->_r == 0) Index: setvbuf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/setvbuf.c,v retrieving revision 1.19 diff -u -p -u -r1.19 setvbuf.c --- setvbuf.c 15 Mar 2012 18:22:30 -0000 1.19 +++ setvbuf.c 16 Mar 2012 19:16:20 -0000 @@ -80,7 +80,7 @@ setvbuf(FILE *fp, char *buf, int mode, s * a seek. */ ret = 0; - (void)__sflush(fp); + (void)FPFLUSH(fp); if (HASUB(fp)) FREEUB(fp); WCIO_FREE(fp); Index: sscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/sscanf.c,v retrieving revision 1.19 diff -u -p -u -r1.19 sscanf.c --- sscanf.c 15 Mar 2012 18:22:30 -0000 1.19 +++ sscanf.c 16 Mar 2012 19:16:20 -0000 @@ -51,8 +51,8 @@ __RCSID("$NetBSD: sscanf.c,v 1.19 2012/0 #include "local.h" /* ARGSUSED */ -static int -eofread(void *cookie, char *buf, int len) +static ssize_t +eofread(void *cookie, void *buf, size_t len) { return 0; Index: stdio.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/stdio.c,v retrieving revision 1.19 diff -u -p -u -r1.19 stdio.c --- stdio.c 15 Mar 2012 18:22:30 -0000 1.19 +++ stdio.c 16 Mar 2012 19:16:20 -0000 @@ -56,37 +56,40 @@ __RCSID("$NetBSD: stdio.c,v 1.19 2012/03 * Small standard I/O/seek/close functions. * These maintain the `known seek offset' for seek optimisation. */ -int -__sread(void *cookie, char *buf, int n) +ssize_t +__sread(void *cookie, void *buf, size_t n) { FILE *fp = cookie; ssize_t ret; - _DIAGASSERT(fp != NULL); + _DIAGASSERT(cookie != NULL); + _DIAGASSERT(cookie == fp->_cookie); _DIAGASSERT(buf != NULL); - ret = read(__sfileno(fp), buf, (size_t)n); + ret = read(__sfileno(fp), buf, n); /* if the read succeeded, update the current offset */ if (ret >= 0) fp->_offset += ret; else fp->_flags &= ~__SOFF; /* paranoia */ - return (int)ret; + return ret; } -int -__swrite(void *cookie, char const *buf, int n) +ssize_t +__swrite(void *cookie, const void *buf, size_t n) { FILE *fp = cookie; _DIAGASSERT(cookie != NULL); + _DIAGASSERT(cookie == fp->_cookie); _DIAGASSERT(buf != NULL); if (fp->_flags & __SAPP) - (void) lseek(__sfileno(fp), (off_t)0, SEEK_END); + if (lseek(__sfileno(fp), (off_t)0, SEEK_END) == (off_t)-1) + return -1; fp->_flags &= ~__SOFF; /* in case FAPPEND mode is set */ - return (int)write(__sfileno(fp), buf, (size_t)n); + return write(__sfileno(fp), buf, n); } off_t @@ -95,10 +98,11 @@ __sseek(void *cookie, off_t offset, int FILE *fp = cookie; off_t ret; - _DIAGASSERT(fp != NULL); + _DIAGASSERT(cookie != NULL); + _DIAGASSERT(cookie == fp->_cookie); ret = lseek(__sfileno(fp), offset, whence); - if (ret == -1L) + if (ret == (off_t)-1L) fp->_flags &= ~__SOFF; else { fp->_flags |= __SOFF; @@ -108,11 +112,23 @@ __sseek(void *cookie, off_t offset, int } int -__sclose(cookie) - void *cookie; +__sflush(void *cookie) +{ + FILE *fp = cookie; + + _DIAGASSERT(cookie != NULL); + _DIAGASSERT(cookie == fp->_cookie); + + return __sfpflush(fp); +} + +int +__sclose(void *cookie) { + FILE *fp = cookie; _DIAGASSERT(cookie != NULL); + _DIAGASSERT(cookie == fp->_cookie); - return close(__sfileno((FILE *)cookie)); + return close(__sfileno(fp)); } Index: ungetc.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/ungetc.c,v retrieving revision 1.17 diff -u -p -u -r1.17 ungetc.c --- ungetc.c 15 Mar 2012 18:22:30 -0000 1.17 +++ ungetc.c 16 Mar 2012 19:16:20 -0000 @@ -111,7 +111,7 @@ ungetc(int c, FILE *fp) return EOF; } if (fp->_flags & __SWR) { - if (__sflush(fp)) { + if (FPFLUSH(fp)) { FUNLOCKFILE(fp); return EOF; } Index: vdprintf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vdprintf.c,v retrieving revision 1.2 diff -u -p -u -r1.2 vdprintf.c --- vdprintf.c 17 Jul 2011 20:54:34 -0000 1.2 +++ vdprintf.c 16 Mar 2012 19:16:20 -0000 @@ -107,6 +107,7 @@ vdprintf(int fd, const char * __restrict f._read = NULL; f._write = __swrite; f._seek = NULL; + f._flush = __sflush; f._close = NULL; if ((ret = vfprintf(&f, fmt, ap)) < 0) Index: vfwprintf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vfwprintf.c,v retrieving revision 1.28 diff -u -p -u -r1.28 vfwprintf.c --- vfwprintf.c 15 Mar 2012 18:22:30 -0000 1.28 +++ vfwprintf.c 16 Mar 2012 19:16:20 -0000 @@ -170,6 +170,7 @@ __sbprintf(FILE *fp, const CHAR_T *fmt, fake._file = fp->_file; fake._cookie = fp->_cookie; fake._write = fp->_write; + fake._flush = fp->_flush; /* set up the buffer */ fake._bf._base = fake._p = buf; Index: vsscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vsscanf.c,v retrieving revision 1.18 diff -u -p -u -r1.18 vsscanf.c --- vsscanf.c 15 Mar 2012 18:22:31 -0000 1.18 +++ vsscanf.c 16 Mar 2012 19:16:20 -0000 @@ -49,8 +49,8 @@ __RCSID("$NetBSD: vsscanf.c,v 1.18 2012/ #include "local.h" /* ARGSUSED */ -static int -eofread(void *cookie, char *buf, int len) +static ssize_t +eofread(void *cookie, void *buf, size_t len) { return 0; } Index: vswscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vswscanf.c,v retrieving revision 1.8 diff -u -p -u -r1.8 vswscanf.c --- vswscanf.c 15 Mar 2012 18:22:31 -0000 1.8 +++ vswscanf.c 16 Mar 2012 19:16:20 -0000 @@ -56,9 +56,9 @@ __RCSID("$NetBSD: vswscanf.c,v 1.8 2012/ #include "reentrant.h" #include "local.h" -static int +static ssize_t /*ARGSUSED*/ -eofread(void *cookie, char *buf, int len) +eofread(void *cookie, void *buf, size_t len) { return 0;