]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/tst-freopen.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / libio / tst-freopen.c
CommitLineData
81c64d40 1/* Test freopen with mmap stdio.
04277e02 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
81c64d40
UD
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
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
81c64d40
UD
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
f1a67a2c
AZ
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)
81c64d40 43{
81c64d40
UD
44 const char * const test = "Let's test freopen.\n";
45 char temp[strlen (test) + 1];
81c64d40 46
f1a67a2c 47 FILE *f = fdopen (fd, "w");
81c64d40 48 if (f == NULL)
f1a67a2c 49 FAIL_EXIT1 ("fdopen: %m");
81c64d40
UD
50
51 fputs (test, f);
52 fclose (f);
53
54 f = fopen (name, "r");
55 if (f == NULL)
f1a67a2c 56 FAIL_EXIT1 ("fopen: %m");
81c64d40
UD
57
58 if (fread (temp, 1, strlen (test), f) != strlen (test))
f1a67a2c 59 FAIL_EXIT1 ("fread: %m");
81c64d40
UD
60 temp [strlen (test)] = '\0';
61
62 if (strcmp (test, temp))
f1a67a2c
AZ
63 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
64 test, temp);
81c64d40
UD
65
66 f = freopen (name, "r+", f);
67 if (f == NULL)
f1a67a2c 68 FAIL_EXIT1 ("freopen: %m");
81c64d40
UD
69
70 if (fseek (f, 0, SEEK_SET) != 0)
f1a67a2c 71 FAIL_EXIT1 ("fseek: %m");
81c64d40
UD
72
73 if (fread (temp, 1, strlen (test), f) != strlen (test))
f1a67a2c 74 FAIL_EXIT1 ("fread: %m");
81c64d40
UD
75 temp [strlen (test)] = '\0';
76
77 if (strcmp (test, temp))
f1a67a2c
AZ
78 FAIL_EXIT1 ("read different string than was written: (%s, %s)",
79 test, temp);
81c64d40
UD
80
81 fclose (f);
f1a67a2c
AZ
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 ();
81c64d40 108
f1a67a2c 109 return 0;
81c64d40 110}
29955b5d 111
f1a67a2c 112#include <support/test-driver.c>