Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-dup3.c @ 14350:4cf3b58aaf12
Don't interfere with a program's definition of __attribute__.
* lib/argp.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(argp_error, __argp_error, argp_failure, __argp_failure): Use it.
* lib/argp-fmtstream.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(__argp_fmtstream_printf, argp_fmtstream_printf): Use it.
* lib/argp-help.c (hol_entry_long_iterate): Use __attribute__ only for
GCC 3 or newer.
* lib/error.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(error, error_at_line): Use it.
* lib/hash.h (__attribute__): Remove definition.
(ATTRIBUTE_WUR): Update definition. Define always.
* lib/openat.h (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
* lib/sigpipe-die.h (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
* lib/vasnprintf.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(asnprintf, vasnprintf): Use it.
* lib/xalloc.h (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
(ATTRIBUTE_MALLOC, ATTRIBUTE_ALLOC_SIZE): Define always.
* lib/xmemdup0.h (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
* lib/xprintf.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(xprintf, xvprintf, xfprintf, xvfprintf): Use it.
* lib/xstrtol.h (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
* lib/xvasprintf.h (__attribute__): Remove definition.
(_GL_ATTRIBUTE_FORMAT): New macro.
(xasprintf, xvasprintf): Use it.
* tests/test-argmatch.c (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
* tests/test-exclude.c (__attribute__): Remove definition.
(ATTRIBUTE_NORETURN): Update definition. Define always.
Reported by Paul Eggert.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 13 Feb 2011 23:21:20 +0100 |
parents | 97fc9a21a8fb |
children | b86e9061a6d0 |
rev | line source |
---|---|
11887 | 1 /* Test duplicating file descriptors. |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
13664
diff
changeset
|
2 Copyright (C) 2009-2011 Free Software Foundation, Inc. |
11887 | 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 of the License, or | |
7 (at your option) 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 | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009, | |
18 and Bruno Haible <bruno@clisp.org>, 2009. */ | |
19 | |
20 #include <config.h> | |
21 | |
22 #include <unistd.h> | |
23 | |
12489 | 24 #include "signature.h" |
25 SIGNATURE_CHECK (dup3, int, (int, int, int)); | |
26 | |
11887 | 27 #include <errno.h> |
28 #include <fcntl.h> | |
29 #include <stdbool.h> | |
30 | |
31 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
32 /* Get declarations of the Win32 API functions. */ | |
33 # define WIN32_LEAN_AND_MEAN | |
34 # include <windows.h> | |
35 #endif | |
36 | |
37 #include "binary-io.h" | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
38 #include "macros.h" |
11887 | 39 |
40 /* Return true if FD is open. */ | |
41 static bool | |
42 is_open (int fd) | |
43 { | |
44 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
45 /* On Win32, the initial state of unassigned standard file | |
46 descriptors is that they are open but point to an | |
47 INVALID_HANDLE_VALUE, and there is no fcntl. */ | |
48 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; | |
49 #else | |
50 # ifndef F_GETFL | |
51 # error Please port fcntl to your platform | |
52 # endif | |
53 return 0 <= fcntl (fd, F_GETFL); | |
54 #endif | |
55 } | |
56 | |
57 /* Return true if FD is not inherited to child processes. */ | |
58 static bool | |
59 is_cloexec (int fd) | |
60 { | |
61 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
62 HANDLE h = (HANDLE) _get_osfhandle (fd); | |
63 DWORD flags; | |
64 ASSERT (GetHandleInformation (h, &flags)); | |
65 return (flags & HANDLE_FLAG_INHERIT) == 0; | |
66 #else | |
67 int flags; | |
68 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0); | |
69 return (flags & FD_CLOEXEC) != 0; | |
70 #endif | |
71 } | |
72 | |
73 int | |
74 main () | |
75 { | |
76 int use_cloexec; | |
77 | |
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
|
78 #if O_CLOEXEC |
11887 | 79 for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++) |
80 #else | |
81 use_cloexec = 0; | |
82 #endif | |
83 { | |
84 const char *file = "test-dup3.tmp"; | |
85 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600); | |
86 int o_flags; | |
87 char buffer[1]; | |
88 | |
89 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
|
90 #if O_CLOEXEC |
11887 | 91 if (use_cloexec) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11887
diff
changeset
|
92 o_flags |= O_CLOEXEC; |
11887 | 93 #endif |
94 | |
95 /* Assume std descriptors were provided by invoker. */ | |
96 ASSERT (STDERR_FILENO < fd); | |
97 ASSERT (is_open (fd)); | |
98 /* Ignore any other fd's leaked into this process. */ | |
99 close (fd + 1); | |
100 close (fd + 2); | |
101 ASSERT (!is_open (fd + 1)); | |
102 ASSERT (!is_open (fd + 2)); | |
103 | |
104 /* Assigning to self is invalid. */ | |
105 errno = 0; | |
106 ASSERT (dup3 (fd, fd, o_flags) == -1); | |
107 ASSERT (errno == EINVAL); | |
108 ASSERT (is_open (fd)); | |
109 | |
110 /* If the source is not open, then the destination is unaffected. */ | |
111 errno = 0; | |
112 ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1); | |
113 ASSERT (errno == EBADF); | |
114 ASSERT (!is_open (fd + 2)); | |
115 errno = 0; | |
116 ASSERT (dup3 (fd + 1, fd, o_flags) == -1); | |
117 ASSERT (errno == EBADF); | |
118 ASSERT (is_open (fd)); | |
119 | |
120 /* The destination must be valid. */ | |
121 errno = 0; | |
122 ASSERT (dup3 (fd, -2, o_flags) == -1); | |
123 ASSERT (errno == EBADF); | |
124 errno = 0; | |
125 ASSERT (dup3 (fd, 10000000, o_flags) == -1); | |
126 ASSERT (errno == EBADF); | |
127 | |
128 /* Using dup3 can skip fds. */ | |
129 ASSERT (dup3 (fd, fd + 2, o_flags) == fd + 2); | |
130 ASSERT (is_open (fd)); | |
131 ASSERT (!is_open (fd + 1)); | |
132 ASSERT (is_open (fd + 2)); | |
133 if (use_cloexec) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11887
diff
changeset
|
134 ASSERT (is_cloexec (fd + 2)); |
11887 | 135 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11887
diff
changeset
|
136 ASSERT (!is_cloexec (fd + 2)); |
11887 | 137 |
138 /* Verify that dup3 closes the previous occupant of a fd. */ | |
139 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1); | |
140 ASSERT (dup3 (fd + 1, fd, o_flags) == fd); | |
141 ASSERT (close (fd + 1) == 0); | |
142 ASSERT (write (fd, "1", 1) == 1); | |
143 ASSERT (dup3 (fd + 2, fd, o_flags) == fd); | |
144 ASSERT (lseek (fd, 0, SEEK_END) == 0); | |
145 ASSERT (write (fd + 2, "2", 1) == 1); | |
146 ASSERT (lseek (fd, 0, SEEK_SET) == 0); | |
147 ASSERT (read (fd, buffer, 1) == 1); | |
148 ASSERT (*buffer == '2'); | |
149 | |
150 /* Clean up. */ | |
151 ASSERT (close (fd + 2) == 0); | |
152 ASSERT (close (fd) == 0); | |
153 ASSERT (unlink (file) == 0); | |
154 } | |
155 | |
156 return 0; | |
157 } |