Mercurial > hg > octave-shane > gnulib-hg
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 |
rev | line source |
---|---|
11870 | 1 /* Create a pipe, with specific opening flags. |
2 Copyright (C) 2009 Free Software Foundation, Inc. | |
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 2, 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 along | |
15 with this program; if not, write to the Free Software Foundation, | |
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | |
17 | |
18 #include <config.h> | |
19 | |
20 /* Specification. */ | |
21 #include <unistd.h> | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 | |
26 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
27 /* Native Woe32 API. */ | |
28 | |
29 # include <io.h> | |
30 | |
31 int | |
32 pipe2 (int fd[2], int flags) | |
33 { | |
34 /* Check the supported flags. */ | |
35 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0) | |
36 { | |
37 errno = EINVAL; | |
38 return -1; | |
39 } | |
40 | |
41 return _pipe (fd, 4096, flags); | |
42 } | |
43 | |
44 #else | |
45 /* Unix API. */ | |
46 | |
47 # ifndef O_CLOEXEC | |
48 # define O_CLOEXEC 0 | |
49 # endif | |
50 | |
51 int | |
52 pipe2 (int fd[2], int flags) | |
53 { | |
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 | 56 { |
57 errno = EINVAL; | |
58 return -1; | |
59 } | |
60 | |
61 if (pipe (fd) < 0) | |
62 return -1; | |
63 | |
64 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html> | |
65 says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on | |
66 both fd[0] amd fd[1]. */ | |
67 | |
68 if (flags & O_NONBLOCK) | |
69 { | |
70 int fcntl_flags; | |
71 | |
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 | 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 | 76 goto fail; |
77 } | |
78 | |
79 if (flags & O_CLOEXEC) | |
80 { | |
81 int fcntl_flags; | |
82 | |
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 | 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 | 87 goto fail; |
88 } | |
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 | 97 return 0; |
98 | |
99 fail: | |
100 { | |
101 int saved_errno = errno; | |
102 close (fd[0]); | |
103 close (fd[1]); | |
104 errno = saved_errno; | |
105 return -1; | |
106 } | |
107 } | |
108 | |
109 #endif |