| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* zread - read data from file descriptor into buffer with retries */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /* Copyright (C) 1999-2002 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <config.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined (errno)
 | 
					
						
							|  |  |  | extern int errno; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef SEEK_CUR
 | 
					
						
							|  |  |  | #  define SEEK_CUR 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | extern void check_signals_and_traps (void); | 
					
						
							|  |  |  | extern int signal_is_trapped (int); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | /* Read LEN bytes from FD into BUF.  Retry the read on EINTR.  Any other
 | 
					
						
							|  |  |  |    error causes the loop to break. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | ssize_t | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | zread (fd, buf, len) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  |      char *buf; | 
					
						
							|  |  |  |      size_t len; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   ssize_t r; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  | #if defined (HAVE_SIGINTERRUPT)
 | 
					
						
							|  |  |  |   if (signal_is_trapped (SIGCHLD)) | 
					
						
							|  |  |  |     siginterrupt (SIGCHLD, 1); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   while ((r = read (fd, buf, len)) < 0 && errno == EINTR) | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |     check_signals_and_traps ();	/* XXX - should it be check_signals()? */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0 
 | 
					
						
							|  |  |  | #if defined (HAVE_SIGINTERRUPT)
 | 
					
						
							|  |  |  |   siginterrupt (SIGCHLD, 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Read LEN bytes from FD into BUF.  Retry the read on EINTR, up to three
 | 
					
						
							|  |  |  |    interrupts.  Any other error causes the loop to break. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef NUM_INTR
 | 
					
						
							|  |  |  | #  undef NUM_INTR
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #define NUM_INTR 3
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | ssize_t | 
					
						
							| 
									
										
										
										
											2009-02-19 22:21:29 +00:00
										 |  |  | zreadretry (fd, buf, len) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |      int fd; | 
					
						
							|  |  |  |      char *buf; | 
					
						
							|  |  |  |      size_t len; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   ssize_t r; | 
					
						
							|  |  |  |   int nintr; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (nintr = 0; ; ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       r = read (fd, buf, len); | 
					
						
							|  |  |  |       if (r >= 0) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 	return r; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |       if (r == -1 && errno == EINTR) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-19 22:21:29 +00:00
										 |  |  | 	  if (++nintr >= NUM_INTR) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 	    return -1; | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       return r; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-19 22:21:29 +00:00
										 |  |  | /* Call read(2) and allow it to be interrupted.  Just a stub for now. */ | 
					
						
							|  |  |  | ssize_t | 
					
						
							|  |  |  | zreadintr (fd, buf, len) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  |      char *buf; | 
					
						
							|  |  |  |      size_t len; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return (read (fd, buf, len)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | /* Read one character from FD and return it in CP.  Return values are as
 | 
					
						
							|  |  |  |    in read(2).  This does some local buffering to avoid many one-character | 
					
						
							|  |  |  |    calls to read(2), like those the `read' builtin performs. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static char lbuf[128]; | 
					
						
							|  |  |  | static size_t lind, lused; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | ssize_t | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | zreadc (fd, cp) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  |      char *cp; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   ssize_t nr; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (lind == lused || lused == 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       nr = zread (fd, lbuf, sizeof (lbuf)); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |       lind = 0; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       if (nr <= 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  lused = 0; | 
					
						
							|  |  |  | 	  return nr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       lused = nr; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   if (cp) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     *cp = lbuf[lind++]; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-19 22:21:29 +00:00
										 |  |  | /* Don't mix calls to zreadc and zreadcintr in the same function, since they
 | 
					
						
							|  |  |  |    use the same local buffer. */ | 
					
						
							|  |  |  | ssize_t | 
					
						
							|  |  |  | zreadcintr (fd, cp) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  |      char *cp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ssize_t nr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (lind == lused || lused == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       nr = zreadintr (fd, lbuf, sizeof (lbuf)); | 
					
						
							|  |  |  |       lind = 0; | 
					
						
							|  |  |  |       if (nr <= 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  lused = 0; | 
					
						
							|  |  |  | 	  return nr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       lused = nr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   if (cp) | 
					
						
							|  |  |  |     *cp = lbuf[lind++]; | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | /* Like zreadc, but read a specified number of characters at a time.  Used
 | 
					
						
							|  |  |  |    for `read -N'. */ | 
					
						
							|  |  |  | ssize_t | 
					
						
							|  |  |  | zreadn (fd, cp, len) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  |      char *cp; | 
					
						
							|  |  |  |      size_t len; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ssize_t nr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (lind == lused || lused == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (len > sizeof (lbuf)) | 
					
						
							|  |  |  | 	len = sizeof (lbuf); | 
					
						
							|  |  |  |       nr = zread (fd, lbuf, len); | 
					
						
							|  |  |  |       lind = 0; | 
					
						
							|  |  |  |       if (nr <= 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  lused = 0; | 
					
						
							|  |  |  | 	  return nr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       lused = nr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   if (cp) | 
					
						
							|  |  |  |     *cp = lbuf[lind++]; | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | void | 
					
						
							|  |  |  | zreset () | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   lind = lused = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Sync the seek pointer for FD so that the kernel's idea of the last char
 | 
					
						
							|  |  |  |    read is the last char returned by zreadc. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | zsyncfd (fd) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-13 15:11:43 -04:00
										 |  |  |   off_t off, r; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   off = lused - lind; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   r = 0; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   if (off > 0) | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |     r = lseek (fd, -off, SEEK_CUR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-13 15:11:43 -04:00
										 |  |  |   if (r != -1) | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |     lused = lind = 0; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | } |