source: branches/libc-0.6/src/libctests/glibc/io/tst-fcntl.c

Last change on this file was 2176, checked in by bird, 20 years ago

..

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/* Tests for fcntl.
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.
5
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.
10
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.
15
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
19 02111-1307 USA. */
20
21#include <errno.h>
22#include <fcntl.h>
23#include <paths.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/stat.h>
27#ifndef HAVE_64BIT_FILEIO_TYPES
28#define stat64 stat
29#define fstat64 fstat
30#endif
31
32/* Prototype for our test function. */
33extern void do_prepare (int argc, char *argv[]);
34extern int do_test (int argc, char *argv[]);
35
36/* We have a preparation function. */
37#define PREPARE do_prepare
38
39#include "../test-skeleton.c"
40
41
42/* Name of the temporary files. */
43static char *name;
44
45void
46do_prepare (int argc, char *argv[])
47{
48 char name_len;
49
50 name_len = strlen (test_dir);
51 name = malloc (name_len + sizeof ("/fcntlXXXXXX"));
52 mempcpy (mempcpy (name, test_dir, name_len),
53 "/fcntlXXXXXX", sizeof ("/fcntlXXXXXX"));
54 add_temp_file (name);
55}
56
57
58int
59do_test (int argc, char *argv[])
60{
61 int fd;
62 int fd2;
63 int fd3;
64 struct stat64 st;
65 int val;
66 int result = 0;
67
68 /* Create the temporary file. */
69 fd = mkstemp (name);
70 if (fd == -1)
71 {
72 printf ("cannot open temporary file: %m\n");
73 return 1;
74 }
75 if (fstat64 (fd, &st) != 0)
76 {
77 printf ("cannot stat test file: %m\n");
78 return 1;
79 }
80 if (! S_ISREG (st.st_mode) || st.st_size != 0)
81 {
82 puts ("file not created correctly");
83 return 1;
84 }
85
86 /* Get the flags with fcntl(). */
87 val = fcntl (fd, F_GETFL);
88 if (val == -1)
89 {
90 printf ("fcntl(fd, F_GETFL) failed: %m\n");
91 result = 1;
92 }
93 else if ((val & O_ACCMODE) != O_RDWR)
94 {
95 puts ("temporary file not opened for read and write");
96 result = 1;
97 }
98
99 /* Set the flags to something else. */
100 if (fcntl (fd, F_SETFL, O_RDONLY) == -1)
101 {
102 printf ("fcntl(fd, F_SETFL, O_RDONLY) failed: %m\n");
103 result = 1;
104 }
105
106 val = fcntl (fd, F_GETFL);
107 if (val == -1)
108 {
109 printf ("fcntl(fd, F_GETFL) after F_SETFL failed: %m\n");
110 result = 1;
111 }
112 else if ((val & O_ACCMODE) != O_RDWR)
113 {
114 puts ("temporary file access mode changed");
115 result = 1;
116 }
117
118 /* Set the flags to something else. */
119 if (fcntl (fd, F_SETFL, O_APPEND) == -1)
120 {
121 printf ("fcntl(fd, F_SETFL, O_APPEND) failed: %m\n");
122 result = 1;
123 }
124
125 val = fcntl (fd, F_GETFL);
126 if (val == -1)
127 {
128 printf ("fcntl(fd, F_GETFL) after second F_SETFL failed: %m\n");
129 result = 1;
130 }
131 else if ((val & O_APPEND) == 0)
132 {
133 puts ("O_APPEND not set");
134 result = 1;
135 }
136
137 val = fcntl (fd, F_GETFD);
138 if (val == -1)
139 {
140 printf ("fcntl(fd, F_GETFD) failed: %m\n");
141 result = 1;
142 }
143 else if (fcntl (fd, F_SETFD, val | FD_CLOEXEC) == -1)
144 {
145 printf ("fcntl(fd, F_SETFD, FD_CLOEXEC) failed: %m\n");
146 result = 1;
147 }
148 else
149 {
150 val = fcntl (fd, F_GETFD);
151 if (val == -1)
152 {
153 printf ("fcntl(fd, F_GETFD) after F_SETFD failed: %m\n");
154 result = 1;
155 }
156 else if ((val & FD_CLOEXEC) == 0)
157 {
158 puts ("FD_CLOEXEC not set");
159 result = 1;
160 }
161 }
162
163 /* Get a number of a free descriptor. If /dev/null is not available
164 don't continue testing. */
165 fd2 = open (_PATH_DEVNULL, O_RDWR);
166 if (fd2 == -1)
167 return result;
168 close (fd2);
169
170 fd3 = fcntl (fd, F_DUPFD, fd2 + 1);
171 if (fd3 == -1)
172 {
173 printf ("fcntl(fd, F_DUPFD, %d) failed: %m\n", fd2 + 1);
174 result = 1;
175 }
176 else if (fd3 <= fd2)
177 {
178 printf ("F_DUPFD returned %d which is not larger than %d\n", fd3, fd2);
179 result = 1;
180 }
181
182 if (fd3 != -1)
183 {
184 val = fcntl (fd3, F_GETFD);
185 if (val == -1)
186 {
187 printf ("fcntl(fd3, F_GETFD) after F_DUPFD failed: %m\n");
188 result = 1;
189 }
190 else if ((val & FD_CLOEXEC) != 0)
191 {
192 puts ("FD_CLOEXEC still set");
193 result = 1;
194 }
195
196 close (fd3);
197 }
198
199 return result;
200}
Note: See TracBrowser for help on using the repository browser.