summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2012-04-26 19:42:22 +0000
committerArthur de Jong <arthur@arthurdejong.org>2012-04-26 19:42:22 +0000
commit9ee0a776cab9a54e3a55525be79bb7c50b215841 (patch)
tree4a62d26e97d8cd0a22a706bff012220850c4de45 /common
parent05c17536eaef591f6c36e1a89f8332faa6635a34 (diff)
split the functionality to read everything from the stream into a separate function and don't assume we use non-blocking IO (fix r1637)
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1659 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common')
-rw-r--r--common/tio.c33
-rw-r--r--common/tio.h7
2 files changed, 28 insertions, 12 deletions
diff --git a/common/tio.c b/common/tio.c
index 67caed8..b13ba99 100644
--- a/common/tio.c
+++ b/common/tio.c
@@ -317,18 +317,19 @@ int tio_read(TFILE *fp, void *buf, size_t count)
}
}
-/* Read and discard the specified number of bytes from the stream.
- If count is 0 reads and discards any data that can be read and empties
- the read buffer. */
+/* Read and discard the specified number of bytes from the stream. */
int tio_skip(TFILE *fp, size_t count)
{
+ return tio_read(fp,NULL,count);
+}
+
+/* Read all available data from the stream and empty the read buffer. */
+int tio_skipall(TFILE *fp)
+{
+ struct timeval tv;
+ fd_set fdset;
int rv;
size_t len;
- /* for simple cases just read */
- if (count>0)
- {
- return tio_read(fp,NULL,count);
- }
/* clear the read buffer */
fp->readbuffer.start=0;
fp->readbuffer.len=0;
@@ -341,8 +342,22 @@ int tio_skip(TFILE *fp, size_t count)
#endif /* SSIZE_MAX */
while (1)
{
+ /* prepare our file descriptor set */
+ FD_ZERO(&fdset);
+ FD_SET(fp->fd,&fdset);
+ /* prepare the time to wait */
+ tv.tv_sec=0;
+ tv.tv_usec=0;
+ /* see if any data is available */
+ rv=select(FD_SETSIZE,&fdset,NULL,NULL,&tv);
+ if (rv==0)
+ return 0; /* no file descriptor ready */
+ if ((rv<0)&&((errno==EINTR)||(errno==EAGAIN)))
+ continue; /* interrupted, try again */
+ if (rv<0)
+ return -1; /* something went wrong */
+ /* read data from the stream */
rv=read(fp->fd,fp->readbuffer.buffer,len);
- /* check for errors */
if (rv==0)
return 0; /* end-of-file */
if ((rv<0)&&(errno==EWOULDBLOCK))
diff --git a/common/tio.h b/common/tio.h
index 832367b..59f26df 100644
--- a/common/tio.h
+++ b/common/tio.h
@@ -56,11 +56,12 @@ TFILE *tio_fdopen(int fd,struct timeval *readtimeout,struct timeval *writetimeou
/* Read the specified number of bytes from the stream. */
int tio_read(TFILE *fp,void *buf,size_t count);
-/* Read and discard the specified number of bytes from the stream.
- If count is 0 reads and discards any data that can be read and empties
- the read buffer. */
+/* Read and discard the specified number of bytes from the stream. */
int tio_skip(TFILE *fp,size_t count);
+/* Read all available data from the stream and empty the read buffer. */
+int tio_skipall(TFILE *fp);
+
/* Write the specified buffer to the stream. */
int tio_write(TFILE *fp,const void *buf,size_t count);