annotate lib/pipe2.c @ 11880:d5e0338c205a

Support O_TEXT, O_BINARY on all platforms.
author Bruno Haible <bruno@clisp.org>
date Sun, 23 Aug 2009 10:46:39 +0200
parents bd0609bf2760
children 9e30ae0c65a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Create a pipe, with specific opening flags.
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2009 Free Software Foundation, Inc.
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 with this program; if not, write to the Free Software Foundation,
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <unistd.h>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <errno.h>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <fcntl.h>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* Native Woe32 API. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # include <io.h>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 int
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 pipe2 (int fd[2], int flags)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Check the supported flags. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 errno = EINVAL;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 return -1;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 return _pipe (fd, 4096, flags);
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #else
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* Unix API. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 # ifndef O_CLOEXEC
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 # define O_CLOEXEC 0
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # endif
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 pipe2 (int fd[2], int flags)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* Check the supported flags. */
11880
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
55 if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_TEXT | O_BINARY)) != 0)
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 errno = EINVAL;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 return -1;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 if (pipe (fd) < 0)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 return -1;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 both fd[0] amd fd[1]. */
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (flags & O_NONBLOCK)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 int fcntl_flags;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
11879
bd0609bf2760 Fix test of fcntl's return value.
Bruno Haible <bruno@clisp.org>
parents: 11870
diff changeset
73 || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
11879
bd0609bf2760 Fix test of fcntl's return value.
Bruno Haible <bruno@clisp.org>
parents: 11870
diff changeset
75 || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 goto fail;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 if (flags & O_CLOEXEC)
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 int fcntl_flags;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
11879
bd0609bf2760 Fix test of fcntl's return value.
Bruno Haible <bruno@clisp.org>
parents: 11870
diff changeset
84 || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
11879
bd0609bf2760 Fix test of fcntl's return value.
Bruno Haible <bruno@clisp.org>
parents: 11870
diff changeset
86 || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 goto fail;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
11880
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
90 #if O_BINARY
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
91 if (flags & O_BINARY)
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
92 setmode (fd, O_BINARY);
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
93 else if (flags & O_TEXT)
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
94 setmode (fd, O_TEXT);
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
95 #endif
d5e0338c205a Support O_TEXT, O_BINARY on all platforms.
Bruno Haible <bruno@clisp.org>
parents: 11879
diff changeset
96
11870
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 return 0;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 fail:
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 {
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 int saved_errno = errno;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 close (fd[0]);
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 close (fd[1]);
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 errno = saved_errno;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 return -1;
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
ddd29d8cdded New module 'pipe2'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 #endif