maint: update all copyright year number ranges
[gnulib.git] / lib / pipe-filter-aux.h
1 /* Auxiliary code for filtering of data through a subprocess.
2    Copyright (C) 2001-2003, 2008-2013 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 _GL_INLINE_HEADER_BEGIN
19 #ifndef PIPE_FILTER_AUX_INLINE
20 # define PIPE_FILTER_AUX_INLINE _GL_INLINE
21 #endif
22
23 #ifndef SSIZE_MAX
24 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
25 #endif
26
27 /* We use a child process, and communicate through a bidirectional pipe.
28    To avoid deadlocks, let the child process decide when it wants to read
29    or to write, and let the parent behave accordingly.  The parent uses
30    select() to know whether it must write or read.  On platforms without
31    select(), we use non-blocking I/O.  (This means the parent is busy
32    looping while waiting for the child.  Not good.  But hardly any platform
33    lacks select() nowadays.)  */
34
35 /* On BeOS select() works only on sockets, not on normal file descriptors.  */
36 #ifdef __BEOS__
37 # undef HAVE_SELECT
38 #endif
39
40 #ifdef EINTR
41
42 /* EINTR handling for close(), read(), write(), select().
43    These functions can return -1/EINTR even though we don't have any
44    signal handlers set up, namely when we get interrupted via SIGSTOP.  */
45
46 PIPE_FILTER_AUX_INLINE int
47 nonintr_close (int fd)
48 {
49   int retval;
50
51   do
52     retval = close (fd);
53   while (retval < 0 && errno == EINTR);
54
55   return retval;
56 }
57 #undef close /* avoid warning related to gnulib module unistd */
58 #define close nonintr_close
59
60 PIPE_FILTER_AUX_INLINE ssize_t
61 nonintr_read (int fd, void *buf, size_t count)
62 {
63   ssize_t retval;
64
65   do
66     retval = read (fd, buf, count);
67   while (retval < 0 && errno == EINTR);
68
69   return retval;
70 }
71 #define read nonintr_read
72
73 PIPE_FILTER_AUX_INLINE ssize_t
74 nonintr_write (int fd, const void *buf, size_t count)
75 {
76   ssize_t retval;
77
78   do
79     retval = write (fd, buf, count);
80   while (retval < 0 && errno == EINTR);
81
82   return retval;
83 }
84 #undef write /* avoid warning on VMS */
85 #define write nonintr_write
86
87 # if HAVE_SELECT
88
89 PIPE_FILTER_AUX_INLINE int
90 nonintr_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
91                 struct timeval *timeout)
92 {
93   int retval;
94
95   do
96     retval = select (n, readfds, writefds, exceptfds, timeout);
97   while (retval < 0 && errno == EINTR);
98
99   return retval;
100 }
101 #  undef select /* avoid warning on VMS */
102 #  define select nonintr_select
103
104 # endif
105
106 #endif
107
108 /* Non-blocking I/O.  */
109 #if HAVE_SELECT
110 # define IS_EAGAIN(errcode) 0
111 #else
112 # ifdef EWOULDBLOCK
113 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN || (errcode) == EWOULDBLOCK)
114 # else
115 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN)
116 # endif
117 #endif
118
119 _GL_INLINE_HEADER_END