File: tst-freopen5.c

package info (click to toggle)
glibc 2.41-9
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 300,164 kB
  • sloc: ansic: 1,050,507; asm: 238,242; makefile: 20,378; python: 13,537; sh: 11,812; cpp: 5,197; awk: 1,795; perl: 317; yacc: 292; pascal: 182; sed: 19
file content (144 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Test freopen and freopen64 with large offsets.
   Copyright (C) 2024-2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>

#include <support/check.h>
#include <support/descriptors.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>

#define START_TEST(DESC)			\
  do						\
    {						\
      fds = support_descriptors_list ();	\
      verbose_printf (DESC);			\
    }						\
  while (0)

#define END_TEST				\
  do						\
    {						\
      support_descriptors_check (fds);		\
      support_descriptors_free (fds);		\
    }						\
  while (0)

int
do_test (void)
{
  mtrace ();
  struct support_descriptors *fds;
  FILE *fp;
  int ret;

  char *temp_dir = support_create_temp_directory ("tst-freopen5");
  /* This file is removed at the end of each test rather than left
     around between tests to avoid problems with subsequent tests
     reopening it as a large (2GB + 1 byte) file.  */
  char *file1 = xasprintf ("%s/file1", temp_dir);

  /* fopen with freopen64: large offsets OK.  */
  START_TEST ("testing fopen with freopen64\n");
  fp = fopen ("/dev/null", "r");
  TEST_VERIFY_EXIT (fp != NULL);
  fp = freopen64 (file1, "w", fp);
  TEST_VERIFY_EXIT (fp != NULL);
  setbuf (fp, NULL);
  ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
  TEST_COMPARE (ret, 0);
  ret = fputc ('x', fp);
  TEST_COMPARE (ret, 'x');
  xfclose (fp);
  ret = remove (file1);
  TEST_COMPARE (ret, 0);
  END_TEST;

  /* fopen64 with freopen64: large offsets OK.  */
  START_TEST ("testing fopen64 with freopen64\n");
  fp = fopen64 ("/dev/null", "r");
  TEST_VERIFY_EXIT (fp != NULL);
  fp = freopen64 (file1, "w", fp);
  TEST_VERIFY_EXIT (fp != NULL);
  setbuf (fp, NULL);
  ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
  TEST_COMPARE (ret, 0);
  ret = fputc ('x', fp);
  TEST_COMPARE (ret, 'x');
  xfclose (fp);
  ret = remove (file1);
  TEST_COMPARE (ret, 0);
  END_TEST;

  /* fopen with freopen: large offsets not OK on 32-bit systems.  */
  START_TEST ("testing fopen with freopen\n");
  fp = fopen ("/dev/null", "r");
  TEST_VERIFY_EXIT (fp != NULL);
  fp = freopen (file1, "w", fp);
  TEST_VERIFY_EXIT (fp != NULL);
  setbuf (fp, NULL);
  ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
  TEST_COMPARE (ret, 0);
  errno = 0;
  ret = fputc ('x', fp);
  if (sizeof (off_t) == 4)
    {
      TEST_COMPARE (ret, EOF);
      TEST_COMPARE (errno, EFBIG);
    }
  else
    TEST_COMPARE (ret, 'x');
  fclose (fp);
  ret = remove (file1);
  TEST_COMPARE (ret, 0);
  END_TEST;

  /* fopen64 with freopen: large offsets not OK on 32-bit systems.  */
  START_TEST ("testing fopen64 with freopen\n");
  fp = fopen64 ("/dev/null", "r");
  TEST_VERIFY_EXIT (fp != NULL);
  fp = freopen (file1, "w", fp);
  TEST_VERIFY_EXIT (fp != NULL);
  setbuf (fp, NULL);
  ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
  TEST_COMPARE (ret, 0);
  errno = 0;
  ret = fputc ('x', fp);
  if (sizeof (off_t) == 4)
    {
      TEST_COMPARE (ret, EOF);
      TEST_COMPARE (errno, EFBIG);
    }
  else
    TEST_COMPARE (ret, 'x');
  fclose (fp);
  ret = remove (file1);
  TEST_COMPARE (ret, 0);
  END_TEST;

  free (temp_dir);
  free (file1);
  return 0;
}

#include <support/test-driver.c>