Line data Source code
1 : /* input_avail.c -- check whether or not data is available for reading on a 2 : specified file descriptor. */ 3 : 4 : /* Copyright (C) 2008,2009 Free Software Foundation, Inc. 5 : 6 : This file is part of GNU Bash, the Bourne Again SHell. 7 : 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. 12 : 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. 17 : 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/>. 20 : */ 21 : 22 : #if defined (__TANDEM) 23 : # include <floss.h> 24 : #endif 25 : 26 : #if defined (HAVE_CONFIG_H) 27 : # include <config.h> 28 : #endif 29 : 30 : #include <sys/types.h> 31 : #include <fcntl.h> 32 : #if defined (HAVE_SYS_FILE_H) 33 : # include <sys/file.h> 34 : #endif /* HAVE_SYS_FILE_H */ 35 : 36 : #if defined (HAVE_UNISTD_H) 37 : # include <unistd.h> 38 : #endif /* HAVE_UNISTD_H */ 39 : 40 : #include "bashansi.h" 41 : 42 : #include "posixselect.h" 43 : 44 : #if defined (FIONREAD_IN_SYS_IOCTL) 45 : # include <sys/ioctl.h> 46 : #endif 47 : 48 : #include <stdio.h> 49 : #include <errno.h> 50 : 51 : #if !defined (errno) 52 : extern int errno; 53 : #endif /* !errno */ 54 : 55 : #if !defined (O_NDELAY) && defined (O_NONBLOCK) 56 : # define O_NDELAY O_NONBLOCK /* Posix style */ 57 : #endif 58 : 59 : /* Return >= 1 if select/FIONREAD indicates data available for reading on 60 : file descriptor FD; 0 if no data available. Return -1 on error. */ 61 : int 62 0 : input_avail (fd) 63 : int fd; 64 : { 65 0 : int result, chars_avail; 66 : #if defined(HAVE_SELECT) 67 0 : fd_set readfds, exceptfds; 68 0 : struct timeval timeout; 69 : #endif 70 : 71 0 : if (fd < 0) 72 : return -1; 73 : 74 0 : chars_avail = 0; 75 : 76 : #if defined (HAVE_SELECT) 77 0 : FD_ZERO (&readfds); 78 0 : FD_ZERO (&exceptfds); 79 0 : FD_SET (fd, &readfds); 80 0 : FD_SET (fd, &exceptfds); 81 0 : timeout.tv_sec = 0; 82 0 : timeout.tv_usec = 0; 83 0 : result = select (fd + 1, &readfds, (fd_set *)NULL, &exceptfds, &timeout); 84 0 : return ((result <= 0) ? 0 : 1); 85 : 86 : #endif 87 : 88 : result = -1; 89 : #if defined (FIONREAD) 90 : errno = 0; 91 : result = ioctl (fd, FIONREAD, &chars_avail); 92 : if (result == -1 && errno == EIO) 93 : return -1; 94 : return (chars_avail); 95 : #endif 96 : 97 : return 0; 98 : }