Handle -EAGAIN in non-blocking reads on Unix, and return 0 from sp_read().

This commit is contained in:
Martin Ling 2013-11-25 10:23:13 +00:00
parent 0ba3e49b45
commit 33d5ff47b5
1 changed files with 11 additions and 2 deletions

View File

@ -60,6 +60,7 @@
struct sp_port {
char *name;
int nonblocking;
#ifdef _WIN32
HANDLE hdl;
#else
@ -578,6 +579,8 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
DEBUG("Opening port %s", port->name);
port->nonblocking = (flags & SP_MODE_NONBLOCK) ? 1 : 0;
#ifdef _WIN32
DWORD desired_access = 0, flags_and_attributes = 0;
char *escaped_port_name;
@ -786,8 +789,14 @@ enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
ssize_t bytes_read;
/* Returns the number of bytes read, or -1 upon failure. */
if ((bytes_read = read(port->fd, buf, count)) < 0)
RETURN_FAIL("read() failed");
if ((bytes_read = read(port->fd, buf, count)) < 0) {
if (port->nonblocking && errno == EAGAIN)
/* Port is opened in nonblocking mode and there are no bytes available. */
bytes_read = 0;
else
/* This is an actual failure. */
RETURN_FAIL("read() failed");
}
RETURN_VALUE("%d", bytes_read);
#endif
}