Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test freopen with mmap stdio.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
5
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.
10
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.
15
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/>. */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include <support/check.h>
26#include <support/temp_file.h>
27
28static int fd;
29static char *name;
30
31static void
32do_prepare (int argc, char *argv[])
33{
34 fd = create_temp_file ("tst-freopen.", &name);
35 TEST_VERIFY_EXIT (fd != -1);
36}
37
38#define PREPARE do_prepare
39
40/* Basic tests for freopen. */
41static void
42do_test_basic (void)
43{
44 const char * const test = "Let's test freopen.\n";
45 char temp[strlen (test) + 1];
46
47 FILE *f = fdopen (fd, "w");
48 if (f == NULL)
49 FAIL_EXIT1 ("fdopen: %m");
50
51 fputs (test, f);
52 fclose (f);
53
54 f = fopen (name, "r");
55 if (f == NULL)
56 FAIL_EXIT1 ("fopen: %m");
57
58 if (fread (temp, 1, strlen (test), f) != strlen (test))
59 FAIL_EXIT1 ("fread: %m");
60 temp [strlen (test)] = '\0';
61
62 if (strcmp (test, temp))
63 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
64 test, temp);
65
66 f = freopen (name, "r+", f);
67 if (f == NULL)
68 FAIL_EXIT1 ("freopen: %m");
69
70 if (fseek (f, 0, SEEK_SET) != 0)
71 FAIL_EXIT1 ("fseek: %m");
72
73 if (fread (temp, 1, strlen (test), f) != strlen (test))
74 FAIL_EXIT1 ("fread: %m");
75 temp [strlen (test)] = '\0';
76
77 if (strcmp (test, temp))
78 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
79 test, temp);
80
81 fclose (f);
82}
83
84/* Test for BZ#21398, where it tries to freopen stdio after the close
85 of its file descriptor. */
86static void
87do_test_bz21398 (void)
88{
89 (void) close (STDIN_FILENO);
90
91 FILE *f = freopen (name, "r", stdin);
92 if (f == NULL)
93 FAIL_EXIT1 ("freopen: %m");
94
95 TEST_VERIFY_EXIT (ferror (f) == 0);
96
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);
101}
102
103static int
104do_test (void)
105{
106 do_test_basic ();
107 do_test_bz21398 ();
108
109 return 0;
110}
111
112#include <support/test-driver.c>
113

Warning: That file was not part of the compilation database. It may have many parsing errors.