1 /* Test freopen with mmap stdio.
2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
25 #include <support/check.h>
26 #include <support/temp_file.h>
32 do_prepare (int argc
, char *argv
[])
34 fd
= create_temp_file ("tst-freopen.", &name
);
35 TEST_VERIFY_EXIT (fd
!= -1);
38 #define PREPARE do_prepare
40 /* Basic tests for freopen. */
44 const char * const test
= "Let's test freopen.\n";
45 char temp
[strlen (test
) + 1];
47 FILE *f
= fdopen (fd
, "w");
49 FAIL_EXIT1 ("fdopen: %m");
54 f
= fopen (name
, "r");
56 FAIL_EXIT1 ("fopen: %m");
58 if (fread (temp
, 1, strlen (test
), f
) != strlen (test
))
59 FAIL_EXIT1 ("fread: %m");
60 temp
[strlen (test
)] = '\0';
62 if (strcmp (test
, temp
))
63 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
66 f
= freopen (name
, "r+", f
);
68 FAIL_EXIT1 ("freopen: %m");
70 if (fseek (f
, 0, SEEK_SET
) != 0)
71 FAIL_EXIT1 ("fseek: %m");
73 if (fread (temp
, 1, strlen (test
), f
) != strlen (test
))
74 FAIL_EXIT1 ("fread: %m");
75 temp
[strlen (test
)] = '\0';
77 if (strcmp (test
, temp
))
78 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
84 /* Test for BZ#21398, where it tries to freopen stdio after the close
85 of its file descriptor. */
87 do_test_bz21398 (void)
89 (void) close (STDIN_FILENO
);
91 FILE *f
= freopen (name
, "r", stdin
);
93 FAIL_EXIT1 ("freopen: %m");
95 TEST_VERIFY_EXIT (ferror (f
) == 0);
98 char *ret
= fgets (buf
, sizeof (buf
), stdin
);
99 TEST_VERIFY_EXIT (ret
!= NULL
);
100 TEST_VERIFY_EXIT (ferror (f
) == 0);
112 #include <support/test-driver.c>