summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2020-08-17 16:17:40 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2020-08-17 16:17:40 +0000
commita2e56088f9b97c1fdb63b1b5ae21d4fe7b9dc9a7 (patch)
treee1a841762f80e98fd94eff3c8451520478933efc /lib
parenta147cf7fbd947555dfde0f8cae5fce3108a64cef (diff)
Fix append mode so it always writes to the end and expand regress.
OK deraadt@ martijn@
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdio/fmemopen.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c
index 35b0ff7062a..bfc9ca4c173 100644
--- a/lib/libc/stdio/fmemopen.c
+++ b/lib/libc/stdio/fmemopen.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fmemopen.c,v 1.4 2020/08/14 12:00:33 millert Exp $ */
+/* $OpenBSD: fmemopen.c,v 1.5 2020/08/17 16:17:39 millert Exp $ */
/*
* Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
@@ -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, int l)
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 char *mode)
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 char *mode)
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;