? include/netdb.h.new ? lib/libc/stdio/o ? lib/libc/stdio/patch1 Index: include/stdio.h =================================================================== RCS file: /cvsroot/src/include/stdio.h,v retrieving revision 1.80 diff -u -p -u -r1.80 stdio.h --- include/stdio.h 22 Jan 2012 18:36:16 -0000 1.80 +++ include/stdio.h 15 Mar 2012 18:45:56 -0000 @@ -121,9 +121,9 @@ typedef struct __sFILE { /* operations */ void *_cookie; /* cookie passed to io functions */ int (*_close)(void *); - int (*_read) (void *, char *, int); + ssize_t (*_read) (void *, void *, size_t); __off_t (*_seek) (void *, __off_t, int); - int (*_write)(void *, const char *, int); + ssize_t (*_write)(void *, const void *, size_t); /* file extension */ struct __sbuf _ext; @@ -427,13 +427,20 @@ __END_DECLS */ __BEGIN_DECLS FILE *funopen(const void *, - int (*)(void *, char *, int), - int (*)(void *, const char *, int), - off_t (*)(void *, off_t, int), - int (*)(void *)); + int (*)(void *, char *, int), + int (*)(void *, const char *, int), + off_t (*)(void *, off_t, int), + int (*)(void *)); +FILE *funopen2(const void *, + ssize_t (*)(void *, void *, size_t), + ssize_t (*)(void *, const void *, size_t), + off_t (*)(void *, off_t, int), + int (*)(void *)); __END_DECLS #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) +#define fropen2(cookie, fn) funopen2(cookie, fn, 0, 0, 0) +#define fwopen2(cookie, fn) funopen2(cookie, 0, fn, 0, 0) #endif /* _NETBSD_SOURCE */ /* ? lib/libc/stdio/o ? lib/libc/stdio/patch1 Index: lib/libc/stdio/fflush.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fflush.c,v retrieving revision 1.17 diff -u -p -u -r1.17 fflush.c --- lib/libc/stdio/fflush.c 15 Mar 2012 18:22:30 -0000 1.17 +++ lib/libc/stdio/fflush.c 15 Mar 2012 18:47:06 -0000 @@ -80,7 +80,8 @@ int __sflush(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: lib/libc/stdio/fmemopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fmemopen.c,v retrieving revision 1.6 diff -u -p -u -r1.6 fmemopen.c --- lib/libc/stdio/fmemopen.c 22 Jan 2012 18:36:17 -0000 1.6 +++ lib/libc/stdio/fmemopen.c 15 Mar 2012 18:47:06 -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,20 @@ 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 off_t @@ -203,9 +204,9 @@ 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->_cookie = (void *)cookie; return fp; Index: lib/libc/stdio/funopen.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/funopen.c,v retrieving revision 1.12 diff -u -p -u -r1.12 funopen.c --- lib/libc/stdio/funopen.c 15 Mar 2012 18:22:30 -0000 1.12 +++ lib/libc/stdio/funopen.c 15 Mar 2012 18:47:06 -0000 @@ -43,13 +43,14 @@ __RCSID("$NetBSD: funopen.c,v 1.12 2012/ #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 (*closefn)(void *)) { @@ -79,3 +80,85 @@ funopen(const void *cookie, 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, cclosefn); + if (fp != NULL) + return fp; + free(d); + return NULL; + } Index: lib/libc/stdio/fvwrite.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/fvwrite.c,v retrieving revision 1.24 diff -u -p -u -r1.24 fvwrite.c --- lib/libc/stdio/fvwrite.c 15 Mar 2012 18:22:30 -0000 1.24 +++ lib/libc/stdio/fvwrite.c 15 Mar 2012 18:47:06 -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: lib/libc/stdio/local.h =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/local.h,v retrieving revision 1.33 diff -u -p -u -r1.33 local.h --- lib/libc/stdio/local.h 15 Mar 2012 13:23:10 -0000 1.33 +++ lib/libc/stdio/local.h 15 Mar 2012 18:47:06 -0000 @@ -50,8 +50,8 @@ extern int __sflush(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 __sclose(void *); extern void __sinit(void); Index: lib/libc/stdio/refill.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/refill.c,v retrieving revision 1.15 diff -u -p -u -r1.15 refill.c --- lib/libc/stdio/refill.c 15 Mar 2012 18:22:30 -0000 1.15 +++ lib/libc/stdio/refill.c 15 Mar 2012 18:47:06 -0000 @@ -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: lib/libc/stdio/sscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/sscanf.c,v retrieving revision 1.19 diff -u -p -u -r1.19 sscanf.c --- lib/libc/stdio/sscanf.c 15 Mar 2012 18:22:30 -0000 1.19 +++ lib/libc/stdio/sscanf.c 15 Mar 2012 18:47:06 -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: lib/libc/stdio/stdio.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/stdio.c,v retrieving revision 1.19 diff -u -p -u -r1.19 stdio.c --- lib/libc/stdio/stdio.c 15 Mar 2012 18:22:30 -0000 1.19 +++ lib/libc/stdio/stdio.c 15 Mar 2012 18:47:06 -0000 @@ -56,8 +56,8 @@ __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; @@ -65,18 +65,18 @@ __sread(void *cookie, char *buf, int n) _DIAGASSERT(fp != NULL); _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; @@ -84,9 +84,10 @@ __swrite(void *cookie, char const *buf, _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 @@ -98,7 +99,7 @@ __sseek(void *cookie, off_t offset, int _DIAGASSERT(fp != NULL); ret = lseek(__sfileno(fp), offset, whence); - if (ret == -1L) + if (ret == (off_t)-1L) fp->_flags &= ~__SOFF; else { fp->_flags |= __SOFF; @@ -108,8 +109,7 @@ __sseek(void *cookie, off_t offset, int } int -__sclose(cookie) - void *cookie; +__sclose(void *cookie) { _DIAGASSERT(cookie != NULL); Index: lib/libc/stdio/vsscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vsscanf.c,v retrieving revision 1.18 diff -u -p -u -r1.18 vsscanf.c --- lib/libc/stdio/vsscanf.c 15 Mar 2012 18:22:31 -0000 1.18 +++ lib/libc/stdio/vsscanf.c 15 Mar 2012 18:47:06 -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: lib/libc/stdio/vswscanf.c =================================================================== RCS file: /cvsroot/src/lib/libc/stdio/vswscanf.c,v retrieving revision 1.8 diff -u -p -u -r1.8 vswscanf.c --- lib/libc/stdio/vswscanf.c 15 Mar 2012 18:22:31 -0000 1.8 +++ lib/libc/stdio/vswscanf.c 15 Mar 2012 18:47:06 -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;