2 Copyright (C) 2000-2014 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, see
18 <http://www.gnu.org/licenses/>. */
28 /* Prototype for our test function. */
29 extern void do_prepare (int argc
, char *argv
[]);
30 extern int do_test (int argc
, char *argv
[]);
32 /* We have a preparation function. */
33 #define PREPARE do_prepare
35 #include "../test-skeleton.c"
38 /* Name of the temporary files. */
42 do_prepare (int argc
, char *argv
[])
46 name_len
= strlen (test_dir
);
47 name
= malloc (name_len
+ sizeof ("/fcntlXXXXXX"));
48 mempcpy (mempcpy (name
, test_dir
, name_len
),
49 "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
55 do_test (int argc
, char *argv
[])
64 /* Create the temporary file. */
68 printf ("cannot open temporary file: %m\n");
71 if (fstat64 (fd
, &st
) != 0)
73 printf ("cannot stat test file: %m\n");
76 if (! S_ISREG (st
.st_mode
) || st
.st_size
!= 0)
78 puts ("file not created correctly");
82 /* Get the flags with fcntl(). */
83 val
= fcntl (fd
, F_GETFL
);
86 printf ("fcntl(fd, F_GETFL) failed: %m\n");
89 else if ((val
& O_ACCMODE
) != O_RDWR
)
91 puts ("temporary file not opened for read and write");
95 /* Set the flags to something else. */
96 if (fcntl (fd
, F_SETFL
, O_RDONLY
) == -1)
98 printf ("fcntl(fd, F_SETFL, O_RDONLY) failed: %m\n");
102 val
= fcntl (fd
, F_GETFL
);
105 printf ("fcntl(fd, F_GETFL) after F_SETFL failed: %m\n");
108 else if ((val
& O_ACCMODE
) != O_RDWR
)
110 puts ("temporary file access mode changed");
114 /* Set the flags to something else. */
115 if (fcntl (fd
, F_SETFL
, O_APPEND
) == -1)
117 printf ("fcntl(fd, F_SETFL, O_APPEND) failed: %m\n");
121 val
= fcntl (fd
, F_GETFL
);
124 printf ("fcntl(fd, F_GETFL) after second F_SETFL failed: %m\n");
127 else if ((val
& O_APPEND
) == 0)
129 puts ("O_APPEND not set");
133 val
= fcntl (fd
, F_GETFD
);
136 printf ("fcntl(fd, F_GETFD) failed: %m\n");
139 else if (fcntl (fd
, F_SETFD
, val
| FD_CLOEXEC
) == -1)
141 printf ("fcntl(fd, F_SETFD, FD_CLOEXEC) failed: %m\n");
146 val
= fcntl (fd
, F_GETFD
);
149 printf ("fcntl(fd, F_GETFD) after F_SETFD failed: %m\n");
152 else if ((val
& FD_CLOEXEC
) == 0)
154 puts ("FD_CLOEXEC not set");
159 /* Get a number of a free descriptor. If /dev/null is not available
160 don't continue testing. */
161 fd2
= open (_PATH_DEVNULL
, O_RDWR
);
166 fd3
= fcntl (fd
, F_DUPFD
, fd2
+ 1);
169 printf ("fcntl(fd, F_DUPFD, %d) failed: %m\n", fd2
+ 1);
174 printf ("F_DUPFD returned %d which is not larger than %d\n", fd3
, fd2
);
180 val
= fcntl (fd3
, F_GETFD
);
183 printf ("fcntl(fd3, F_GETFD) after F_DUPFD failed: %m\n");
186 else if ((val
& FD_CLOEXEC
) != 0)
188 puts ("FD_CLOEXEC still set");