1 /* zread - read data from file descriptor into buffer with retries */
3 /* Copyright (C) 1999-2002 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
25 #if defined (HAVE_UNISTD_H)
40 extern void check_signals_and_traps (void);
41 extern int signal_is_trapped (int);
43 /* Read LEN bytes from FD into BUF. Retry the read on EINTR. Any other
44 error causes the loop to break. */
54 #if defined (HAVE_SIGINTERRUPT)
55 if (signal_is_trapped (SIGCHLD))
56 siginterrupt (SIGCHLD, 1);
60 while ((r = read (fd, buf, len)) < 0 && errno == EINTR)
61 check_signals_and_traps (); /* XXX - should it be check_signals()? */
64 #if defined (HAVE_SIGINTERRUPT)
65 siginterrupt (SIGCHLD, 0);
72 /* Read LEN bytes from FD into BUF. Retry the read on EINTR, up to three
73 interrupts. Any other error causes the loop to break. */
81 zreadretry (fd, buf, len)
91 r = read (fd, buf, len);
94 if (r == -1 && errno == EINTR)
96 if (++nintr >= NUM_INTR)
104 /* Call read(2) and allow it to be interrupted. Just a stub for now. */
106 zreadintr (fd, buf, len)
111 return (read (fd, buf, len));
114 /* Read one character from FD and return it in CP. Return values are as
115 in read(2). This does some local buffering to avoid many one-character
116 calls to read(2), like those the `read' builtin performs. */
118 static char lbuf[128];
119 static size_t lind, lused;
128 if (lind == lused || lused == 0)
130 nr = zread (fd, lbuf, sizeof (lbuf));
144 /* Don't mix calls to zreadc and zreadcintr in the same function, since they
145 use the same local buffer. */
153 if (lind == lused || lused == 0)
155 nr = zreadintr (fd, lbuf, sizeof (lbuf));
169 /* Like zreadc, but read a specified number of characters at a time. Used
179 if (lind == lused || lused == 0)
181 if (len > sizeof (lbuf))
183 nr = zread (fd, lbuf, len);
203 /* Sync the seek pointer for FD so that the kernel's idea of the last char
204 read is the last char returned by zreadc. */
214 r = lseek (fd, -off, SEEK_CUR);