This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 4/6] [BZ #12836] Make fmemopen(buf,size,"w+b") binary.
- From: Ondrej Bilka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Cc: Ondrej Bilka <neleai at seznam dot cz>
- Date: Sat, 1 Jun 2013 17:11:26 +0200
- Subject: [PATCH 4/6] [BZ #12836] Make fmemopen(buf,size,"w+b") binary.
- References: <1370099488-13916-1-git-send-email-neleai at seznam dot cz>
Resubmission of http://sourceware.org/ml/libc-alpha/2013-05/msg00261.html
* libio/Makefile (tests): Add bug-fmemopen-openmode.
* libio/bug-fmemopen-openmode.c: New file.
* libio/fmemopen.c: Test binary mode at all positions.
---
libio/Makefile | 3 ++-
libio/bug-fmemopen-openmode.c | 50 +++++++++++++++++++++++++++++++++++++++++
libio/fmemopen.c | 5 ++++-
3 files changed, 56 insertions(+), 2 deletions(-)
create mode 100644 libio/bug-fmemopen-openmode.c
diff --git a/libio/Makefile b/libio/Makefile
index e4bbe0f..a68b0e9 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -49,13 +49,14 @@ routines := \
include ../Makeconfig
tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \
- tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-ext2 \
+ tst_wprintf2 tst-widetext tst-ext tst-ext2 \
tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf \
tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof \
tst-freopen bug-rewind bug-rewind2 bug-ungetc bug-fseek \
tst-mmap-eofsync tst-mmap-fflushsync bug-mmap-fflush \
tst-mmap2-eofsync tst-mmap-offend bug-fopena+ bug-wfflush \
bug-ungetc2 bug-ftell bug-ungetc3 bug-ungetc4 tst-fopenloc2 \
+ test-fmemopen bug-fmemopen-openmode \
tst-memstream1 tst-memstream2 \
tst-wmemstream1 tst-wmemstream2 \
bug-memstream1 bug-wmemstream1 \
diff --git a/libio/bug-fmemopen-openmode.c b/libio/bug-fmemopen-openmode.c
new file mode 100644
index 0000000..98e2676
--- /dev/null
+++ b/libio/bug-fmemopen-openmode.c
@@ -0,0 +1,50 @@
+/* Test if binary mode is recognized at all positions.
+ Copyright (C) 2013 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ 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/>. */
+
+
+#include <stdio.h>
+#include <string.h>
+
+int
+do_test (int argc, char **argv)
+{
+ FILE *stream;
+ char buffer[] = "foobar";
+ char s[] = "abc";
+
+ stream = fmemopen (buffer, sizeof (buffer), "w+b");
+ fprintf (stream, s);
+ fclose (stream);
+
+ for (int i = 0; i < strlen (s); i++)
+ if (buffer[i] != s[i])
+ {
+ printf ("Disagree at position %i.\n", i);
+ return 1;
+ }
+
+ if (buffer[strlen (s)] == '\0')
+ {
+ puts ("Not opened in binary mode.\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+#include "../test-skeleton.c"
diff --git a/libio/fmemopen.c b/libio/fmemopen.c
index 7ddbb89..dcc031b 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -249,7 +249,10 @@ einval:
else
c->pos = 0;
- c->binmode = mode[0] != '\0' && mode[1] == 'b';
+ c->binmode = 0;
+ for (int i = 0; mode[i] != '\0'; i++)
+ if (mode[i] == 'b')
+ c->binmode = 1;
iof.read = fmemopen_read;
iof.write = fmemopen_write;
--
1.7.10.4