annotate tests/test-dup3.c @ 17682:68b0a0f80871

pthread_sigmask, timer-time: use gl_THREADLIB only if needed Without this fix, Emacs would sometimes call sigprocmask instead of pthread_sigmask, which is a no-no in multithreaded applications. Problem reported by Jorgen Schaefer in <http://bugs.gnu.org/17561>. * m4/pthread_sigmask.m4 (gl_FUNC_PTHREAD_SIGMASK): Suppress check for pthread_sigmask working without -lpthread if the application always links with -lpthread. Do not link with $LIBMULTITHREAD if gl_THREADLIB is not defined. * m4/timer_time.m4 (gl_TIMER_TIME): Require gl_THREADLIB only if it is defined. Do not append $LIBMULTITHREAD to LIB_TIMER_TIME if gl_THREADLIB is not defined.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 28 May 2014 21:04:04 -0700
parents 344018b6e5d7
children ab58d4870664
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test duplicating file descriptors.
17587
344018b6e5d7 maint: update copyright
Eric Blake <eblake@redhat.com>
parents: 17509
diff changeset
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009,
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 and Bruno Haible <bruno@clisp.org>, 2009. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <config.h>
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <unistd.h>
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25 SIGNATURE_CHECK (dup3, int, (int, int, int));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
26
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <errno.h>
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <fcntl.h>
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include <stdbool.h>
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #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
32 /* Get declarations of the native Windows API functions. */
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # define WIN32_LEAN_AND_MEAN
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # include <windows.h>
15752
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35 /* Get _get_osfhandle. */
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
36 # include "msvc-nothrow.h"
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "binary-io.h"
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
40 #include "macros.h"
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* Return true if FD is open. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 static bool
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 is_open (int fd)
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 {
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 #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
47 /* On native Windows, the initial state of unassigned standard file
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 descriptors is that they are open but point to an
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 INVALID_HANDLE_VALUE, and there is no fcntl. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #else
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # ifndef F_GETFL
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 # error Please port fcntl to your platform
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 # endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 return 0 <= fcntl (fd, F_GETFL);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 #endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 }
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 /* Return true if FD is not inherited to child processes. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 static bool
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 is_cloexec (int fd)
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 HANDLE h = (HANDLE) _get_osfhandle (fd);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 DWORD flags;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 ASSERT (GetHandleInformation (h, &flags));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 return (flags & HANDLE_FLAG_INHERIT) == 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 #else
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 int flags;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return (flags & FD_CLOEXEC) != 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 #endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 int
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 main ()
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 {
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 int use_cloexec;
17384
160b88fd4691 tests: don't assume getdtablesize () <= 10000000
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
79 int bad_fd = getdtablesize ();
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
81 #if O_CLOEXEC
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 #else
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 use_cloexec = 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 #endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 {
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 const char *file = "test-dup3.tmp";
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 int o_flags;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 char buffer[1];
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 o_flags = 0;
13664
241057e2e60f fcntl-h: define O_CLOEXEC and O_EXEC if not defined; use new defines
Paul Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
93 #if O_CLOEXEC
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 if (use_cloexec)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11887
diff changeset
95 o_flags |= O_CLOEXEC;
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 #endif
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 /* Assume std descriptors were provided by invoker. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 ASSERT (STDERR_FILENO < fd);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 ASSERT (is_open (fd));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 /* Ignore any other fd's leaked into this process. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 close (fd + 1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 close (fd + 2);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 ASSERT (!is_open (fd + 1));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 ASSERT (!is_open (fd + 2));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 /* Assigning to self is invalid. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 errno = 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 ASSERT (dup3 (fd, fd, o_flags) == -1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 ASSERT (errno == EINVAL);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 ASSERT (is_open (fd));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 /* If the source is not open, then the destination is unaffected. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 errno = 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 ASSERT (errno == EBADF);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 ASSERT (!is_open (fd + 2));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 errno = 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 ASSERT (dup3 (fd + 1, fd, o_flags) == -1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 ASSERT (errno == EBADF);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 ASSERT (is_open (fd));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 /* The destination must be valid. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 errno = 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 ASSERT (dup3 (fd, -2, o_flags) == -1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 ASSERT (errno == EBADF);
17509
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
127 if (bad_fd > 256)
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
128 {
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
129 ASSERT (dup3 (fd, 255, 0) == 255);
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
130 ASSERT (dup3 (fd, 256, 0) == 256);
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
131 ASSERT (close (255) == 0);
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
132 ASSERT (close (256) == 0);
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
133 }
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
134 ASSERT (dup3 (fd, bad_fd - 1, 0) == bad_fd - 1);
7fadcfb1189f dup2, dup3: work around another cygwin crasher
Eric Blake <eblake@redhat.com>
parents: 17384
diff changeset
135 ASSERT (close (bad_fd - 1) == 0);
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 errno = 0;
17384
160b88fd4691 tests: don't assume getdtablesize () <= 10000000
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
137 ASSERT (dup3 (fd, bad_fd, o_flags) == -1);
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 ASSERT (errno == EBADF);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 /* Using dup3 can skip fds. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 ASSERT (dup3 (fd, fd + 2, o_flags) == fd + 2);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 ASSERT (is_open (fd));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 ASSERT (!is_open (fd + 1));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 ASSERT (is_open (fd + 2));
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 if (use_cloexec)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11887
diff changeset
146 ASSERT (is_cloexec (fd + 2));
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11887
diff changeset
148 ASSERT (!is_cloexec (fd + 2));
11887
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 /* Verify that dup3 closes the previous occupant of a fd. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 ASSERT (dup3 (fd + 1, fd, o_flags) == fd);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 ASSERT (close (fd + 1) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 ASSERT (write (fd, "1", 1) == 1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 ASSERT (dup3 (fd + 2, fd, o_flags) == fd);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 ASSERT (lseek (fd, 0, SEEK_END) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 ASSERT (write (fd + 2, "2", 1) == 1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 ASSERT (read (fd, buffer, 1) == 1);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 ASSERT (*buffer == '2');
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 /* Clean up. */
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 ASSERT (close (fd + 2) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 ASSERT (close (fd) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 ASSERT (unlink (file) == 0);
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 }
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 return 0;
ce99ebbf27a3 Tests for module 'dup3'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 }