Fix ChangeLog typo
[glibc.git] / libio / tst-freopen.c
blob5ad589bffa1f273d369790769086a687bc7ebbfa
1 /* Test freopen with mmap stdio.
2 Copyright (C) 2002-2017 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 <http://www.gnu.org/licenses/>. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <support/check.h>
26 #include <support/temp_file.h>
28 static int fd;
29 static char *name;
31 static void
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. */
41 static void
42 do_test_basic (void)
44 const char * const test = "Let's test freopen.\n";
45 char temp[strlen (test) + 1];
47 FILE *f = fdopen (fd, "w");
48 if (f == NULL)
49 FAIL_EXIT1 ("fdopen: %m");
51 fputs (test, f);
52 fclose (f);
54 f = fopen (name, "r");
55 if (f == NULL)
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)",
64 test, temp);
66 f = freopen (name, "r+", f);
67 if (f == NULL)
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)",
79 test, temp);
81 fclose (f);
84 /* Test for BZ#21398, where it tries to freopen stdio after the close
85 of its file descriptor. */
86 static void
87 do_test_bz21398 (void)
89 (void) close (STDIN_FILENO);
91 FILE *f = freopen (name, "r", stdin);
92 if (f == NULL)
93 FAIL_EXIT1 ("freopen: %m");
95 TEST_VERIFY_EXIT (ferror (f) == 0);
97 char buf[128];
98 char *ret = fgets (buf, sizeof (buf), stdin);
99 TEST_VERIFY_EXIT (ret != NULL);
100 TEST_VERIFY_EXIT (ferror (f) == 0);
103 static int
104 do_test (void)
106 do_test_basic ();
107 do_test_bz21398 ();
109 return 0;
112 #include <support/test-driver.c>