1 /* Test of opening a file descriptor.
2 Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 /* Include <config.h> and a form of <fcntl.h> first. */
26 #define ASSERT(expr) \
31 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38 /* Test fopen. Assumes BASE is defined. */
44 /* Remove anything from prior partial run. */
47 /* Cannot create directory. */
49 ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
50 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT);
52 /* Create a regular file. */
53 fd = open (BASE "file", O_CREAT | O_RDONLY, 0600);
55 ASSERT (close (fd) == 0);
57 /* Trailing slash handling. */
59 ASSERT (open (BASE "file/", O_RDONLY) == -1);
60 ASSERT (errno == ENOTDIR || errno == EISDIR);
62 /* Directories cannot be opened for writing. */
64 ASSERT (open (".", O_WRONLY) == -1);
65 ASSERT (errno == EISDIR);
67 /* /dev/null must exist, and be writable. */
68 fd = open ("/dev/null", O_RDONLY);
72 ASSERT (read (fd, &c, 1) == 0);
74 ASSERT (close (fd) == 0);
75 fd = open ("/dev/null", O_WRONLY);
77 ASSERT (write (fd, "c", 1) == 1);
78 ASSERT (close (fd) == 0);
81 ASSERT (unlink (BASE "file") == 0);