maint: update almost all copyright ranges to include 2011
[gnulib.git] / lib / pipe-filter-aux.h
1 /* Auxiliary code for filtering of data through a subprocess.
2    Copyright (C) 2001-2003, 2008-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18
19 #ifndef SSIZE_MAX
20 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
21 #endif
22
23 /* We use a child process, and communicate through a bidirectional pipe.
24    To avoid deadlocks, let the child process decide when it wants to read
25    or to write, and let the parent behave accordingly.  The parent uses
26    select() to know whether it must write or read.  On platforms without
27    select(), we use non-blocking I/O.  (This means the parent is busy
28    looping while waiting for the child.  Not good.  But hardly any platform
29    lacks select() nowadays.)  */
30
31 /* On BeOS select() works only on sockets, not on normal file descriptors.  */
32 #ifdef __BEOS__
33 # undef HAVE_SELECT
34 #endif
35
36 #ifdef EINTR
37
38 /* EINTR handling for close(), read(), write(), select().
39    These functions can return -1/EINTR even though we don't have any
40    signal handlers set up, namely when we get interrupted via SIGSTOP.  */
41
42 static inline int
43 nonintr_close (int fd)
44 {
45   int retval;
46
47   do
48     retval = close (fd);
49   while (retval < 0 && errno == EINTR);
50
51   return retval;
52 }
53 #undef close /* avoid warning related to gnulib module unistd */
54 #define close nonintr_close
55
56 static inline ssize_t
57 nonintr_read (int fd, void *buf, size_t count)
58 {
59   ssize_t retval;
60
61   do
62     retval = read (fd, buf, count);
63   while (retval < 0 && errno == EINTR);
64
65   return retval;
66 }
67 #define read nonintr_read
68
69 static inline ssize_t
70 nonintr_write (int fd, const void *buf, size_t count)
71 {
72   ssize_t retval;
73
74   do
75     retval = write (fd, buf, count);
76   while (retval < 0 && errno == EINTR);
77
78   return retval;
79 }
80 #undef write /* avoid warning on VMS */
81 #define write nonintr_write
82
83 # if HAVE_SELECT
84
85 static inline int
86 nonintr_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
87                 struct timeval *timeout)
88 {
89   int retval;
90
91   do
92     retval = select (n, readfds, writefds, exceptfds, timeout);
93   while (retval < 0 && errno == EINTR);
94
95   return retval;
96 }
97 #  undef select /* avoid warning on VMS */
98 #  define select nonintr_select
99
100 # endif
101
102 #endif
103
104 /* Non-blocking I/O.  */
105 #ifndef O_NONBLOCK
106 # define O_NONBLOCK O_NDELAY
107 #endif
108 #if HAVE_SELECT
109 # define IS_EAGAIN(errcode) 0
110 #else
111 # ifdef EWOULDBLOCK
112 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN || (errcode) == EWOULDBLOCK)
113 # else
114 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN)
115 # endif
116 #endif