Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src add fmemopen(3) derrived from POSIX:2008.
details: https://anonhg.NetBSD.org/src/rev/53fce467d7ea
branches: trunk
changeset: 757802:53fce467d7ea
user: tnozaki <tnozaki%NetBSD.org@localhost>
date: Fri Sep 24 09:21:53 2010 +0000
description:
add fmemopen(3) derrived from POSIX:2008.
libc minor bump.
diffstat:
include/stdio.h | 7 +-
lib/libc/shlib_version | 4 +-
lib/libc/stdio/Makefile.inc | 3 +-
lib/libc/stdio/fmemopen.c | 216 ++++++
tests/lib/libc/stdio/Makefile | 9 +
tests/lib/libc/stdio/t_fmemopen.c | 1170 +++++++++++++++++++++++++++++++++++++
6 files changed, 1405 insertions(+), 4 deletions(-)
diffs (truncated from 1461 to 300 lines):
diff -r caec1cea1c48 -r 53fce467d7ea include/stdio.h
--- a/include/stdio.h Fri Sep 24 07:48:59 2010 +0000
+++ b/include/stdio.h Fri Sep 24 09:21:53 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: stdio.h,v 1.77 2010/09/06 14:52:26 christos Exp $ */
+/* $NetBSD: stdio.h,v 1.78 2010/09/24 09:21:53 tnozaki Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -508,6 +508,11 @@
#define putchar_unlocked(x) putc_unlocked(x, stdout)
#endif /* _POSIX_C_SOURCE >= 199506 || _XOPEN_SOURCE >= 500 || _REENTRANT... */
+#if (_POSIX_C_SOURCE - 0) >= 200809L || (_XOPEN_SOURCE - 0) >= 700 || \
+ defined(_NETBSD_SOURCE)
+FILE *fmemopen(void * __restrict, size_t, const char * __restrict);
+#endif
+
#if _FORTIFY_SOURCE > 0
#include <ssp/stdio.h>
#endif
diff -r caec1cea1c48 -r 53fce467d7ea lib/libc/shlib_version
--- a/lib/libc/shlib_version Fri Sep 24 07:48:59 2010 +0000
+++ b/lib/libc/shlib_version Fri Sep 24 09:21:53 2010 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: shlib_version,v 1.218 2010/06/07 13:52:29 tnozaki Exp $
+# $NetBSD: shlib_version,v 1.219 2010/09/24 09:21:53 tnozaki Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
#
# things we wish to do on next major version bump:
@@ -31,4 +31,4 @@
# it's insufficient bitwidth to implement all ctype class.
# see isblank's comment in ctype.h.
major=12
-minor=173
+minor=174
diff -r caec1cea1c48 -r 53fce467d7ea lib/libc/stdio/Makefile.inc
--- a/lib/libc/stdio/Makefile.inc Fri Sep 24 07:48:59 2010 +0000
+++ b/lib/libc/stdio/Makefile.inc Fri Sep 24 09:21:53 2010 +0000
@@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
-# $NetBSD: Makefile.inc,v 1.37 2010/09/06 14:52:55 christos Exp $
+# $NetBSD: Makefile.inc,v 1.38 2010/09/24 09:21:53 tnozaki Exp $
# stdio sources
.PATH: ${.CURDIR}/stdio
@@ -20,6 +20,7 @@
vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \
vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
+SRCS+= fmemopen.c
.if !defined(AUDIT)
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
diff -r caec1cea1c48 -r 53fce467d7ea lib/libc/stdio/fmemopen.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdio/fmemopen.c Fri Sep 24 09:21:53 2010 +0000
@@ -0,0 +1,216 @@
+/* $NetBSD: fmemopen.c,v 1.1 2010/09/24 09:21:53 tnozaki Exp $ */
+
+/*-
+ * Copyright (c)2007, 2010 Takehiko NOZAKI,
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+#if defined(LIBC_SCCS) && !defined(lint)
+__RCSID("$NetBSD: fmemopen.c,v 1.1 2010/09/24 09:21:53 tnozaki Exp $");
+#endif /* LIBC_SCCS and not lint */
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "reentrant.h"
+#include "local.h"
+
+struct fmemopen_cookie {
+ char *head, *tail, *cur, *eob;
+};
+
+static int
+fmemopen_read(void *cookie, char *buf, int nbytes)
+{
+ struct fmemopen_cookie *p;
+ char *s;
+
+ _DIAGASSERT(cookie != NULL);
+ _DIAGASSERT(buf != NULL && nbytes > 0);
+
+ p = (struct fmemopen_cookie *)cookie;
+ s = p->cur;
+ do {
+ if (p->cur == p->tail)
+ break;
+ *buf++ = *p->cur++;
+ } while (--nbytes > 0);
+
+ return (int)(p->cur - s);
+}
+
+static int
+fmemopen_write(void *cookie, const char *buf, int nbytes)
+{
+ struct fmemopen_cookie *p;
+ char *s, *t;
+
+ _DIAGASSERT(cookie != NULL);
+ _DIAGASSERT(buf != NULL && nbytes > 0);
+
+ p = (struct fmemopen_cookie *)cookie;
+ if (p->cur >= p->tail)
+ return 0;
+ s = p->cur;
+ t = p->tail - 1;
+ do {
+ if (p->cur == t) {
+ if (*buf == '\0')
+ *p->cur++ = *buf++;
+ break;
+ }
+ *p->cur++ = *buf++;
+ } while (--nbytes > 0);
+ *p->cur = '\0';
+ if (p->cur > p->eob)
+ p->eob = p->cur;
+
+ return (int)(p->cur - s);
+}
+
+static fpos_t
+fmemopen_seek(void *cookie, fpos_t offset, int whence)
+{
+ struct fmemopen_cookie *p;
+
+ _DIAGASSERT(cookie != NULL);
+
+ p = (struct fmemopen_cookie *)cookie;
+ switch (whence) {
+ case SEEK_SET:
+ break;
+ case SEEK_CUR:
+ offset += p->cur - p->head;
+ break;
+ case SEEK_END:
+ offset += p->eob - p->head;
+ break;
+ default:
+ errno = EINVAL;
+ goto error;
+ }
+ if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
+ p->cur = p->head + offset;
+ return (fpos_t)(p->cur - p->head);
+ }
+error:
+ return (fpos_t)-1;
+}
+
+static int
+fmemopen_close0(void *cookie)
+{
+ _DIAGASSERT(cookie != NULL);
+
+ free(cookie);
+
+ return 0;
+}
+
+static int
+fmemopen_close1(void *cookie)
+{
+ struct fmemopen_cookie *p;
+
+ _DIAGASSERT(cookie != NULL);
+
+ p = (struct fmemopen_cookie *)cookie;
+ free(p->head);
+ free(p);
+
+ return 0;
+}
+
+
+FILE *
+fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
+{
+ int oflags;
+ FILE *fp;
+ struct fmemopen_cookie *cookie;
+
+ if (size < (size_t)1) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ fp = __sfp();
+ if (fp == NULL)
+ return NULL;
+ fp->_file = -1;
+
+ fp->_flags = __sflags(mode, &oflags);
+ if (fp->_flags == 0)
+ return NULL;
+
+ if ((oflags & O_RDWR) == 0 && buf == NULL) {
+ errno = EINVAL;
+ goto release;
+ }
+
+ cookie = malloc(sizeof(*cookie));
+ if (cookie == NULL)
+ goto release;
+
+ if (buf == NULL) {
+ cookie->head = malloc(size);
+ if (cookie->head == NULL) {
+ free(cookie);
+ goto release;
+ }
+ *cookie->head = '\0';
+ fp->_close = &fmemopen_close1;
+ } else {
+ cookie->head = (char *)buf;
+ if (oflags & O_TRUNC)
+ *cookie->head = '\0';
+ fp->_close = &fmemopen_close0;
+ }
+
+ cookie->tail = cookie->head + size;
+ cookie->eob = cookie->head;
+ do {
+ if (*cookie->eob == '\0')
+ break;
+ ++cookie->eob;
+ } while (--size > 0);
+
+ cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
+
+ fp->_write = (fp->_flags & __SRD) ? NULL : &fmemopen_write;
+ fp->_read = (fp->_flags & __SWR) ? NULL : &fmemopen_read;
+ fp->_seek = &fmemopen_seek;
+ fp->_cookie = (void *)cookie;
+
+ return fp;
+
+release:
+ fp->_flags = 0;
+ return NULL;
+}
diff -r caec1cea1c48 -r 53fce467d7ea tests/lib/libc/stdio/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/stdio/Makefile Fri Sep 24 09:21:53 2010 +0000
@@ -0,0 +1,9 @@
+# $NetBSD: Makefile,v 1.1 2010/09/24 09:21:53 tnozaki Exp $
+
+.include <bsd.own.mk>
+
+TESTSDIR= ${TESTSBASE}/lib/libc/stdio
+
+TESTS_C+= t_fmemopen
+
+.include <bsd.test.mk>
diff -r caec1cea1c48 -r 53fce467d7ea tests/lib/libc/stdio/t_fmemopen.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/stdio/t_fmemopen.c Fri Sep 24 09:21:53 2010 +0000
@@ -0,0 +1,1170 @@
+/* $NetBSD: t_fmemopen.c,v 1.1 2010/09/24 09:21:53 tnozaki Exp $ */
+
+/*-
+ * Copyright (c)2010 Takehiko NOZAKI,
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
Home |
Main Index |
Thread Index |
Old Index