This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH 2/6 v2] Add fmemopen compatibility version.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Wed, 5 Jun 2013 12:21:41 +0200
- Subject: Re: [PATCH 2/6 v2] Add fmemopen compatibility version.
- References: <1370099488-13916-1-git-send-email-neleai at seznam dot cz> <1370099488-13916-3-git-send-email-neleai at seznam dot cz> <Pine dot LNX dot 4 dot 64 dot 1306031546390 dot 18596 at digraph dot polyomino dot org dot uk>
On Mon, Jun 03, 2013 at 03:54:52PM +0000, Joseph S. Myers wrote:
> On Sat, 1 Jun 2013, Ondrej Bilka wrote:
>
> > + GLIBC_2.17 {
> > + fmemopen;
> > + }
>
> That's certainly wrong. The next glibc release is 2.18, so the new
> version of fmemopen would use GLIBC_2.18 (or 2.19 if it doesn't get into
> 2.18).
>
> > +#if SHLIB_COMPAT (libc, GLIBC_2_17, GLIBC_2_18)
>
> I believe this should be
>
> #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_18)
>
> since the symbol was introduced in version 2.2 and this version was
> obsoleted in 2.18.
>
> You may also need a versioned_symbol call to give the new fmemopen the
> right version; I'm not sure if that's required or if listing the new
> version in the Versions file will be sufficient.
>
How is this done?
> > +compat_symbol (libc, oldfmemopen, fmemopen, GLIBC_2_17);
>
> And that should use GLIBC_2_2.
>
> --
> Joseph S. Myers
> joseph@codesourcery.com
Here is v2.
* libio/Makefile (routines): Add oldfmemopen.
* libio/Versions (libc): Add compatibility symbol.
* libio/oldfmemopen.c: New file.
---
libio/Makefile | 2 +-
libio/Versions | 3 +
libio/oldfmemopen.c | 268 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 272 insertions(+), 1 deletion(-)
create mode 100644 libio/oldfmemopen.c
diff --git a/libio/Makefile b/libio/Makefile
index e15cd40..1e3e67f 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -44,7 +44,7 @@ routines := \
__fbufsize __freading __fwriting __freadable __fwritable __flbf \
__fpurge __fpending __fsetlocking \
\
- libc_fatal fmemopen
+ libc_fatal oldfmemopen fmemopen
include ../Makeconfig
diff --git a/libio/Versions b/libio/Versions
index 8df89d2..e1c3f14 100644
--- a/libio/Versions
+++ b/libio/Versions
@@ -148,6 +148,9 @@ libc {
GLIBC_2.4 {
open_wmemstream;
}
+ GLIBC_2.18 {
+ fmemopen;
+ }
GLIBC_PRIVATE {
# Used by NPTL and librt
__libc_fatal;
diff --git a/libio/oldfmemopen.c b/libio/oldfmemopen.c
new file mode 100644
index 0000000..f67fa3e
--- /dev/null
+++ b/libio/oldfmemopen.c
@@ -0,0 +1,268 @@
+/* Fmemopen implementation.
+ Copyright (C) 2000-2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+/*
+ * fmemopen() - "my" version of a string stream
+ * Hanno Mueller, kontakt@hanno.de
+ *
+ *
+ * I needed fmemopen() for an application that I currently work on,
+ * but couldn't find it in libio. The following snippet of code is an
+ * attempt to implement what glibc's documentation describes.
+ *
+ *
+ *
+ * I already see some potential problems:
+ *
+ * - I never used the "original" fmemopen(). I am sure that "my"
+ * fmemopen() behaves differently than the original version.
+ *
+ * - The documentation doesn't say wether a string stream allows
+ * seeks. I checked the old fmemopen implementation in glibc's stdio
+ * directory, wasn't quite able to see what is going on in that
+ * source, but as far as I understand there was no seek there. For
+ * my application, I needed fseek() and ftell(), so it's here.
+ *
+ * - "append" mode and fseek(p, SEEK_END) have two different ideas
+ * about the "end" of the stream.
+ *
+ * As described in the documentation, when opening the file in
+ * "append" mode, the position pointer will be set to the first null
+ * character of the string buffer (yet the buffer may already
+ * contain more data). For fseek(), the last byte of the buffer is
+ * used as the end of the stream.
+ *
+ * - It is unclear to me what the documentation tries to say when it
+ * explains what happens when you use fmemopen with a NULL
+ * buffer.
+ *
+ * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
+ * is really only useful if you are going to write things to the
+ * buffer and then read them back in again."
+ *
+ * What does that mean if the original fmemopen() did not allow
+ * seeking? How do you read what you just wrote without seeking back
+ * to the beginning of the stream?
+ *
+ * - I think there should be a second version of fmemopen() that does
+ * not add null characters for each write. (At least in my
+ * application, I am not actually using strings but binary data and
+ * so I don't need the stream to add null characters on its own.)
+ */
+
+#include <shlib-compat.h>
+#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_18)
+
+
+# include <errno.h>
+# include <libio.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <stdint.h>
+# include <string.h>
+# include <sys/types.h>
+# include "libioP.h"
+
+
+typedef struct oldfmemopen_cookie_struct oldfmemopen_cookie_t;
+struct oldfmemopen_cookie_struct
+{
+ char *buffer;
+ int mybuffer;
+ int binmode;
+ size_t size;
+ _IO_off64_t pos;
+ size_t maxpos;
+};
+
+
+static ssize_t
+oldfmemopen_read (void *cookie, char *b, size_t s)
+{
+ oldfmemopen_cookie_t *c;
+
+ c = (oldfmemopen_cookie_t *) cookie;
+
+ if (c->pos + s > c->size)
+ {
+ if ((size_t) c->pos == c->size)
+ return 0;
+ s = c->size - c->pos;
+ }
+
+ memcpy (b, &(c->buffer[c->pos]), s);
+
+ c->pos += s;
+ if ((size_t) c->pos > c->maxpos)
+ c->maxpos = c->pos;
+
+ return s;
+}
+
+
+static ssize_t
+oldfmemopen_write (void *cookie, const char *b, size_t s)
+{
+ oldfmemopen_cookie_t *c;
+ int addnullc;
+
+ c = (oldfmemopen_cookie_t *) cookie;
+
+ addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0');
+
+ if (c->pos + s + addnullc > c->size)
+ {
+ if ((size_t) (c->pos + addnullc) == c->size)
+ {
+ __set_errno (ENOSPC);
+ return 0;
+ }
+ s = c->size - c->pos - addnullc;
+ }
+
+ memcpy (&(c->buffer[c->pos]), b, s);
+
+ c->pos += s;
+ if ((size_t) c->pos > c->maxpos)
+ {
+ c->maxpos = c->pos;
+ if (addnullc)
+ c->buffer[c->maxpos] = '\0';
+ }
+
+ return s;
+}
+
+
+static int
+oldfmemopen_seek (void *cookie, _IO_off64_t *p, int w)
+{
+ _IO_off64_t np;
+ oldfmemopen_cookie_t *c;
+
+ c = (oldfmemopen_cookie_t *) cookie;
+
+ switch (w)
+ {
+ case SEEK_SET:
+ np = *p;
+ break;
+
+ case SEEK_CUR:
+ np = c->pos + *p;
+ break;
+
+ case SEEK_END:
+ np = (c->binmode ? c->size : c->maxpos) - *p;
+ break;
+
+ default:
+ return -1;
+ }
+
+ if (np < 0 || (size_t) np > c->size)
+ return -1;
+
+ *p = c->pos = np;
+
+ return 0;
+}
+
+
+static int
+oldfmemopen_close (void *cookie)
+{
+ oldfmemopen_cookie_t *c;
+
+ c = (oldfmemopen_cookie_t *) cookie;
+
+ if (c->mybuffer)
+ free (c->buffer);
+ free (c);
+
+ return 0;
+}
+
+
+FILE *
+attribute_compat_text_section
+oldfmemopen (void *buf, size_t len, const char *mode)
+{
+ cookie_io_functions_t iof;
+ oldfmemopen_cookie_t *c;
+
+ if (__builtin_expect (len == 0, 0))
+ {
+ einval:
+ __set_errno (EINVAL);
+ return NULL;
+ }
+
+ c = (oldfmemopen_cookie_t *) malloc (sizeof (oldfmemopen_cookie_t));
+ if (c == NULL)
+ return NULL;
+
+ c->mybuffer = (buf == NULL);
+
+ if (c->mybuffer)
+ {
+ c->buffer = (char *) malloc (len);
+ if (c->buffer == NULL)
+ {
+ free (c);
+ return NULL;
+ }
+ c->buffer[0] = '\0';
+ c->maxpos = 0;
+ }
+ else
+ {
+ if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0))
+ {
+ free (c);
+ goto einval;
+ }
+
+ c->buffer = buf;
+
+ if (mode[0] == 'w')
+ c->buffer[0] = '\0';
+
+ c->maxpos = strnlen (c->buffer, len);
+ }
+
+ c->size = len;
+
+ if (mode[0] == 'a')
+ c->pos = c->maxpos;
+ else
+ c->pos = 0;
+
+ c->binmode = mode[0] != '\0' && mode[1] == 'b';
+
+ iof.read = oldfmemopen_read;
+ iof.write = oldfmemopen_write;
+ iof.seek = oldfmemopen_seek;
+ iof.close = oldfmemopen_close;
+
+ return _IO_fopencookie (c, mode, iof);
+}
+
+compat_symbol (libc, oldfmemopen, fmemopen, GLIBC_2_2);
+#endif
--
1.7.10.4