1 /* Test freopen with mmap stdio.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <support/check.h>
25 #include <support/temp_file.h>
31 do_prepare (int argc
, char *argv
[])
33 fd
= create_temp_file ("tst-freopen.", &name
);
34 TEST_VERIFY_EXIT (fd
!= -1);
37 #define PREPARE do_prepare
39 /* Basic tests for freopen. */
43 const char * const test
= "Let's test freopen.\n";
44 char temp
[strlen (test
) + 1];
46 FILE *f
= fdopen (fd
, "w");
48 FAIL_EXIT1 ("fdopen: %m");
53 f
= fopen (name
, "r");
55 FAIL_EXIT1 ("fopen: %m");
57 if (fread (temp
, 1, strlen (test
), f
) != strlen (test
))
58 FAIL_EXIT1 ("fread: %m");
59 temp
[strlen (test
)] = '\0';
61 if (strcmp (test
, temp
))
62 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
65 f
= freopen (name
, "r+", f
);
67 FAIL_EXIT1 ("freopen: %m");
69 if (fseek (f
, 0, SEEK_SET
) != 0)
70 FAIL_EXIT1 ("fseek: %m");
72 if (fread (temp
, 1, strlen (test
), f
) != strlen (test
))
73 FAIL_EXIT1 ("fread: %m");
74 temp
[strlen (test
)] = '\0';
76 if (strcmp (test
, temp
))
77 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
83 #if defined __GNUC__ && __GNUC__ >= 11
84 /* Force an error to detect incorrectly making freopen a deallocator
85 for its last argument via attribute malloc. The function closes
86 the stream without deallocating it so either the argument or
87 the pointer returned from the function (but not both) can be passed
89 #pragma GCC diagnostic push
90 #pragma GCC diagnostic error "-Wmismatched-dealloc"
93 /* Verify that freopen returns stream. */
95 do_test_return_stream (void)
97 FILE *f1
= fopen (name
, "r");
99 FAIL_EXIT1 ("fopen: %m");
101 FILE *f2
= freopen (name
, "r+", f1
);
103 FAIL_EXIT1 ("freopen: %m");
105 /* Verify that freopen isn't declared with the no-argument attribute
106 malloc (which could let GCC fold the inequality to false). */
108 FAIL_EXIT1 ("freopen returned a different stream");
110 /* This shouldn't trigger -Wmismatched-dealloc. */
114 #if defined __GNUC__ && __GNUC__ >= 11
115 /* Pop -Wmismatched-dealloc set to error above. */
116 # pragma GCC diagnostic pop
119 /* Test for BZ#21398, where it tries to freopen stdio after the close
120 of its file descriptor. */
122 do_test_bz21398 (void)
124 (void) close (STDIN_FILENO
);
126 FILE *f
= freopen (name
, "r", stdin
);
128 FAIL_EXIT1 ("freopen: %m");
130 TEST_VERIFY_EXIT (ferror (f
) == 0);
133 char *ret
= fgets (buf
, sizeof (buf
), stdin
);
134 TEST_VERIFY_EXIT (ret
!= NULL
);
135 TEST_VERIFY_EXIT (ferror (f
) == 0);
143 do_test_return_stream ();
148 #include <support/test-driver.c>