1 /* input_avail.c -- check whether or not data is available for reading on a
2 specified file descriptor. */
4 /* Copyright (C) 2008,2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
22 #if defined (__TANDEM)
26 #if defined (HAVE_CONFIG_H)
30 #include <sys/types.h>
32 #if defined (HAVE_SYS_FILE_H)
33 # include <sys/file.h>
34 #endif /* HAVE_SYS_FILE_H */
36 #if defined (HAVE_UNISTD_H)
38 #endif /* HAVE_UNISTD_H */
42 #if defined (HAVE_SELECT)
43 # if !defined (HAVE_SYS_SELECT_H) || !defined (M_UNIX)
44 # include <sys/time.h>
46 #endif /* HAVE_SELECT */
47 #if defined (HAVE_SYS_SELECT_H)
48 # include <sys/select.h>
51 #if defined (FIONREAD_IN_SYS_IOCTL)
52 # include <sys/ioctl.h>
62 #if !defined (O_NDELAY) && defined (O_NONBLOCK)
63 # define O_NDELAY O_NONBLOCK /* Posix style */
66 /* Return >= 1 if select/FIONREAD indicates data available for reading on
67 file descriptor FD; 0 if no data available. Return -1 on error. */
72 int result, chars_avail;
73 #if defined(HAVE_SELECT)
74 fd_set readfds, exceptfds;
75 struct timeval timeout;
83 #if defined (HAVE_SELECT)
86 FD_SET (fd, &readfds);
87 FD_SET (fd, &exceptfds);
90 result = select (fd + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout);
91 return ((result <= 0) ? 0 : 1);
96 #if defined (FIONREAD)
98 result = ioctl (fd, FIONREAD, &chars_avail);
99 if (result == -1 && errno == EIO)
101 return (chars_avail);