Make freopen reset the stream orientation.

Signed-off-by: Wren Turkal <wt@penguintechs.org>
This commit is contained in:
Wren Turkal
2020-07-08 05:43:01 -07:00
parent 865b7962a1
commit b623e245c0
3 changed files with 30 additions and 1 deletions
+1
View File
@@ -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
}
+2
View File
@@ -1 +1,3 @@
Hello
0
0
+27 -1
View File
@@ -1,12 +1,38 @@
#include <assert.h>
#include <stdio.h>
#include <wchar.h>
#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<sizeof(tests)/sizeof(int(*)(void)); i++) {
printf("%d\n", (*tests[i])());
}
}