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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/* Test the fdopen function.
Copyright (C) 2024 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 <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xunistd.h>
#include <support/temp_file.h>
char *tmp_dir;
char *path_to_file;
void
prepare_tmp_dir (void)
{
tmp_dir = support_create_temp_directory ("tst-fdopen2");
path_to_file = xasprintf ("%s/tst-fdopen2.txt", tmp_dir);
}
/* open temp file descriptor with mode. */
int
open_tmp_fd (int mode)
{
int fd = xopen (path_to_file, mode, 0644);
return fd;
}
/* close and remove temp file with close. */
void
close_tmp_fd (int fd)
{
xclose (fd);
xunlink (path_to_file);
}
/* close and remove temp file with fclose. */
void
close_tmp_fp (FILE *fp)
{
fclose (fp);
xunlink (path_to_file);
}
/* test "w" fdopen mode. */
void
do_test_fdopen_w (void)
{
int fd, ret;
FILE *fp;
fd = open_tmp_fd (O_WRONLY | O_CREAT | O_TRUNC);
/* test mode mismatch. */
fp = fdopen (fd, "r");
if (fp != NULL || errno != EINVAL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, r) should fail with EINVAL: %m", fd);
}
fp = fdopen (fd, "w");
if (fp == NULL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
}
const void *buf = "AAAA";
ret = fwrite (buf, 1, 4, fp);
if (ret != 4)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fwrite (): %m");
}
unsigned char buf2[4];
rewind (fp);
clearerr (fp);
/* fread should fail in "w" mode */
ret = fread (buf2, 1, 4, fp);
if (ret != 0 || ferror (fp) == 0)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fread should fail in \"w\" mode");
}
fclose (fp);
}
/* test "r" fdopen mode. */
void
do_test_fdopen_r (void)
{
int fd, ret;
FILE *fp;
fd = open_tmp_fd (O_RDONLY);
/* test mode mismatch. */
fp = fdopen (fd, "w");
if (fp != NULL || errno != EINVAL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, w) should fail with EINVAL: %m", fd);
}
fp = fdopen (fd, "r");
if (fp == NULL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
}
const void *buf = "BBBB";
/* fwrite should fail in "r" mode. */
ret = fwrite (buf, 1, 4, fp);
if (ret != 0 || ferror (fp) == 0)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fwrite should fail in \"r\" mode");
}
unsigned char buf2[4];
ret = fread (buf2, 1, 4, fp);
if (ret != 4)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fread (): %m");
}
fclose (fp);
}
/* test "a" fdopen mode. */
void
do_test_fdopen_a (void)
{
int fd, ret;
FILE *fp;
fd = open_tmp_fd (O_WRONLY | O_CREAT | O_APPEND);
/* test mode mismatch. */
fp = fdopen (fd, "r+");
if (fp != NULL || errno != EINVAL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, \"r+\") should fail with EINVAL: %m", fd);
}
fp = fdopen (fd, "a");
if (fp == NULL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, w): %m", fd);
}
const void *buf = "CCCC";
ret = fwrite (buf, 1, 4, fp);
if (ret != 4)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fwrite (): %m");
}
/* fread should fail in "a" mode. */
unsigned char buf2[4];
clearerr (fp);
ret = fread (buf2, 1, 4, fp);
if (ret != 0 || ferror (fp) == 0)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fread should fail \"a\" mode");
}
fclose (fp);
}
void
do_test_fdopen_mode (int mode, const char *fmode)
{
int fd, ret;
FILE *fp;
fd = open_tmp_fd (mode);
fp = fdopen (fd, fmode);
if (fp == NULL)
{
close_tmp_fd (fd);
FAIL_EXIT1 ("fdopen (%d, %s): %m", fd, fmode);
}
const void *buf = "EEEE";
ret = fwrite (buf, 1, 4, fp);
if (ret != 4)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fwrite () in mode:%s returns %d: %m", fmode, ret);
}
rewind (fp);
unsigned char buf2[4];
ret = fread (buf2, 1, 4, fp);
if (ret != 4)
{
close_tmp_fp (fp);
FAIL_EXIT1 ("fread () in mode:%s returns %d: %m", fmode, ret);
}
fclose (fp);
}
static int
do_test (void)
{
prepare_tmp_dir ();
do_test_fdopen_w ();
do_test_fdopen_r ();
do_test_fdopen_a ();
/* test r+ w+ a+ fdopen modes. */
do_test_fdopen_mode (O_RDWR, "r+");
do_test_fdopen_mode (O_RDWR | O_CREAT | O_TRUNC, "w+");
do_test_fdopen_mode (O_RDWR | O_CREAT | O_APPEND, "a+");
xunlink (path_to_file);
return 0;
}
#include <support/test-driver.c>
|