Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / gnulib-lib / pipe2.c
1 /* Create a pipe, with specific opening flags.
2    Copyright (C) 2009, 2010 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 3, 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 #include "binary-io.h"
27
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 /* Native Woe32 API.  */
30
31 # include <io.h>
32
33 #else
34 /* Unix API.  */
35
36 # ifndef O_CLOEXEC
37 #  define O_CLOEXEC 0
38 # endif
39
40 #endif
41
42 int
43 pipe2 (int fd[2], int flags)
44 {
45 #if HAVE_PIPE2
46 # undef pipe2
47   /* Try the system call first, if it exists.  (We may be running with a glibc
48      that has the function but with an older kernel that lacks it.)  */
49   {
50     /* Cache the information whether the system call really exists.  */
51     static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
52     if (have_pipe2_really >= 0)
53       {
54         int result = pipe2 (fd, flags);
55         if (!(result < 0 && errno == ENOSYS))
56           {
57             have_pipe2_really = 1;
58             return result;
59           }
60         have_pipe2_really = -1;
61       }
62   }
63 #endif
64
65 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
66 /* Native Woe32 API.  */
67
68   /* Check the supported flags.  */
69   if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
70     {
71       errno = EINVAL;
72       return -1;
73     }
74
75   return _pipe (fd, 4096, flags);
76
77 #else
78 /* Unix API.  */
79
80   /* Check the supported flags.  */
81   if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_TEXT | O_BINARY)) != 0)
82     {
83       errno = EINVAL;
84       return -1;
85     }
86
87   if (pipe (fd) < 0)
88     return -1;
89
90   /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
91      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
92      both fd[0] amd fd[1].  */
93
94   if (flags & O_NONBLOCK)
95     {
96       int fcntl_flags;
97
98       if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
99           || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
100           || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
101           || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
102         goto fail;
103     }
104
105   if (flags & O_CLOEXEC)
106     {
107       int fcntl_flags;
108
109       if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
110           || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
111           || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
112           || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
113         goto fail;
114     }
115
116 # if O_BINARY
117   if (flags & O_BINARY)
118     {
119       setmode (fd[1], O_BINARY);
120       setmode (fd[0], O_BINARY);
121     }
122   else if (flags & O_TEXT)
123     {
124       setmode (fd[1], O_TEXT);
125       setmode (fd[0], O_TEXT);
126     }
127 # endif
128
129   return 0;
130
131  fail:
132   {
133     int saved_errno = errno;
134     close (fd[0]);
135     close (fd[1]);
136     errno = saved_errno;
137     return -1;
138   }
139
140 #endif
141 }