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