From 64bc3c03d24a413af2033bd93104480990756344 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 12 May 2011 20:27:10 -0400 Subject: [PATCH] fixing EOF case when reading arrays. closes #17. --- j/io.j | 7 ++++--- src/support/ios.c | 33 ++++----------------------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/j/io.j b/j/io.j index e625880375625..5fb97aee6f778 100644 --- a/j/io.j +++ b/j/io.j @@ -164,9 +164,10 @@ end function read{T}(s::IOStream, a::Array{T}) if isa(T,BitsKind) nb = numel(a)*sizeof(T) - ccall(:ios_readall, Ulong, - (Ptr{Void}, Ptr{Void}, Ulong), s.ios, a, ulong(nb)) - # TODO: detect eof + if ccall(:ios_readall, Ulong, + (Ptr{Void}, Ptr{Void}, Ulong), s.ios, a, ulong(nb)) < nb + throw(EOFError()) + end a else invoke(read, (Any, Array), s, a) diff --git a/src/support/ios.c b/src/support/ios.c index 542a7c7d4f2c8..2d438c7b53da8 100644 --- a/src/support/ios.c +++ b/src/support/ios.c @@ -61,22 +61,6 @@ static int _fd_available(long fd) } */ -// poll for read, unless forwrite!=0 -static void _fd_poll(long fd, int forwrite) -{ -#ifndef WIN32 - fd_set set; - - FD_ZERO(&set); - FD_SET(fd, &set); - if (forwrite) - select(fd+1, NULL, &set, NULL, NULL); - else - select(fd+1, &set, NULL, NULL, NULL); -#else -#endif -} - static int _enonfatal(int err) { return (err == EAGAIN || err == EINPROGRESS || err == EINTR || @@ -117,10 +101,8 @@ static int _os_read_all(long fd, void *buf, size_t n, size_t *nread) n -= got; *nread += got; buf += got; - if (err) + if (err || got==0) return err; - if (got == 0) - _fd_poll(fd, 0); } return 0; } @@ -157,8 +139,6 @@ int _os_write_all(long fd, void *buf, size_t n, size_t *nwritten) buf += wrote; if (err) return err; - if (wrote == 0) - _fd_poll(fd, 1); } return 0; } @@ -283,7 +263,7 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all) else result = _os_read(s->fd, dest, n, &got); tot += got; - if (got < n) + if (got == 0) s->_eof = 1; return tot; } @@ -294,13 +274,8 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all) return tot; } if (got == 0) { - if (all) { - _fd_poll(s->fd, 0); - } - else { - s->_eof = 1; - return tot; - } + s->_eof = 1; + return tot; } s->size = got; }