Move #ifs inside function.
[gnulib.git] / lib / pipe2.c
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 #else
32 /* Unix API.  */
33
34 # ifndef O_CLOEXEC
35 #  define O_CLOEXEC 0
36 # endif
37
38 #endif
39
40 int
41 pipe2 (int fd[2], int flags)
42 {
43 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
44 /* Native Woe32 API.  */
45
46   /* Check the supported flags.  */
47   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
48     {
49       errno = EINVAL;
50       return -1;
51     }
52
53   return _pipe (fd, 4096, flags);
54
55 #else
56 /* Unix API.  */
57
58   /* Check the supported flags.  */
59   if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_TEXT | O_BINARY)) != 0)
60     {
61       errno = EINVAL;
62       return -1;
63     }
64
65   if (pipe (fd) < 0)
66     return -1;
67
68   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
69      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
70      both fd[0] amd fd[1].  */
71
72   if (flags & O_NONBLOCK)
73     {
74       int fcntl_flags;
75
76       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
77           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
78           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
79           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
80         goto fail;
81     }
82
83   if (flags & O_CLOEXEC)
84     {
85       int fcntl_flags;
86
87       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
88           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
89           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
90           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
91         goto fail;
92     }
93
94 # if O_BINARY
95   if (flags & O_BINARY)
96     {
97       setmode (fd[1], O_BINARY);
98       setmode (fd[0], O_BINARY);
99     }
100   else if (flags & O_TEXT)
101     {
102       setmode (fd[1], O_TEXT);
103       setmode (fd[0], O_TEXT);
104     }
105 # endif
106
107   return 0;
108
109  fail:
110   {
111     int saved_errno = errno;
112     close (fd[0]);
113     close (fd[1]);
114     errno = saved_errno;
115     return -1;
116   }
117
118 #endif
119 }