From b623e245c05ce047ee512b53da5d6e493c09ac3d Mon Sep 17 00:00:00 2001 From: Wren Turkal Date: Wed, 8 Jul 2020 05:43:01 -0700 Subject: [PATCH] Make freopen reset the stream orientation. Signed-off-by: Wren Turkal --- src/header/stdio/mod.rs | 1 + tests/expected/stdio/freopen.stdout | 2 ++ tests/stdio/freopen.c | 28 +++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index b6c9c916a7..73f1bba3e6 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -519,6 +519,7 @@ pub unsafe extern "C" fn freopen( stream.flags = (stream.flags & constants::F_PERM) | new.flags; fclose(new); } + stream.orientation = 0; funlockfile(stream); stream } diff --git a/tests/expected/stdio/freopen.stdout b/tests/expected/stdio/freopen.stdout index e965047ad7..1dd338fd1a 100644 --- a/tests/expected/stdio/freopen.stdout +++ b/tests/expected/stdio/freopen.stdout @@ -1 +1,3 @@ Hello +0 +0 diff --git a/tests/stdio/freopen.c b/tests/stdio/freopen.c index 6c658435f6..3fb6069cbf 100644 --- a/tests/stdio/freopen.c +++ b/tests/stdio/freopen.c @@ -1,12 +1,38 @@ +#include #include +#include #include "test_helpers.h" -int main(void) { +int test_reopen_opens_file(void) { FILE *f = freopen("stdio/stdio.in", "r", stdin); ERROR_IF(freopen, f, == NULL); char in[6]; fgets(in, 6, stdin); printf("%s\n", in); // should print Hello + fclose(f); + return 0; +} + +int test_reopen_resets_orientation(void) { + FILE *f = freopen("stdio/stdio.in", "r", stdin); + assert(fwide(f, 0) == 0); + assert(fwide(f, -1) == -1); + + f = freopen("stdio/stdio.in", "r", stdin); + assert(fwide(f, 0) == 0); + + fclose(f); + return 0; +} + +int main(void) { + int(*tests[])(void) = { + &test_reopen_opens_file, + &test_reopen_resets_orientation, + }; + for(int i=0; i