pthread-once tests: Fix crash on mingw.
[gnulib.git] / tests / test-fopen-gnu.c
blob6a84b57dd2bcaa2de84cb03f6daab647b308a882
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. */
19 #include <config.h>
21 /* Specification. */
22 #include <stdio.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
28 #include "macros.h"
30 #define BASE "test-fopen-gnu.t"
32 /* 0x1a is an EOF on Windows. */
33 #define DATA "abc\x1axyz"
35 int
36 main (void)
38 FILE *f;
39 int fd;
40 int flags;
41 char buf[16];
43 /* Remove anything from prior partial run. */
44 unlink (BASE "file");
45 unlink (BASE "binary");
47 /* Create the file. */
48 f = fopen (BASE "file", "w");
49 ASSERT (f);
50 fd = fileno (f);
51 ASSERT (fd >= 0);
52 flags = fcntl (fd, F_GETFD);
53 ASSERT (flags >= 0);
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");
59 ASSERT (f);
60 fd = fileno (f);
61 ASSERT (fd >= 0);
62 flags = fcntl (fd, F_GETFD);
63 ASSERT (flags >= 0);
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");
69 ASSERT (f == NULL);
70 ASSERT (errno == EEXIST);
72 /* Open a binary file and check that the 'e' mode doesn't interfere. */
73 f = fopen (BASE "binary", "wbe");
74 ASSERT (f);
75 ASSERT (fwrite (DATA, 1, sizeof (DATA)-1, f) == sizeof (DATA)-1);
76 ASSERT (fclose (f) == 0);
78 f = fopen (BASE "binary", "rbe");
79 ASSERT (f);
80 ASSERT (fread (buf, 1, sizeof (buf), f) == sizeof (DATA)-1);
81 ASSERT (fclose (f) == 0);
83 /* Cleanup. */
84 ASSERT (unlink (BASE "file") == 0);
85 ASSERT (unlink (BASE "binary") == 0);
87 return test_exit_status;