1 /* Test of opening a file stream.
2 Copyright (C) 2007-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>, 2007. */
19 /* Include <config.h> and a form of <stdio.h> first. */
26 /* Tell GCC not to warn about the specific edge cases tested here. */
28 # pragma GCC diagnostic ignored "-Wanalyzer-file-leak"
31 /* Test fopen. Assumes BASE is defined. */
37 /* Remove anything from prior partial run. */
40 /* Read requires existing file. */
42 ASSERT (fopen (BASE
"file", "r") == NULL
);
43 ASSERT (errno
== ENOENT
);
45 /* Write can create a file. */
46 f
= fopen (BASE
"file", "w");
48 ASSERT (fclose (f
) == 0);
50 /* Trailing slash is invalid on non-directory. */
52 ASSERT (fopen (BASE
"file/", "r") == NULL
);
53 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== EINVAL
);
56 ASSERT (fopen (BASE
"file/", "r+") == NULL
);
57 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== EINVAL
);
59 /* Cannot create a directory. */
61 ASSERT (fopen ("nonexist.ent/", "w") == NULL
);
62 ASSERT (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== ENOENT
65 /* Directories cannot be opened for writing. */
67 ASSERT (fopen (".", "w") == NULL
);
68 ASSERT (errno
== EISDIR
|| errno
== EINVAL
|| errno
== EACCES
);
71 ASSERT (fopen ("./", "w") == NULL
);
72 ASSERT (errno
== EISDIR
|| errno
== EINVAL
|| errno
== EACCES
);
75 ASSERT (fopen (".", "r+") == NULL
);
76 ASSERT (errno
== EISDIR
|| errno
== EINVAL
|| errno
== EACCES
);
79 ASSERT (fopen ("./", "r+") == NULL
);
80 ASSERT (errno
== EISDIR
|| errno
== EINVAL
|| errno
== EACCES
);
82 /* /dev/null must exist, and be writable. */
83 f
= fopen ("/dev/null", "r");
85 ASSERT (fclose (f
) == 0);
86 f
= fopen ("/dev/null", "w");
88 ASSERT (fclose (f
) == 0);
91 ASSERT (unlink (BASE
"file") == 0);