1 /* Test of opening a file stream.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
30 #define BASE "test-fopen-gnu.t"
32 /* 0x1a is an EOF on Windows. */
33 #define DATA "abc\x1axyz"
43 /* Remove anything from prior partial run. */
45 unlink (BASE
"binary");
47 /* Create the file. */
48 f
= fopen (BASE
"file", "w");
52 flags
= fcntl (fd
, F_GETFD
);
54 ASSERT ((flags
& FD_CLOEXEC
) == 0);
55 ASSERT (fclose (f
) == 0);
57 /* Create the file and check the 'e' mode. */
58 f
= fopen (BASE
"file", "we");
62 flags
= fcntl (fd
, F_GETFD
);
64 ASSERT ((flags
& FD_CLOEXEC
) != 0);
65 ASSERT (fclose (f
) == 0);
67 /* Open the file and check the 'x' mode. */
68 f
= fopen (BASE
"file", "ax");
70 ASSERT (errno
== EEXIST
);
72 /* Open a binary file and check that the 'e' mode doesn't interfere. */
73 f
= fopen (BASE
"binary", "wbe");
75 ASSERT (fwrite (DATA
, 1, sizeof (DATA
)-1, f
) == sizeof (DATA
)-1);
76 ASSERT (fclose (f
) == 0);
78 f
= fopen (BASE
"binary", "rbe");
80 ASSERT (fread (buf
, 1, sizeof (buf
), f
) == sizeof (DATA
)-1);
81 ASSERT (fclose (f
) == 0);
84 ASSERT (unlink (BASE
"file") == 0);
85 ASSERT (unlink (BASE
"binary") == 0);
87 return test_exit_status
;