On Fri, 14 Aug 2020 07:04:13 -0600, Todd C. Miller wrote:

> Yes, there is special handling for O_APPEND that sets the size.

However, there are other bugs with respect to append.  I've added
an append flag to the state but we could just as easily store the
open flag instead.

I checked other implementations and they do set the position to the
end for each write, even if the position has been changed earlier
due to fseek().

 - todd

Index: lib/libc/stdio/fmemopen.c
===================================================================
RCS file: /cvs/src/lib/libc/stdio/fmemopen.c,v
retrieving revision 1.4
diff -u -p -u -r1.4 fmemopen.c
--- lib/libc/stdio/fmemopen.c   14 Aug 2020 12:00:33 -0000      1.4
+++ lib/libc/stdio/fmemopen.c   14 Aug 2020 15:28:09 -0000
@@ -30,6 +30,7 @@ struct state {
        size_t           size;          /* allocated size */
        size_t           len;           /* length of the data */
        int              update;        /* open for update */
+       int              append;        /* open for append */
 };
 
 static int
@@ -51,6 +52,9 @@ fmemopen_write(void *v, const char *b, i
        struct state    *st = v;
        int             i;
 
+       if (st->append)
+               st->pos = st->len;
+
        for (i = 0; i < l && i + st->pos < st->size; i++)
                st->string[st->pos + i] = b[i];
        st->pos += i;
@@ -147,6 +151,7 @@ fmemopen(void *buf, size_t size, const c
        st->len = (oflags & O_TRUNC) ? 0 : size;
        st->size = size;
        st->update = oflags & O_RDWR;
+       st->append = oflags & O_APPEND;
 
        if (buf == NULL) {
                if ((st->string = malloc(size)) == NULL) {
@@ -173,7 +178,7 @@ fmemopen(void *buf, size_t size, const c
 
        fp->_flags = (short)flags;
        fp->_file = -1;
-       fp->_cookie = (void *)st;
+       fp->_cookie = st;
        fp->_read = (flags & __SWR) ? NULL : fmemopen_read;
        fp->_write = (flags & __SRD) ? NULL : fmemopen_write;
        fp->_seek = fmemopen_seek;

Reply via email to