From 5214b1538d1015bb530f225d03c3bb94846dde2f Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 23 Nov 2011 16:25:25 -0800 Subject: [PATCH] Set guac_error in guac_select() --- libguac/include/guacio.h | 6 ++++++ libguac/src/guacio.c | 23 +++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index 49055ba7..6b5ac161 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -216,6 +216,12 @@ ssize_t guac_flush(GUACIO* io); * Waits for input to be available on the given GUACIO object until the * specified timeout elapses. * + * If an error occurs while waiting, a negative value is returned, and + * guac_error is set appropriately. + * + * If a timeout occurs while waiting, zero value is returned, and + * guac_error is set to GUAC_STATUS_INPUT_TIMEOUT. + * * @param io The GUACIO object to wait for. * @param usec_timeout The maximum number of microseconds to wait for data, or * -1 to potentially wait forever. diff --git a/libguac/src/guacio.c b/libguac/src/guacio.c index da0b9e00..9ed2ba33 100644 --- a/libguac/src/guacio.c +++ b/libguac/src/guacio.c @@ -299,17 +299,28 @@ int guac_select(GUACIO* io, int usec_timeout) { fd_set fds; struct timeval timeout; + int retval; + /* No timeout if usec_timeout is negative */ if (usec_timeout < 0) - return select(io->fd + 1, &fds, NULL, NULL, NULL); + retval = select(io->fd + 1, &fds, NULL, NULL, NULL); - timeout.tv_sec = usec_timeout/1000000; - timeout.tv_usec = usec_timeout%1000000; + /* Handle timeout if specified */ + else { + timeout.tv_sec = usec_timeout/1000000; + timeout.tv_usec = usec_timeout%1000000; - FD_ZERO(&fds); - FD_SET(io->fd, &fds); + FD_ZERO(&fds); + FD_SET(io->fd, &fds); - return select(io->fd + 1, &fds, NULL, NULL, &timeout); + retval = select(io->fd + 1, &fds, NULL, NULL, &timeout); + } + + /* Properly set guac_error */ + if (retval < 0) guac_error = GUAC_STATUS_INPUT_ERROR; + if (retval == 0) guac_error = GUAC_STATUS_INPUT_TIMEOUT; + + return retval; }