Mercurial > gnulib
annotate tests/test-pipe2.c @ 17363:5a51fb7777a9
sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin
Problem reported by Marco Atzeri in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>.
* lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]:
Simply delegate to the system <sys/select.h> in this case too.
Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only
if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to
be needed on Solaris either.
* lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]:
Simply delgate to the system <sys/time.h> in this case.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 19 Mar 2013 09:08:47 -0700 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
11871 | 1 /* Test of pipe2. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16366
diff
changeset
|
2 Copyright (C) 2009-2013 Free Software Foundation, Inc. |
11871 | 3 |
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, or (at your option) | |
7 any later version. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
16366
bb182ee4a09d
maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents:
16214
diff
changeset
|
15 along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
11871 | 16 |
17 #include <config.h> | |
18 | |
19 #include <unistd.h> | |
20 | |
12489 | 21 #include "signature.h" |
22 SIGNATURE_CHECK (pipe2, int, (int[2], int)); | |
23 | |
11871 | 24 #include <fcntl.h> |
25 #include <stdbool.h> | |
26 | |
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
28 /* Get declarations of the native Windows API functions. */ |
11871 | 29 # define WIN32_LEAN_AND_MEAN |
30 # include <windows.h> | |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
14856
diff
changeset
|
31 /* Get _get_osfhandle. */ |
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
14856
diff
changeset
|
32 # include "msvc-nothrow.h" |
11871 | 33 #endif |
34 | |
35 #include "binary-io.h" | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
36 #include "macros.h" |
14856
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
37 #if GNULIB_NONBLOCKING |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
38 # include "nonblocking.h" |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
39 #endif |
11871 | 40 |
41 /* Return true if FD is open. */ | |
42 static bool | |
43 is_open (int fd) | |
44 { | |
45 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
46 /* On native Windows, the initial state of unassigned standard file |
11871 | 47 descriptors is that they are open but point to an |
48 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
49 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; | |
50 #else | |
51 # ifndef F_GETFL | |
52 # error Please port fcntl to your platform | |
53 # endif | |
54 return 0 <= fcntl (fd, F_GETFL); | |
55 #endif | |
56 } | |
57 | |
58 /* Return true if FD is not inherited to child processes. */ | |
59 static bool | |
60 is_cloexec (int fd) | |
61 { | |
62 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
63 HANDLE h = (HANDLE) _get_osfhandle (fd); | |
64 DWORD flags; | |
65 ASSERT (GetHandleInformation (h, &flags)); | |
66 return (flags & HANDLE_FLAG_INHERIT) == 0; | |
67 #else | |
68 int flags; | |
69 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0); | |
70 return (flags & FD_CLOEXEC) != 0; | |
71 #endif | |
72 } | |
73 | |
14856
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
74 #if ! GNULIB_NONBLOCKING |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
75 static int |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
76 get_nonblocking_flag (int fd) |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
77 { |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
78 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
79 return 0; |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
80 # else |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
81 # ifndef F_GETFL |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
82 # error Please port fcntl to your platform |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
83 # endif |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
84 int flags; |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
85 ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0); |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
86 return (flags & O_NONBLOCK) != 0; |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
87 # endif |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
88 } |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
89 #endif |
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
90 |
11871 | 91 int |
92 main () | |
93 { | |
94 int use_nonblocking; | |
95 int use_cloexec; | |
96 | |
14856
c593fd7d2ab9
pipe2: Remove dependency on 'nonblocking' module.
Bruno Haible <bruno@clisp.org>
parents:
14535
diff
changeset
|
97 for (use_nonblocking = 0; use_nonblocking <= !!O_NONBLOCK; use_nonblocking++) |
14535
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
98 for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++) |
11871 | 99 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
100 int o_flags; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
101 int fd[2]; |
11871 | 102 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
103 o_flags = 0; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
104 if (use_nonblocking) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
105 o_flags |= O_NONBLOCK; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
106 if (use_cloexec) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
107 o_flags |= O_CLOEXEC; |
11871 | 108 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
109 fd[0] = -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
110 fd[1] = -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
111 ASSERT (pipe2 (fd, o_flags) >= 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
112 ASSERT (fd[0] >= 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
113 ASSERT (fd[1] >= 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
114 ASSERT (fd[0] != fd[1]); |
13832
992d47604da1
tests: fix compiler warnings
Eric Blake <eblake@redhat.com>
parents:
13664
diff
changeset
|
115 ASSERT (is_open (fd[0])); |
992d47604da1
tests: fix compiler warnings
Eric Blake <eblake@redhat.com>
parents:
13664
diff
changeset
|
116 ASSERT (is_open (fd[1])); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
117 if (use_cloexec) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
118 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
119 ASSERT (is_cloexec (fd[0])); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
120 ASSERT (is_cloexec (fd[1])); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
121 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
122 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
123 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
124 ASSERT (!is_cloexec (fd[0])); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
125 ASSERT (!is_cloexec (fd[1])); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
126 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
127 if (use_nonblocking) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
128 { |
14535
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
129 ASSERT (get_nonblocking_flag (fd[0]) == 1); |
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
130 ASSERT (get_nonblocking_flag (fd[1]) == 1); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
131 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
132 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
133 { |
14535
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
134 ASSERT (get_nonblocking_flag (fd[0]) == 0); |
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
135 ASSERT (get_nonblocking_flag (fd[1]) == 0); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11871
diff
changeset
|
136 } |
14535
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
137 |
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
138 ASSERT (close (fd[0]) == 0); |
7ba508565384
pipe2: fix O_NONBLOCK support on mingw
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
139 ASSERT (close (fd[1]) == 0); |
11871 | 140 } |
141 | |
142 return 0; | |
143 } |