--- a/ExternalStream.st Mon Nov 21 12:00:11 2016 +0100
+++ b/ExternalStream.st Tue Nov 22 12:09:33 2016 +0100
@@ -404,16 +404,17 @@
} \
CLEAR_ERRNO; \
{ \
- int res = 0; \
- if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res), 1)) \
- || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
- || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0), 1))) { \
+ int res = -1, ok = 0; \
+ if ((handleType == @symbol(socketFilePointer) && ((ok = ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res) == 0), 1)) \
+ || (handleType == @symbol(socketHandle) && ((ok = ioctlsocket((SOCKET)(f), FIONREAD, &res) == 0), 1)) \
+ || (handleType == @symbol(pipeFilePointer) && ((ok = PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)), 1))) { \
+ if (!ok) { (ret) = -1; break }; \
if (res > 0) { \
if (res > ((cnt)-__offs)) \
res = (cnt)-__offs; \
READ((ret), f, (buf)+__offs, res, handleType); \
} else { \
- (ret) = 0; \
+ (ret) = res; \
break; \
} \
} else { \
@@ -421,7 +422,7 @@
} \
} \
if ((ret) <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) \
+ if ((ret) < 0 && __threadErrno == EINTR)\
continue; \
break; \
} \
@@ -477,7 +478,7 @@
l = (cnt)-__offs; \
if ( l > IO_BUFFER_SIZE) \
l = IO_BUFFER_SIZE; \
- READ((ret),f, __buf, l, handleType); \
+ READ((ret),f, __buf, l, handleType); \
if ((ret) <= 0) { \
if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
@@ -487,7 +488,7 @@
} \
if ((ret) > 0 ) { \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
memcpy((buf)+__ooffs+__offs,__buf,(ret)); \
__offs += (ret); \
} else { \
@@ -515,7 +516,7 @@
if (__threadErrno == EINTR) { \
clearerr(f); \
/* refetch */ \
- buf = (char *)(obj);\
+ buf = (char *)(obj); \
continue; \
} \
} else { \
@@ -540,26 +541,26 @@
continue; \
} \
{ \
- int res = 0; \
+ int res = -1, ok = 0; \
int l = (cnt)-__offs; \
CLEAR_ERRNO; \
- if (l > IO_BUFFER_SIZE) \
- l = IO_BUFFER_SIZE; \
- if ((handleType == @symbol(socketFilePointer) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)), FIONREAD, &res), 1)) \
- || (handleType == @symbol(socketHandle) && (ioctlsocket((SOCKET)(f), FIONREAD, &res), 1)) \
- || (handleType == @symbol(pipeFilePointer) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0,0,0,&res,0), 1))) { \
- if (res > 0) { \
- if (res > l) res = l; \
+ if (l > IO_BUFFER_SIZE) l = IO_BUFFER_SIZE; \
+ if ((handleType == @symbol(socketFilePointer) && ((ok = ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res) == 0), 1)) \
+ || (handleType == @symbol(socketHandle) && ((ok = ioctlsocket((SOCKET)(f), FIONREAD, &res) == 0), 1)) \
+ || (handleType == @symbol(pipeFilePointer) && ((ok = PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)), 1))) { \
+ if (!ok) { (ret) = -1; break }; \
+ if (res > 0) { \
+ if (res > l) res = l; \
READ((ret), f, __buf, res, handleType); \
} else { \
- (ret) = 0; \
+ (ret) = res; \
break; \
} \
} else { \
READ((ret), f, __buf, l, handleType); \
} \
if ((ret) <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) \
+ if ((ret) < 0 && __threadErrno == EINTR) \
continue; \
break; \
} \
@@ -585,7 +586,7 @@
if ((ret) >= 0) { \
(ret) = 1; \
} else if (ferror(f)) { \
- if (__threadErrno == EINTR) { \
+ if (__threadErrno == EINTR) { \
clearerr(f); \
continue; \
} \
@@ -596,8 +597,8 @@
} else { \
for (;;) { \
CLEAR_ERRNO; \
- WRITE(ret,f, buf, 1, handleType); \
- if ((ret) >= 0 || __threadErrno != EINTR) \
+ WRITE(ret,f, buf, 1, handleType); \
+ if ((ret) >= 0 || __threadErrno != EINTR) \
break; \
} \
}
@@ -611,7 +612,7 @@
(ret) = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
if ((ret) <= 0) { \
if (ferror(f)) { \
- if (__threadErrno == EINTR) { \
+ if (__threadErrno == EINTR) { \
clearerr(f); \
continue; \
} \
@@ -628,9 +629,9 @@
int __offs = 0; \
while (__offs < (cnt)) { \
CLEAR_ERRNO; \
- WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
- if (ret <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) { \
+ WRITE((ret),f, (buf)+__offs, (cnt)-__offs, handleType); \
+ if ((ret) <= 0) { \
+ if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
} \
break; \
@@ -656,7 +657,7 @@
if (ferror(f)) { \
if (__threadErrno == EINTR) { \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
clearerr(f); \
continue; \
} \
@@ -678,11 +679,11 @@
if ( l > IO_BUFFER_SIZE) \
l = IO_BUFFER_SIZE; \
/* refetch */ \
- buf = (char *)(obj); \
+ buf = (char *)(obj); \
memcpy(__buf,(buf)+__ooffs+__offs,l); \
- WRITE(ret,f, __buf, l, handleType); \
- if (ret <= 0) { \
- if (ret < 0 && __threadErrno == EINTR) { \
+ WRITE(ret,f, __buf, l, handleType); \
+ if ((ret) <= 0) { \
+ if ((ret) < 0 && __threadErrno == EINTR) { \
continue; \
} \
break; \