2 Copyright (C) 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 /* Prototype for our test function. */
30 extern void do_prepare (int argc, char *argv[]);
31 extern int do_test (int argc, char *argv[]);
33 /* We have a preparation function. */
34 #define PREPARE do_prepare
36 #include "../test-skeleton.c"
39 /* Name of the temporary files. */
43 do_prepare (int argc, char *argv[])
47 name_len = strlen (test_dir);
48 name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
49 mempcpy (mempcpy (name, test_dir, name_len),
50 "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
56 do_test (int argc, char *argv[])
65 /* Create the temporary file. */
69 printf ("cannot open temporary file: %m\n");
72 if (fstat64 (fd, &st) != 0)
74 printf ("cannot stat test file: %m\n");
77 if (! S_ISREG (st.st_mode) || st.st_size != 0)
79 puts ("file not created correctly");
83 /* Get the flags with fcntl(). */
84 val = fcntl (fd, F_GETFL);
87 printf ("fcntl(fd, F_GETFL) failed: %m\n");
90 else if ((val & O_ACCMODE) != O_RDWR)
92 puts ("temporary file not opened for read and write");
96 /* Set the flags to something else. */
97 if (fcntl (fd, F_SETFL, O_RDONLY) == -1)
99 printf ("fcntl(fd, F_SETFL, O_RDONLY) failed: %m\n");
103 val = fcntl (fd, F_GETFL);
106 printf ("fcntl(fd, F_GETFL) after F_SETFL failed: %m\n");
109 else if ((val & O_ACCMODE) != O_RDWR)
111 puts ("temporary file access mode changed");
115 /* Set the flags to something else. */
116 if (fcntl (fd, F_SETFL, O_APPEND) == -1)
118 printf ("fcntl(fd, F_SETFL, O_APPEND) failed: %m\n");
122 val = fcntl (fd, F_GETFL);
125 printf ("fcntl(fd, F_GETFL) after second F_SETFL failed: %m\n");
128 else if ((val & O_APPEND) == 0)
130 puts ("O_APPEND not set");
134 val = fcntl (fd, F_GETFD);
137 printf ("fcntl(fd, F_GETFD) failed: %m\n");
140 else if (fcntl (fd, F_SETFD, val | FD_CLOEXEC) == -1)
142 printf ("fcntl(fd, F_SETFD, FD_CLOEXEC) failed: %m\n");
147 val = fcntl (fd, F_GETFD);
150 printf ("fcntl(fd, F_GETFD) after F_SETFD failed: %m\n");
153 else if ((val & FD_CLOEXEC) == 0)
155 puts ("FD_CLOEXEC not set");
160 /* Get a number of a free descriptor. If /dev/null is not available
161 don't continue testing. */
162 fd2 = open (_PATH_DEVNULL, O_RDWR);
167 fd3 = fcntl (fd, F_DUPFD, fd2 + 1);
170 printf ("fcntl(fd, F_DUPFD, %d) failed: %m\n", fd2 + 1);
175 printf ("F_DUPFD returned %d which is not larger than %d\n", fd3, fd2);
181 val = fcntl (fd3, F_GETFD);
184 printf ("fcntl(fd3, F_GETFD) after F_DUPFD failed: %m\n");
187 else if ((val & FD_CLOEXEC) != 0)
189 puts ("FD_CLOEXEC still set");