Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/stdio - widen the internal read and write calls to ...
details: https://anonhg.NetBSD.org/src/rev/d9e812af1207
branches: trunk
changeset: 778455:d9e812af1207
user: christos <christos%NetBSD.org@localhost>
date: Tue Mar 27 15:05:42 2012 +0000
description:
- widen the internal read and write calls to match the syscalls
- add funopen2() which provides access to flush() and the wider calls.
- make use of the new flush call in fmemopen()
diffstat:
lib/libc/stdio/Makefile.inc | 3 +-
lib/libc/stdio/fflush.c | 13 +++--
lib/libc/stdio/findfp.c | 39 +++++++++++++-----
lib/libc/stdio/fmemopen.c | 50 +++++++++++++++-------
lib/libc/stdio/freopen.c | 6 +-
lib/libc/stdio/fseeko.c | 6 +-
lib/libc/stdio/ftell.c | 6 +-
lib/libc/stdio/ftello.c | 6 +-
lib/libc/stdio/funopen.3 | 26 ++++++++++-
lib/libc/stdio/funopen.c | 96 ++++++++++++++++++++++++++++++++++++++++++--
lib/libc/stdio/fvwrite.c | 28 ++++++------
lib/libc/stdio/local.h | 6 +-
lib/libc/stdio/refill.c | 7 +-
lib/libc/stdio/sscanf.c | 8 +-
lib/libc/stdio/stdio.c | 34 +++++++++------
lib/libc/stdio/vfwprintf.c | 5 +-
lib/libc/stdio/vsscanf.c | 8 +-
lib/libc/stdio/vswscanf.c | 8 +-
18 files changed, 253 insertions(+), 102 deletions(-)
diffs (truncated from 918 to 300 lines):
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/Makefile.inc
--- a/lib/libc/stdio/Makefile.inc Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/Makefile.inc Tue Mar 27 15:05:42 2012 +0000
@@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
-# $NetBSD: Makefile.inc,v 1.40 2010/12/22 16:59:10 christos Exp $
+# $NetBSD: Makefile.inc,v 1.41 2012/03/27 15:05:42 christos Exp $
# stdio sources
.PATH: ${.CURDIR}/stdio
@@ -45,6 +45,7 @@
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
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/fflush.c
--- a/lib/libc/stdio/fflush.c Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/fflush.c Tue Mar 27 15:05:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fflush.c,v 1.17 2012/03/15 18:22:30 christos Exp $ */
+/* $NetBSD: fflush.c,v 1.18 2012/03/27 15:05:42 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: fflush.c,v 1.17 2012/03/15 18:22:30 christos Exp $");
+__RCSID("$NetBSD: fflush.c,v 1.18 2012/03/27 15:05:42 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -80,7 +80,8 @@
__sflush(FILE *fp)
{
unsigned char *p;
- int n, t;
+ size_t n;
+ ssize_t t;
_DIAGASSERT(fp != NULL);
@@ -92,8 +93,8 @@
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
@@ -109,5 +110,7 @@
return EOF;
}
}
+ if (fp->_flush)
+ return (*fp->_flush)(fp->_cookie);
return 0;
}
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/findfp.c
--- a/lib/libc/stdio/findfp.c Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/findfp.c Tue Mar 27 15:05:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: findfp.c,v 1.27 2012/03/15 18:22:30 christos Exp $ */
+/* $NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
#else
-__RCSID("$NetBSD: findfp.c,v 1.27 2012/03/15 18:22:30 christos Exp $");
+__RCSID("$NetBSD: findfp.c,v 1.28 2012/03/27 15:05:42 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -56,13 +56,29 @@
#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 = NULL, \
+ ._lb_unused = { '\0' }, \
+ ._blksize = 0, \
+ ._offset = (off_t)0, \
+}
/* the usual - (stdin + stdout + stderr) */
static FILE usual[FOPEN_MAX - 3];
@@ -99,8 +115,8 @@
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));
@@ -129,6 +145,7 @@
fp->_lbfsize = 0; /* not line buffered */
fp->_file = -1; /* no file */
/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
+ fp->_flush = NULL; /* default flush */
_UB(fp)._base = NULL; /* no ungetc buffer */
_UB(fp)._size = 0;
memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/fmemopen.c
--- a/lib/libc/stdio/fmemopen.c Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/fmemopen.c Tue Mar 27 15:05:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fmemopen.c,v 1.6 2012/01/22 18:36:17 christos Exp $ */
+/* $NetBSD: fmemopen.c,v 1.7 2012/03/27 15:05:42 christos Exp $ */
/*-
* Copyright (c)2007, 2010 Takehiko NOZAKI,
@@ -29,7 +29,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: fmemopen.c,v 1.6 2012/01/22 18:36:17 christos Exp $");
+__RCSID("$NetBSD: fmemopen.c,v 1.7 2012/03/27 15:05:42 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include <assert.h>
@@ -46,11 +46,11 @@
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 @@
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 @@
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
@@ -184,12 +199,12 @@
goto release;
}
*cookie->head = '\0';
- fp->_close = &fmemopen_close1;
+ fp->_close = fmemopen_close1;
} else {
cookie->head = (char *)buf;
if (oflags & O_TRUNC)
*cookie->head = '\0';
- fp->_close = &fmemopen_close0;
+ fp->_close = fmemopen_close0;
}
cookie->tail = cookie->head + size;
@@ -203,9 +218,10 @@
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;
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/freopen.c
--- a/lib/libc/stdio/freopen.c Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/freopen.c Tue Mar 27 15:05:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: freopen.c,v 1.18 2012/03/15 18:22:30 christos Exp $ */
+/* $NetBSD: freopen.c,v 1.19 2012/03/27 15:05:42 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: freopen.c,v 1.18 2012/03/15 18:22:30 christos Exp $");
+__RCSID("$NetBSD: freopen.c,v 1.19 2012/03/27 15:05:42 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -93,7 +93,7 @@
} else {
/* flush the stream; ANSI doesn't require this. */
if (fp->_flags & __SWR)
- (void) __sflush(fp);
+ (void)__sflush(fp);
/* if close is NULL, closing is a no-op, hence pointless */
isopen = fp->_close != NULL;
if ((wantfd = __sfileno(fp)) == -1 && isopen) {
diff -r 1b8c34ce15b7 -r d9e812af1207 lib/libc/stdio/fseeko.c
--- a/lib/libc/stdio/fseeko.c Tue Mar 27 12:32:47 2012 +0000
+++ b/lib/libc/stdio/fseeko.c Tue Mar 27 15:05:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fseeko.c,v 1.11 2012/03/15 18:22:30 christos Exp $ */
+/* $NetBSD: fseeko.c,v 1.12 2012/03/27 15:05:42 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -34,7 +34,7 @@
Home |
Main Index |
Thread Index |
Old Index