ExternalStream.st
changeset 15435 fc902ce16af0
parent 15361 ff526d39bb07
child 15452 e2a3b2dc064b
child 18070 d262e3aecaca
equal deleted inserted replaced
15434:f07282a09038 15435:fc902ce16af0
   125 # define NOCRYPT
   125 # define NOCRYPT
   126 //# define NOMCX
   126 //# define NOMCX
   127 # define WIN32_LEAN_AND_MEAN
   127 # define WIN32_LEAN_AND_MEAN
   128 
   128 
   129 # include <windows.h>
   129 # include <windows.h>
   130 # include <winsock2.h> 
   130 # include <winsock2.h>
   131 
   131 
   132 # ifdef __DEF_Array
   132 # ifdef __DEF_Array
   133 #  define Array __DEF_Array
   133 #  define Array __DEF_Array
   134 # endif
   134 # endif
   135 # ifdef __DEF_Number
   135 # ifdef __DEF_Number
   197 #ifdef DEBUGGING
   197 #ifdef DEBUGGING
   198   extern char *__survStartPtr, *__survEndPtr;
   198   extern char *__survStartPtr, *__survEndPtr;
   199 # define DEBUGBUFFER(buf)  \
   199 # define DEBUGBUFFER(buf)  \
   200     if (((char *)(buf) >= __survStartPtr) \
   200     if (((char *)(buf) >= __survStartPtr) \
   201      && ((char *)(buf) < __survEndPtr)) { \
   201      && ((char *)(buf) < __survEndPtr)) { \
   202         __fatal0("read into survivor\n"); \
   202 	__fatal0("read into survivor\n"); \
   203     }
   203     }
   204 
   204 
   205 #else
   205 #else
   206 # define DEBUGBUFFER(buf) /* nothing */
   206 # define DEBUGBUFFER(buf) /* nothing */
   207 #endif
   207 #endif
   224    int _rtl_read();
   224    int _rtl_read();
   225    int _rtl_write();
   225    int _rtl_write();
   226 
   226 
   227 #  define READ(ret,f, cp, n, handleType) \
   227 #  define READ(ret,f, cp, n, handleType) \
   228       { int __res;\
   228       { int __res;\
   229         HANDLE h; \
   229 	HANDLE h; \
   230         h = _get_osfhandle(fileno(f)); \
   230 	h = _get_osfhandle(fileno(f)); \
   231         if ((handleType == @symbol(socketFilePointer)) \
   231 	if ((handleType == @symbol(socketFilePointer)) \
   232         || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   232 	|| ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   233           (ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\
   233 	  (ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\
   234         } else { \
   234 	} else { \
   235           (ret) = __STX_C_NOINT_CALL3("_rtl_read", _rtl_read, fileno(f), (cp), (n));\
   235 	  (ret) = __STX_C_NOINT_CALL3("_rtl_read", _rtl_read, fileno(f), (cp), (n));\
   236         } \
   236 	} \
   237       }
   237       }
   238 
   238 
   239 #  define WRITE(ret,f, cp, n, handleType) \
   239 #  define WRITE(ret,f, cp, n, handleType) \
   240       { int __res;\
   240       { int __res;\
   241         HANDLE h; \
   241 	HANDLE h; \
   242         h = _get_osfhandle(fileno(f)); \
   242 	h = _get_osfhandle(fileno(f)); \
   243         if ((handleType == @symbol(socketFilePointer)) \
   243 	if ((handleType == @symbol(socketFilePointer)) \
   244         || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   244 	|| ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   245           (ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\
   245 	  (ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\
   246         } else { \
   246 	} else { \
   247           (ret) = __STX_C_NOINT_CALL3("_rtl_write", _rtl_write, fileno(f), (cp), (n));\
   247 	  (ret) = __STX_C_NOINT_CALL3("_rtl_write", _rtl_write, fileno(f), (cp), (n));\
   248         } \
   248 	} \
   249       }
   249       }
   250 
   250 
   251 # else /* MSC */
   251 # else /* MSC */
   252 
   252 
   253 #  define READ(ret,f, cp, n, handleType) \
   253 #  define READ(ret,f, cp, n, handleType) \
   254       { int __res;\
   254       { int __res;\
   255         int fd; \
   255 	int fd; \
   256         HANDLE h; \
   256 	HANDLE h; \
   257         fd = fileno(f); \
   257 	fd = fileno(f); \
   258         if ((handleType == @symbol(socketFileDescriptor)) \
   258 	if ((handleType == @symbol(socketFileDescriptor)) \
   259          || ((handleType == nil) && (ioctlsocket((SOCKET)fd,FIONREAD,&__res)==0))) { \
   259 	 || ((handleType == nil) && (ioctlsocket((SOCKET)fd,FIONREAD,&__res)==0))) { \
   260           (ret) = __STX_WSA_NOINT_CALL4("recv", recv, fd, (cp), (n), 0);\
   260 	  (ret) = __STX_WSA_NOINT_CALL4("recv", recv, fd, (cp), (n), 0);\
   261         } else { \
   261 	} else { \
   262           h = _get_osfhandle(fd); \
   262 	  h = _get_osfhandle(fd); \
   263           if ((handleType == @symbol(socketFilePointer)) \
   263 	  if ((handleType == @symbol(socketFilePointer)) \
   264           || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   264 	  || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   265             (ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\
   265 	    (ret) = __STX_WSA_NOINT_CALL4("recv", recv, h, (cp), (n), 0);\
   266           } else { \
   266 	  } else { \
   267             (ret) = __STX_API_NOINT_CALL5("ReadFile", ReadFile, h, (cp), (n), &__res, 0);\
   267 	    (ret) = __STX_API_NOINT_CALL5("ReadFile", ReadFile, h, (cp), (n), &__res, 0);\
   268             if (ret) \
   268 	    if (ret) \
   269               ret = __res; \
   269 	      ret = __res; \
   270           } \
   270 	  } \
   271         } \
   271 	} \
   272       }
   272       }
   273 #  define WRITE(ret,f, cp, n, handleType) \
   273 #  define WRITE(ret,f, cp, n, handleType) \
   274       { int __res;\
   274       { int __res;\
   275         int fd; \
   275 	int fd; \
   276         HANDLE h; \
   276 	HANDLE h; \
   277         fd = fileno(f); \
   277 	fd = fileno(f); \
   278         if ((handleType == @symbol(socketFileDescriptor)) \
   278 	if ((handleType == @symbol(socketFileDescriptor)) \
   279          || ((handleType == nil) && (ioctlsocket((SOCKET)fd,FIONREAD,&__res)==0))) { \
   279 	 || ((handleType == nil) && (ioctlsocket((SOCKET)fd,FIONREAD,&__res)==0))) { \
   280           (ret) = __STX_WSA_NOINT_CALL4("send", send, fd, (cp), (n), 0);\
   280 	  (ret) = __STX_WSA_NOINT_CALL4("send", send, fd, (cp), (n), 0);\
   281         } else {\
   281 	} else {\
   282           h = _get_osfhandle(fd); \
   282 	  h = _get_osfhandle(fd); \
   283           if ((handleType == @symbol(socketFilePointer)) \
   283 	  if ((handleType == @symbol(socketFilePointer)) \
   284           || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   284 	  || ((handleType == nil) && (ioctlsocket((SOCKET)h,FIONREAD,&__res)==0))) { \
   285             (ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\
   285 	    (ret) = __STX_WSA_NOINT_CALL4("send", send, h, (cp), (n), 0);\
   286           } else {\
   286 	  } else {\
   287             (ret) = __STX_API_NOINT_CALL5("WriteFile", WriteFile, h, (cp), (n), &__res, 0);\
   287 	    (ret) = __STX_API_NOINT_CALL5("WriteFile", WriteFile, h, (cp), (n), &__res, 0);\
   288             if (ret) \
   288 	    if (ret) \
   289               ret = __res; \
   289 	      ret = __res; \
   290           } \
   290 	  } \
   291         } \
   291 	} \
   292       }
   292       }
   293 # endif /* MSC */
   293 # endif /* MSC */
   294 
   294 
   295 # define FFLUSH(fp)             fflush(fp)
   295 # define FFLUSH(fp)             fflush(fp)
   296 # undef STDIO_NEEDS_FSEEK
   296 # undef STDIO_NEEDS_FSEEK
   298 # define FILENO(f)              fileno(f)
   298 # define FILENO(f)              fileno(f)
   299 
   299 
   300 # define __READING__(f)                          \
   300 # define __READING__(f)                          \
   301     if ((__INST(didWrite) != false)              \
   301     if ((__INST(didWrite) != false)              \
   302      && (__INST(mode) == @symbol(readwrite))) {  \
   302      && (__INST(mode) == @symbol(readwrite))) {  \
   303         __INST(didWrite) = false;                \
   303 	__INST(didWrite) = false;                \
   304         OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   304 	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   305     }
   305     }
   306 
   306 
   307 # define __WRITING__(f)                          \
   307 # define __WRITING__(f)                          \
   308     if ((__INST(didWrite) != true)               \
   308     if ((__INST(didWrite) != true)               \
   309      && (__INST(mode) == @symbol(readwrite))) {  \
   309      && (__INST(mode) == @symbol(readwrite))) {  \
   310         __INST(didWrite) = true;                 \
   310 	__INST(didWrite) = true;                 \
   311         OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   311 	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   312     }
   312     }
   313 
   313 
   314 # define __UNGETC__(c, f, isBuffered)                   \
   314 # define __UNGETC__(c, f, isBuffered)                   \
   315     if (isBuffered) {                                   \
   315     if (isBuffered) {                                   \
   316         ungetc((c), (f));                               \
   316 	ungetc((c), (f));                               \
   317     } else {                                            \
   317     } else {                                            \
   318       __INST(readAhead) = __mkSmallInteger((c));            \
   318       __INST(readAhead) = __mkSmallInteger((c));            \
   319     }
   319     }
   320 
   320 
   321 # define __READBYTE__(ret, f, buf, isBuffered, handleType)          \
   321 # define __READBYTE__(ret, f, buf, isBuffered, handleType)          \
   322     if (isBuffered) {                                   \
   322     if (isBuffered) {                                   \
   323         for (;;) {                                      \
   323 	for (;;) {                                      \
   324             CLEAR_ERRNO;                                \
   324 	    CLEAR_ERRNO;                                \
   325             (ret) = getc(f);                            \
   325 	    (ret) = getc(f);                            \
   326             if ((ret) >= 0) {                           \
   326 	    if ((ret) >= 0) {                           \
   327                 *(buf) = (ret);                         \
   327 		*(buf) = (ret);                         \
   328                 (ret) = 1;                              \
   328 		(ret) = 1;                              \
   329             } else if (ferror(f)) {                     \
   329 	    } else if (ferror(f)) {                     \
   330                 if (__threadErrno == EINTR) {           \
   330 		if (__threadErrno == EINTR) {           \
   331                     clearerr(f);                        \
   331 		    clearerr(f);                        \
   332                     continue;                           \
   332 		    continue;                           \
   333                 }                                       \
   333 		}                                       \
   334             } else {                                    \
   334 	    } else {                                    \
   335                 (ret) = 0;                              \
   335 		(ret) = 0;                              \
   336             }                                           \
   336 	    }                                           \
   337             break;                                      \
   337 	    break;                                      \
   338         }                                               \
   338 	}                                               \
   339     } else {                                            \
   339     } else {                                            \
   340         OBJ rA = __INST(readAhead);                     \
   340 	OBJ rA = __INST(readAhead);                     \
   341         if (rA != nil) {                                \
   341 	if (rA != nil) {                                \
   342             *(buf) = (char)__intVal(rA);                \
   342 	    *(buf) = (char)__intVal(rA);                \
   343             __INST(readAhead) = nil;                    \
   343 	    __INST(readAhead) = nil;                    \
   344             (ret) = 1;                                  \
   344 	    (ret) = 1;                                  \
   345         } else {                                        \
   345 	} else {                                        \
   346             for (;;) {                                  \
   346 	    for (;;) {                                  \
   347                 CLEAR_ERRNO;                            \
   347 		CLEAR_ERRNO;                            \
   348                 READ(ret,f, buf, 1, handleType);                    \
   348 		READ(ret,f, buf, 1, handleType);                    \
   349                 if ((ret) >= 0 || __threadErrno != EINTR)       \
   349 		if ((ret) >= 0 || __threadErrno != EINTR)       \
   350                     break;                              \
   350 		    break;                              \
   351             }                                           \
   351 	    }                                           \
   352         }                                               \
   352 	}                                               \
   353     }
   353     }
   354 
   354 
   355   /*
   355   /*
   356    * read_bytes into a c-buffer
   356    * read_bytes into a c-buffer
   357    * (which may NOT move)
   357    * (which may NOT move)
   358    */
   358    */
   359 # define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
   359 # define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
   360     (ret) = 0;                                          \
   360     (ret) = 0;                                          \
   361     if (isBuffered) {                                   \
   361     if (isBuffered) {                                   \
   362         int __offs = 0;                                 \
   362 	int __offs = 0;                                 \
   363         while (__offs < (cnt)) {                        \
   363 	while (__offs < (cnt)) {                        \
   364             CLEAR_ERRNO;                                \
   364 	    CLEAR_ERRNO;                                \
   365             (ret) = getc(f);                            \
   365 	    (ret) = getc(f);                            \
   366             if ((ret) < 0) {                            \
   366 	    if ((ret) < 0) {                            \
   367                 if (ferror(f)) {                        \
   367 		if (ferror(f)) {                        \
   368                     if (__threadErrno == EINTR) {               \
   368 		    if (__threadErrno == EINTR) {               \
   369                         clearerr(f);                    \
   369 			clearerr(f);                    \
   370                         continue;                       \
   370 			continue;                       \
   371                     }                                   \
   371 		    }                                   \
   372                 } else {                                \
   372 		} else {                                \
   373                     (ret) = 0;                          \
   373 		    (ret) = 0;                          \
   374                 }                                       \
   374 		}                                       \
   375                 break;                                  \
   375 		break;                                  \
   376             }                                           \
   376 	    }                                           \
   377             (buf)[__offs++] = (ret);                    \
   377 	    (buf)[__offs++] = (ret);                    \
   378         }                                               \
   378 	}                                               \
   379         if (__offs > 0)                                 \
   379 	if (__offs > 0)                                 \
   380             (ret) = __offs;                             \
   380 	    (ret) = __offs;                             \
   381     } else {                                            \
   381     } else {                                            \
   382         int __offs = 0;                                 \
   382 	int __offs = 0;                                 \
   383                                                         \
   383 							\
   384         while (__offs < (cnt)) {                        \
   384 	while (__offs < (cnt)) {                        \
   385             OBJ rA = __INST(readAhead);                 \
   385 	    OBJ rA = __INST(readAhead);                 \
   386             if (rA != nil) {                            \
   386 	    if (rA != nil) {                            \
   387                 (buf)[__offs] = __intVal(rA);           \
   387 		(buf)[__offs] = __intVal(rA);           \
   388                 __INST(readAhead) = nil;                \
   388 		__INST(readAhead) = nil;                \
   389                 (ret) = 1;                              \
   389 		(ret) = 1;                              \
   390             } else {                                    \
   390 	    } else {                                    \
   391                 CLEAR_ERRNO;                            \
   391 		CLEAR_ERRNO;                            \
   392                 READ(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
   392 		READ(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
   393                 if ((ret) <= 0) {                       \
   393 		if ((ret) <= 0) {                       \
   394                     if ((ret) < 0 && __threadErrno == EINTR) {  \
   394 		    if ((ret) < 0 && __threadErrno == EINTR) {  \
   395                         continue;                       \
   395 			continue;                       \
   396                     }                                   \
   396 		    }                                   \
   397                     break;                              \
   397 		    break;                              \
   398                 }                                       \
   398 		}                                       \
   399             }                                           \
   399 	    }                                           \
   400             __offs += (ret);                            \
   400 	    __offs += (ret);                            \
   401         }                                               \
   401 	}                                               \
   402         if (__offs > 0)                                 \
   402 	if (__offs > 0)                                 \
   403             (ret) = __offs;                             \
   403 	    (ret) = __offs;                             \
   404    }
   404    }
   405 
   405 
   406 # define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
   406 # define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
   407   {                                                     \
   407   {                                                     \
   408     int __offs = 0;                                     \
   408     int __offs = 0;                                     \
   409     int oldFlags;                                       \
   409     int oldFlags;                                       \
   410                                                         \
   410 							\
   411     (ret) = 0;                                          \
   411     (ret) = 0;                                          \
   412     if (isBuffered) {                                   \
   412     if (isBuffered) {                                   \
   413         while (__offs < (cnt)) {                        \
   413 	while (__offs < (cnt)) {                        \
   414             CLEAR_ERRNO;                                \
   414 	    CLEAR_ERRNO;                                \
   415             (ret) = getc(f);                            \
   415 	    (ret) = getc(f);                            \
   416             if ((ret) < 0) {                            \
   416 	    if ((ret) < 0) {                            \
   417                 if (ferror(f)) {                        \
   417 		if (ferror(f)) {                        \
   418                     if (__threadErrno == EINTR) {               \
   418 		    if (__threadErrno == EINTR) {               \
   419                         clearerr(f);                    \
   419 			clearerr(f);                    \
   420                         continue;                       \
   420 			continue;                       \
   421                     }                                   \
   421 		    }                                   \
   422                 } else {                                \
   422 		} else {                                \
   423                     (ret) = 0;                          \
   423 		    (ret) = 0;                          \
   424                 }                                       \
   424 		}                                       \
   425                 break;                                  \
   425 		break;                                  \
   426             }                                           \
   426 	    }                                           \
   427             (buf)[__offs++] = (ret);                    \
   427 	    (buf)[__offs++] = (ret);                    \
   428         }                                               \
   428 	}                                               \
   429         if (__offs > 0)                                 \
   429 	if (__offs > 0)                                 \
   430             (ret) = __offs;                             \
   430 	    (ret) = __offs;                             \
   431     } else {                                            \
   431     } else {                                            \
   432         while (__offs < (cnt)) {                        \
   432 	while (__offs < (cnt)) {                        \
   433             OBJ rA = __INST(readAhead);                 \
   433 	    OBJ rA = __INST(readAhead);                 \
   434             if (rA != nil) {                            \
   434 	    if (rA != nil) {                            \
   435                 (buf)[__offs] = __intVal(rA);           \
   435 		(buf)[__offs] = __intVal(rA);           \
   436                 __INST(readAhead) = nil;                \
   436 		__INST(readAhead) = nil;                \
   437                 (ret) = 1;                              \
   437 		(ret) = 1;                              \
   438                 __offs += (ret);                        \
   438 		__offs += (ret);                        \
   439                 continue;                               \
   439 		continue;                               \
   440             }                                           \
   440 	    }                                           \
   441             CLEAR_ERRNO;                                \
   441 	    CLEAR_ERRNO;                                \
   442             {                                           \
   442 	    {                                           \
   443               int res;                                  \
   443 	      int res;                                  \
   444               if ((handleType == @symbol(socketFilePointer)) \
   444 	      if ((handleType == @symbol(socketFilePointer)) \
   445                || ((handleType == nil) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0))) { \
   445 	       || ((handleType == nil) && (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0))) { \
   446                   if (res > 0) {                        \
   446 		  if (res > 0) {                        \
   447                       if (res > ((cnt)-__offs))         \
   447 		      if (res > ((cnt)-__offs))         \
   448                         res = (cnt)-__offs;             \
   448 			res = (cnt)-__offs;             \
   449                       READ(ret,f, (buf)+__offs, res, handleType);   \
   449 		      READ(ret,f, (buf)+__offs, res, handleType);   \
   450                   } else {                              \
   450 		  } else {                              \
   451                       (ret) = 0;                        \
   451 		      (ret) = 0;                        \
   452                   }                                     \
   452 		  }                                     \
   453               } else if ((handleType == @symbol(pipeFilePointer)) \
   453 	      } else if ((handleType == @symbol(pipeFilePointer)) \
   454                          || ((handleType == nil) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)))) { \
   454 			 || ((handleType == nil) && (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)))) { \
   455                   if (res > 0) {                        \
   455 		  if (res > 0) {                        \
   456                       if (res > ((cnt)-__offs))         \
   456 		      if (res > ((cnt)-__offs))         \
   457                         res = (cnt)-__offs;             \
   457 			res = (cnt)-__offs;             \
   458                       READ(ret,f, (buf)+__offs, res, handleType);   \
   458 		      READ(ret,f, (buf)+__offs, res, handleType);   \
   459                   } else                                \
   459 		  } else                                \
   460                       ret = 0;                          \
   460 		      ret = 0;                          \
   461               } else {                                  \
   461 	      } else {                                  \
   462                   READ(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
   462 		  READ(ret,f, (buf)+__offs, (cnt)-__offs, handleType); \
   463               }                                         \
   463 	      }                                         \
   464             }                                           \
   464 	    }                                           \
   465             if ((ret) < 0) {                            \
   465 	    if ((ret) < 0) {                            \
   466                 if (__threadErrno == EINTR) {                   \
   466 		if (__threadErrno == EINTR) {                   \
   467                     continue;                           \
   467 		    continue;                           \
   468                 }                                       \
   468 		}                                       \
   469                 break;                                  \
   469 		break;                                  \
   470             }                                           \
   470 	    }                                           \
   471             __offs += (ret);                            \
   471 	    __offs += (ret);                            \
   472             break;                                      \
   472 	    break;                                      \
   473         }                                               \
   473 	}                                               \
   474         if (__offs > 0)                                 \
   474 	if (__offs > 0)                                 \
   475             (ret) = __offs;                             \
   475 	    (ret) = __offs;                             \
   476     }                                                   \
   476     }                                                   \
   477   }
   477   }
   478 
   478 
   479 # define IO_BUFFER_SIZE        (8*1024)
   479 # define IO_BUFFER_SIZE        (8*1024)
   480 
   480 
   481 # define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
   481 # define __READBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
   482   {                                                     \
   482   {                                                     \
   483     int __ooffs = obj_offs;                             \
   483     int __ooffs = obj_offs;                             \
   484     int __offs = 0;                                     \
   484     int __offs = 0;                                     \
   485     char *buf = (char *)(obj);                          \
   485     char *buf = (char *)(obj);                          \
   486                                                         \
   486 							\
   487     (ret) = 0;                                          \
   487     (ret) = 0;                                          \
   488     if (isBuffered) {                                   \
   488     if (isBuffered) {                                   \
   489         while (__offs < (cnt)) {                        \
   489 	while (__offs < (cnt)) {                        \
   490             CLEAR_ERRNO;                                \
   490 	    CLEAR_ERRNO;                                \
   491             (ret) = getc(f);                            \
   491 	    (ret) = getc(f);                            \
   492             if ((ret) < 0) {                            \
   492 	    if ((ret) < 0) {                            \
   493                 if (ferror(f)) {                        \
   493 		if (ferror(f)) {                        \
   494                     if (__threadErrno == EINTR) {       \
   494 		    if (__threadErrno == EINTR) {       \
   495                         clearerr(f);                    \
   495 			clearerr(f);                    \
   496                         /* refetch */                   \
   496 			/* refetch */                   \
   497                         buf = (char *)(obj);   \
   497 			buf = (char *)(obj);   \
   498                         continue;                       \
   498 			continue;                       \
   499                     }                                   \
   499 		    }                                   \
   500                 } else {                                \
   500 		} else {                                \
   501                     (ret) = 0;                          \
   501 		    (ret) = 0;                          \
   502                 }                                       \
   502 		}                                       \
   503                 break;                                  \
   503 		break;                                  \
   504             }                                           \
   504 	    }                                           \
   505             (buf)[__ooffs+__offs] = (ret);              \
   505 	    (buf)[__ooffs+__offs] = (ret);              \
   506             __offs++;                                   \
   506 	    __offs++;                                   \
   507         }                                               \
   507 	}                                               \
   508         if (__offs > 0)                                 \
   508 	if (__offs > 0)                                 \
   509             (ret) = __offs;                             \
   509 	    (ret) = __offs;                             \
   510     } else {                                            \
   510     } else {                                            \
   511         while (__offs < (cnt)) {                        \
   511 	while (__offs < (cnt)) {                        \
   512             char __buf[IO_BUFFER_SIZE];                 \
   512 	    char __buf[IO_BUFFER_SIZE];                 \
   513             OBJ rA = __INST(readAhead);                 \
   513 	    OBJ rA = __INST(readAhead);                 \
   514             if (rA != nil) {                            \
   514 	    if (rA != nil) {                            \
   515                 (buf)[__ooffs+__offs] = __intVal(rA);   \
   515 		(buf)[__ooffs+__offs] = __intVal(rA);   \
   516                 __INST(readAhead) = nil;                \
   516 		__INST(readAhead) = nil;                \
   517                 (ret) = 1;                              \
   517 		(ret) = 1;                              \
   518             } else {                                    \
   518 	    } else {                                    \
   519                 int l;                                  \
   519 		int l;                                  \
   520                 CLEAR_ERRNO;                            \
   520 		CLEAR_ERRNO;                            \
   521                 l = (cnt)-__offs;                       \
   521 		l = (cnt)-__offs;                       \
   522                 if ( l > IO_BUFFER_SIZE)                \
   522 		if ( l > IO_BUFFER_SIZE)                \
   523                   l = IO_BUFFER_SIZE;                   \
   523 		  l = IO_BUFFER_SIZE;                   \
   524                 READ(ret,f, __buf, l, handleType);                  \
   524 		READ(ret,f, __buf, l, handleType);                  \
   525                 if ((ret) <= 0) {                       \
   525 		if ((ret) <= 0) {                       \
   526                     if ((ret) < 0 && __threadErrno == EINTR) {  \
   526 		    if ((ret) < 0 && __threadErrno == EINTR) {  \
   527                         continue;                       \
   527 			continue;                       \
   528                     }                                   \
   528 		    }                                   \
   529                     break;                              \
   529 		    break;                              \
   530                 }                                       \
   530 		}                                       \
   531             }                                           \
   531 	    }                                           \
   532             if( (ret) > 0 ) {                               \
   532 	    if( (ret) > 0 ) {                               \
   533                 /* refetch */                               \
   533 		/* refetch */                               \
   534                 buf = (char *)(obj);               \
   534 		buf = (char *)(obj);               \
   535                 memcpy((buf)+__ooffs+__offs,__buf,(ret));   \
   535 		memcpy((buf)+__ooffs+__offs,__buf,(ret));   \
   536                 __offs += (ret);                            \
   536 		__offs += (ret);                            \
   537             } else {                                        \
   537 	    } else {                                        \
   538                 (ret) = 0;                                  \
   538 		(ret) = 0;                                  \
   539             }                                               \
   539 	    }                                               \
   540         }                                               \
   540 	}                                               \
   541         if (__offs > 0)                                 \
   541 	if (__offs > 0)                                 \
   542             (ret) = __offs;                             \
   542 	    (ret) = __offs;                             \
   543     }                                                   \
   543     }                                                   \
   544   }
   544   }
   545 
   545 
   546 # define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)\
   546 # define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)\
   547   {                                                  \
   547   {                                                  \
   548     int __ooffs = obj_offs;                          \
   548     int __ooffs = obj_offs;                          \
   549     int __offs = 0;                                  \
   549     int __offs = 0;                                  \
   550     char *buf = (char *)(obj);                       \
   550     char *buf = (char *)(obj);                       \
   551                                                      \
   551 						     \
   552     (ret) = 0;                                       \
   552     (ret) = 0;                                       \
   553     if (isBuffered) {                                \
   553     if (isBuffered) {                                \
   554         while (__offs < (cnt)) {                     \
   554 	while (__offs < (cnt)) {                     \
   555             CLEAR_ERRNO;                             \
   555 	    CLEAR_ERRNO;                             \
   556             (ret) = getc(f);                         \
   556 	    (ret) = getc(f);                         \
   557             if ((ret) < 0) {                         \
   557 	    if ((ret) < 0) {                         \
   558                 if (ferror(f)) {                     \
   558 		if (ferror(f)) {                     \
   559                     if (__threadErrno == EINTR) {    \
   559 		    if (__threadErrno == EINTR) {    \
   560                         clearerr(f);                 \
   560 			clearerr(f);                 \
   561                         /* refetch */                \
   561 			/* refetch */                \
   562                         buf = (char *)(obj);\
   562 			buf = (char *)(obj);\
   563                         continue;                    \
   563 			continue;                    \
   564                     }                                \
   564 		    }                                \
   565                 } else {                             \
   565 		} else {                             \
   566                     (ret) = 0;                       \
   566 		    (ret) = 0;                       \
   567                 }                                    \
   567 		}                                    \
   568                 break;                               \
   568 		break;                               \
   569             }                                        \
   569 	    }                                        \
   570             (buf)[__ooffs+__offs] = (ret);           \
   570 	    (buf)[__ooffs+__offs] = (ret);           \
   571             __offs++;                                \
   571 	    __offs++;                                \
   572         }                                            \
   572 	}                                            \
   573         if (__offs > 0)                              \
   573 	if (__offs > 0)                              \
   574             (ret) = __offs;                          \
   574 	    (ret) = __offs;                          \
   575     } else {                                         \
   575     } else {                                         \
   576         while (__offs < (cnt)) {                     \
   576 	while (__offs < (cnt)) {                     \
   577             char __buf[IO_BUFFER_SIZE];              \
   577 	    char __buf[IO_BUFFER_SIZE];              \
   578             OBJ rA = __INST(readAhead);              \
   578 	    OBJ rA = __INST(readAhead);              \
   579             if (rA != nil) {                         \
   579 	    if (rA != nil) {                         \
   580                 (buf)[__ooffs+__offs] = __intVal(rA);\
   580 		(buf)[__ooffs+__offs] = __intVal(rA);\
   581                 __INST(readAhead) = nil;             \
   581 		__INST(readAhead) = nil;             \
   582                 (ret) = 1;                           \
   582 		(ret) = 1;                           \
   583                 __offs += (ret);                     \
   583 		__offs += (ret);                     \
   584                 continue;                            \
   584 		continue;                            \
   585             }                                        \
   585 	    }                                        \
   586             {                                        \
   586 	    {                                        \
   587                 int l;                               \
   587 		int l;                               \
   588                 int res;                             \
   588 		int res;                             \
   589                 CLEAR_ERRNO;                         \
   589 		CLEAR_ERRNO;                         \
   590                 l = (cnt)-__offs;                    \
   590 		l = (cnt)-__offs;                    \
   591                 if ( l > IO_BUFFER_SIZE)             \
   591 		if ( l > IO_BUFFER_SIZE)             \
   592                     l = IO_BUFFER_SIZE;              \
   592 		    l = IO_BUFFER_SIZE;              \
   593                 if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0) { \
   593 		if (ioctlsocket((SOCKET)_get_osfhandle(fileno(f)),FIONREAD,&res)==0) { \
   594                     if (res > 0) {                   \
   594 		    if (res > 0) {                   \
   595                         if (res > l)                 \
   595 			if (res > l)                 \
   596                             res = l;                 \
   596 			    res = l;                 \
   597                         READ(ret,f, __buf, res, handleType);     \
   597 			READ(ret,f, __buf, res, handleType);     \
   598                     } else {                         \
   598 		    } else {                         \
   599                         (ret) = 0;                   \
   599 			(ret) = 0;                   \
   600                     }                                \
   600 		    }                                \
   601                 } else if (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)) { \
   601 		} else if (PeekNamedPipe((HANDLE)_get_osfhandle(fileno(f)),0, 0,0,&res,0)) { \
   602                     if (res > 0) {                   \
   602 		    if (res > 0) {                   \
   603                         if (res > l)                 \
   603 			if (res > l)                 \
   604                             res = l;                 \
   604 			    res = l;                 \
   605                         READ(ret,f, __buf, res, handleType);     \
   605 			READ(ret,f, __buf, res, handleType);     \
   606                     } else                           \
   606 		    } else                           \
   607                         (ret) = 0;                   \
   607 			(ret) = 0;                   \
   608                 } else {                             \
   608 		} else {                             \
   609                     READ(ret,f, __buf, l, handleType);           \
   609 		    READ(ret,f, __buf, l, handleType);           \
   610                 }                                    \
   610 		}                                    \
   611                 if ((ret) < 0 && __threadErrno == EINTR) {   \
   611 		if ((ret) < 0 && __threadErrno == EINTR) {   \
   612                     continue;                        \
   612 		    continue;                        \
   613                 }                                    \
   613 		}                                    \
   614             }                                        \
   614 	    }                                        \
   615             if( (ret) > 0 ) {                               \
   615 	    if( (ret) > 0 ) {                               \
   616                 /* refetch */                               \
   616 		/* refetch */                               \
   617                 buf = (char *)(obj);               \
   617 		buf = (char *)(obj);               \
   618                 memcpy((buf)+__ooffs+__offs,__buf,(ret));   \
   618 		memcpy((buf)+__ooffs+__offs,__buf,(ret));   \
   619                 __offs += (ret);                            \
   619 		__offs += (ret);                            \
   620             } else {                                        \
   620 	    } else {                                        \
   621                 (ret) = 0;                                  \
   621 		(ret) = 0;                                  \
   622             }                                               \
   622 	    }                                               \
   623             break;                                      \
   623 	    break;                                      \
   624         }                                               \
   624 	}                                               \
   625         if (__offs > 0)                                 \
   625 	if (__offs > 0)                                 \
   626             (ret) = __offs;                             \
   626 	    (ret) = __offs;                             \
   627     }                                                   \
   627     }                                                   \
   628   }
   628   }
   629 
   629 
   630 # define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)         \
   630 # define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)         \
   631     if (isBuffered) {                                   \
   631     if (isBuffered) {                                   \
   632         for (;;) {                                      \
   632 	for (;;) {                                      \
   633             CLEAR_ERRNO;                                \
   633 	    CLEAR_ERRNO;                                \
   634             ret = putc(*(buf), f);                      \
   634 	    ret = putc(*(buf), f);                      \
   635             if ((ret) >= 0) {                           \
   635 	    if ((ret) >= 0) {                           \
   636                 (ret) = 1;                              \
   636 		(ret) = 1;                              \
   637             } else if (ferror(f)) {                     \
   637 	    } else if (ferror(f)) {                     \
   638                 if (__threadErrno == EINTR) {                   \
   638 		if (__threadErrno == EINTR) {                   \
   639                     clearerr(f);                        \
   639 		    clearerr(f);                        \
   640                     continue;                           \
   640 		    continue;                           \
   641                 }                                       \
   641 		}                                       \
   642             } else                                      \
   642 	    } else                                      \
   643                 (ret) = 0;                              \
   643 		(ret) = 0;                              \
   644             break;                                      \
   644 	    break;                                      \
   645         }                                               \
   645 	}                                               \
   646     } else {                                            \
   646     } else {                                            \
   647         for (;;) {                                      \
   647 	for (;;) {                                      \
   648             CLEAR_ERRNO;                                \
   648 	    CLEAR_ERRNO;                                \
   649             WRITE(ret,f, buf, 1, handleType);                       \
   649 	    WRITE(ret,f, buf, 1, handleType);                       \
   650             if ((ret) >= 0 || __threadErrno != EINTR)           \
   650 	    if ((ret) >= 0 || __threadErrno != EINTR)           \
   651                 break;                                  \
   651 		break;                                  \
   652         }                                               \
   652 	}                                               \
   653    }
   653    }
   654 
   654 
   655 # define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
   655 # define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
   656     (ret) = 0;                                          \
   656     (ret) = 0;                                          \
   657     if (isBuffered) {                                   \
   657     if (isBuffered) {                                   \
   658         int __offs = 0;                                 \
   658 	int __offs = 0;                                 \
   659         while (__offs < (cnt)) {                        \
   659 	while (__offs < (cnt)) {                        \
   660             CLEAR_ERRNO;                                \
   660 	    CLEAR_ERRNO;                                \
   661             ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
   661 	    ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
   662             if ((ret) <= 0) {                           \
   662 	    if ((ret) <= 0) {                           \
   663                 if (ferror(f)) {                        \
   663 		if (ferror(f)) {                        \
   664                     if (__threadErrno == EINTR) {               \
   664 		    if (__threadErrno == EINTR) {               \
   665                         clearerr(f);                    \
   665 			clearerr(f);                    \
   666                         continue;                       \
   666 			continue;                       \
   667                     }                                   \
   667 		    }                                   \
   668                     break;                              \
   668 		    break;                              \
   669                 } else {                                \
   669 		} else {                                \
   670                     (ret) = 0;                          \
   670 		    (ret) = 0;                          \
   671                 }                                       \
   671 		}                                       \
   672             }                                           \
   672 	    }                                           \
   673             __offs += (ret);                            \
   673 	    __offs += (ret);                            \
   674         }                                               \
   674 	}                                               \
   675         if (__offs > 0)                                 \
   675 	if (__offs > 0)                                 \
   676             (ret) = __offs;                             \
   676 	    (ret) = __offs;                             \
   677     } else {                                            \
   677     } else {                                            \
   678         int __offs = 0;                                 \
   678 	int __offs = 0;                                 \
   679         while (__offs < (cnt)) {                        \
   679 	while (__offs < (cnt)) {                        \
   680             CLEAR_ERRNO;                                \
   680 	    CLEAR_ERRNO;                                \
   681             WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType);   \
   681 	    WRITE(ret,f, (buf)+__offs, (cnt)-__offs, handleType);   \
   682             if (ret <= 0) {                             \
   682 	    if (ret <= 0) {                             \
   683                 if (ret < 0 && __threadErrno == EINTR) {        \
   683 		if (ret < 0 && __threadErrno == EINTR) {        \
   684                     continue;                           \
   684 		    continue;                           \
   685                 }                                       \
   685 		}                                       \
   686                 break;                                  \
   686 		break;                                  \
   687             }                                           \
   687 	    }                                           \
   688             __offs += (ret);                            \
   688 	    __offs += (ret);                            \
   689         }                                               \
   689 	}                                               \
   690         if (__offs > 0)                                 \
   690 	if (__offs > 0)                                 \
   691             (ret) = __offs;                             \
   691 	    (ret) = __offs;                             \
   692    }
   692    }
   693 
   693 
   694 # define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
   694 # define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType) \
   695   {                                                     \
   695   {                                                     \
   696     int __ooffs = obj_offs;                             \
   696     int __ooffs = obj_offs;                             \
   697     int __offs = 0;                                     \
   697     int __offs = 0;                                     \
   698     char *buf = (char *)(obj);                          \
   698     char *buf = (char *)(obj);                          \
   699                                                         \
   699 							\
   700     (ret) = 0;                                          \
   700     (ret) = 0;                                          \
   701     if (isBuffered) {                                   \
   701     if (isBuffered) {                                   \
   702         while (__offs < (cnt)) {                        \
   702 	while (__offs < (cnt)) {                        \
   703             CLEAR_ERRNO;                                \
   703 	    CLEAR_ERRNO;                                \
   704             ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \
   704 	    ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f); \
   705             if ((ret) <= 0) {                           \
   705 	    if ((ret) <= 0) {                           \
   706                 if (ferror(f)) {                        \
   706 		if (ferror(f)) {                        \
   707                     if (__threadErrno == EINTR) {       \
   707 		    if (__threadErrno == EINTR) {       \
   708                         /* refetch */                   \
   708 			/* refetch */                   \
   709                         buf = (char *)(obj);   \
   709 			buf = (char *)(obj);   \
   710                         clearerr(f);                    \
   710 			clearerr(f);                    \
   711                         continue;                       \
   711 			continue;                       \
   712                     }                                   \
   712 		    }                                   \
   713                     break;                              \
   713 		    break;                              \
   714                 } else {                                \
   714 		} else {                                \
   715                     (ret) = 0;                          \
   715 		    (ret) = 0;                          \
   716                 }                                       \
   716 		}                                       \
   717             }                                           \
   717 	    }                                           \
   718             __offs += (ret);                            \
   718 	    __offs += (ret);                            \
   719         }                                               \
   719 	}                                               \
   720         if (__offs > 0)                                 \
   720 	if (__offs > 0)                                 \
   721             (ret) = __offs;                             \
   721 	    (ret) = __offs;                             \
   722     } else {                                            \
   722     } else {                                            \
   723         while (__offs < (cnt)) {                        \
   723 	while (__offs < (cnt)) {                        \
   724             char __buf[IO_BUFFER_SIZE];                 \
   724 	    char __buf[IO_BUFFER_SIZE];                 \
   725             int l;                                      \
   725 	    int l;                                      \
   726             CLEAR_ERRNO;                                \
   726 	    CLEAR_ERRNO;                                \
   727             l = (cnt)-__offs;                           \
   727 	    l = (cnt)-__offs;                           \
   728             if ( l > IO_BUFFER_SIZE)                    \
   728 	    if ( l > IO_BUFFER_SIZE)                    \
   729               l = IO_BUFFER_SIZE;                       \
   729 	      l = IO_BUFFER_SIZE;                       \
   730             /* refetch */                               \
   730 	    /* refetch */                               \
   731             buf = (char *)(obj);               \
   731 	    buf = (char *)(obj);               \
   732             memcpy(__buf,(buf)+__ooffs+__offs,l);       \
   732 	    memcpy(__buf,(buf)+__ooffs+__offs,l);       \
   733             WRITE(ret,f, __buf, l, handleType);                     \
   733 	    WRITE(ret,f, __buf, l, handleType);                     \
   734             if (ret <= 0) {                             \
   734 	    if (ret <= 0) {                             \
   735                 if (ret < 0 && __threadErrno == EINTR) {        \
   735 		if (ret < 0 && __threadErrno == EINTR) {        \
   736                     continue;                           \
   736 		    continue;                           \
   737                 }                                       \
   737 		}                                       \
   738                 break;                                  \
   738 		break;                                  \
   739             }                                           \
   739 	    }                                           \
   740             __offs += (ret);                            \
   740 	    __offs += (ret);                            \
   741         }                                               \
   741 	}                                               \
   742         if (__offs > 0)                                 \
   742 	if (__offs > 0)                                 \
   743             (ret) = __offs;                             \
   743 	    (ret) = __offs;                             \
   744     }                                                   \
   744     }                                                   \
   745   }
   745   }
   746 
   746 
   747 #else /* no WIN32 */
   747 #else /* no WIN32 */
   748 
   748 
   749 # define __READING__(f)                          \
   749 # define __READING__(f)                          \
   750     if ((__INST(didWrite) != false)              \
   750     if ((__INST(didWrite) != false)              \
   751      && (__INST(mode) == @symbol(readwrite))) {  \
   751      && (__INST(mode) == @symbol(readwrite))) {  \
   752         __INST(didWrite) = false;                \
   752 	__INST(didWrite) = false;                \
   753         OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   753 	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   754     }
   754     }
   755 
   755 
   756 # define __WRITING__(f)                          \
   756 # define __WRITING__(f)                          \
   757     if ((__INST(didWrite) != true)               \
   757     if ((__INST(didWrite) != true)               \
   758      && (__INST(mode) == @symbol(readwrite))) {  \
   758      && (__INST(mode) == @symbol(readwrite))) {  \
   759         __INST(didWrite) = true;                 \
   759 	__INST(didWrite) = true;                 \
   760         OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   760 	OPT_FSEEK(f, 0L, SEEK_CUR); /* needed in stdio */  \
   761     }
   761     }
   762 
   762 
   763 
   763 
   764 # ifdef NO_STDIO
   764 # ifdef NO_STDIO
   765 #  define __UNGETC__(c, f, isBuffered)                  \
   765 #  define __UNGETC__(c, f, isBuffered)                  \
   766     __INST(readAhead) = __mkSmallInteger((c));
   766     __INST(readAhead) = __mkSmallInteger((c));
   767 # else /* use STDIO */
   767 # else /* use STDIO */
   768 #  define __UNGETC__(c, f, isBuffered)                  \
   768 #  define __UNGETC__(c, f, isBuffered)                  \
   769     if (isBuffered) {                                   \
   769     if (isBuffered) {                                   \
   770         ungetc((c), (f));                               \
   770 	ungetc((c), (f));                               \
   771     } else {                                            \
   771     } else {                                            \
   772         __INST(readAhead) = __mkSmallInteger((c));          \
   772 	__INST(readAhead) = __mkSmallInteger((c));          \
   773     }
   773     }
   774 # endif /* use STDIO */
   774 # endif /* use STDIO */
   775 
   775 
   776 # ifdef NO_STDIO
   776 # ifdef NO_STDIO
   777 #  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
   777 #  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
   778     {                                                   \
   778     {                                                   \
   779         OBJ rA = __INST(readAhead);                     \
   779 	OBJ rA = __INST(readAhead);                     \
   780         if (rA != nil) {                                \
   780 	if (rA != nil) {                                \
   781             *(buf) = __intVal(rA);                      \
   781 	    *(buf) = __intVal(rA);                      \
   782             DEBUGBUFFER(buf);                           \
   782 	    DEBUGBUFFER(buf);                           \
   783             __INST(readAhead) = nil;                    \
   783 	    __INST(readAhead) = nil;                    \
   784             (ret) = 1;                                  \
   784 	    (ret) = 1;                                  \
   785         } else {                                        \
   785 	} else {                                        \
   786             for (;;) {                                  \
   786 	    for (;;) {                                  \
   787                 CLEAR_ERRNO;                            \
   787 		CLEAR_ERRNO;                            \
   788                 (ret) = READ(f, buf, 1, handleType);                \
   788 		(ret) = READ(f, buf, 1, handleType);                \
   789                 DEBUGBUFFER(buf);                       \
   789 		DEBUGBUFFER(buf);                       \
   790                 if ((ret) >= 0 || __threadErrno != EINTR)       \
   790 		if ((ret) >= 0 || __threadErrno != EINTR)       \
   791                     break;                              \
   791 		    break;                              \
   792                 __HANDLE_INTERRUPTS__;                  \
   792 		__HANDLE_INTERRUPTS__;                  \
   793             }                                           \
   793 	    }                                           \
   794         }                                               \
   794 	}                                               \
   795     }
   795     }
   796 # else /* use STDIO */
   796 # else /* use STDIO */
   797 #  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
   797 #  define __READBYTE__(ret, f, buf, isBuffered, handleType)         \
   798     if (isBuffered) {                                   \
   798     if (isBuffered) {                                   \
   799         for (;;) {                                      \
   799 	for (;;) {                                      \
   800             CLEAR_ERRNO;                                \
   800 	    CLEAR_ERRNO;                                \
   801             (ret) = getc(f);                            \
   801 	    (ret) = getc(f);                            \
   802             if ((ret) >= 0) {                           \
   802 	    if ((ret) >= 0) {                           \
   803                 DEBUGBUFFER(buf);                       \
   803 		DEBUGBUFFER(buf);                       \
   804                 *(buf) = (ret);                         \
   804 		*(buf) = (ret);                         \
   805                 (ret) = 1;                              \
   805 		(ret) = 1;                              \
   806             } else if (ferror(f)) {                     \
   806 	    } else if (ferror(f)) {                     \
   807                 if (__threadErrno == EINTR) {                   \
   807 		if (__threadErrno == EINTR) {                   \
   808                     __HANDLE_INTERRUPTS__;              \
   808 		    __HANDLE_INTERRUPTS__;              \
   809                     clearerr(f);                        \
   809 		    clearerr(f);                        \
   810                     continue;                           \
   810 		    continue;                           \
   811                 }                                       \
   811 		}                                       \
   812             } else                                      \
   812 	    } else                                      \
   813                 (ret) = 0;                              \
   813 		(ret) = 0;                              \
   814             break;                                      \
   814 	    break;                                      \
   815         }                                               \
   815 	}                                               \
   816     } else {                                            \
   816     } else {                                            \
   817         OBJ rA = __INST(readAhead);                     \
   817 	OBJ rA = __INST(readAhead);                     \
   818         if (rA != nil) {                                \
   818 	if (rA != nil) {                                \
   819             *(buf) = __intVal(rA);                      \
   819 	    *(buf) = __intVal(rA);                      \
   820             DEBUGBUFFER(buf);                           \
   820 	    DEBUGBUFFER(buf);                           \
   821             __INST(readAhead) = nil;                    \
   821 	    __INST(readAhead) = nil;                    \
   822             (ret) = 1;                                  \
   822 	    (ret) = 1;                                  \
   823         } else {                                        \
   823 	} else {                                        \
   824             for (;;) {                                  \
   824 	    for (;;) {                                  \
   825                 CLEAR_ERRNO;                            \
   825 		CLEAR_ERRNO;                            \
   826                 (ret) = read(fileno(f), buf, 1);        \
   826 		(ret) = read(fileno(f), buf, 1);        \
   827                 DEBUGBUFFER(buf);                       \
   827 		DEBUGBUFFER(buf);                       \
   828                 if ((ret) >= 0 || __threadErrno != EINTR)       \
   828 		if ((ret) >= 0 || __threadErrno != EINTR)       \
   829                     break;                              \
   829 		    break;                              \
   830                 __HANDLE_INTERRUPTS__;                  \
   830 		__HANDLE_INTERRUPTS__;                  \
   831             }                                           \
   831 	    }                                           \
   832         }                                               \
   832 	}                                               \
   833    }
   833    }
   834 # endif /* use STDIO */
   834 # endif /* use STDIO */
   835 
   835 
   836 /*
   836 /*
   837  * read_bytes into a c-buffer
   837  * read_bytes into a c-buffer
   838  * (which may NOT move)
   838  * (which may NOT move)
   839  */
   839  */
   840 # ifdef NO_STDIO
   840 # ifdef NO_STDIO
   841 #  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
   841 #  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)   \
   842     {                                                   \
   842     {                                                   \
   843         int __offs = 0, __cnt;                                 \
   843 	int __offs = 0, __cnt;                                 \
   844                                                         \
   844 							\
   845         while (__offs < (cnt)) {                        \
   845 	while (__offs < (cnt)) {                        \
   846             OBJ rA = __INST(readAhead);                 \
   846 	    OBJ rA = __INST(readAhead);                 \
   847             if (rA != nil) {                            \
   847 	    if (rA != nil) {                            \
   848                 (buf)[__offs] = __intVal(rA);           \
   848 		(buf)[__offs] = __intVal(rA);           \
   849                 DEBUGBUFFER(buf);                       \
   849 		DEBUGBUFFER(buf);                       \
   850                 __INST(readAhead) = nil;                \
   850 		__INST(readAhead) = nil;                \
   851                 __offs++;                               \
   851 		__offs++;                               \
   852             } else {                                    \
   852 	    } else {                                    \
   853                 CLEAR_ERRNO;                            \
   853 		CLEAR_ERRNO;                            \
   854                 __cnt = READ(f, (buf)+__offs, (cnt)-__offs, handleType); \
   854 		__cnt = READ(f, (buf)+__offs, (cnt)-__offs, handleType); \
   855                 DEBUGBUFFER(buf);                       \
   855 		DEBUGBUFFER(buf);                       \
   856                 if (__cnt <= 0) {                       \
   856 		if (__cnt <= 0) {                       \
   857                     if (__cnt < 0 && __threadErrno == EINTR) {  \
   857 		    if (__cnt < 0 && __threadErrno == EINTR) {  \
   858                         __HANDLE_INTERRUPTS__;          \
   858 			__HANDLE_INTERRUPTS__;          \
   859                         continue;                       \
   859 			continue;                       \
   860                     }                                   \
   860 		    }                                   \
   861                     break;                              \
   861 		    break;                              \
   862                 }                                       \
   862 		}                                       \
   863                 __offs += __cnt;                        \
   863 		__offs += __cnt;                        \
   864             }                                           \
   864 	    }                                           \
   865         }                                               \
   865 	}                                               \
   866         if (__offs > 0)                                 \
   866 	if (__offs > 0)                                 \
   867             (ret) = __offs;                             \
   867 	    (ret) = __offs;                             \
   868    }
   868    }
   869 # else /* use STDIO */
   869 # else /* use STDIO */
   870 #  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)     \
   870 #  define __READBYTES__(ret, f, buf, cnt, isBuffered, handleType)     \
   871     (ret) = 0;                                          \
   871     (ret) = 0;                                          \
   872     if (isBuffered) {                                   \
   872     if (isBuffered) {                                   \
   873         int __offs = 0;                                 \
   873 	int __offs = 0;                                 \
   874         while (__offs < (cnt)) {                        \
   874 	while (__offs < (cnt)) {                        \
   875             CLEAR_ERRNO;                                \
   875 	    CLEAR_ERRNO;                                \
   876             (ret) = getc(f);                            \
   876 	    (ret) = getc(f);                            \
   877             if ((ret) < 0) {                            \
   877 	    if ((ret) < 0) {                            \
   878                 if (ferror(f)) {                        \
   878 		if (ferror(f)) {                        \
   879                     if (__threadErrno == EINTR) {               \
   879 		    if (__threadErrno == EINTR) {               \
   880                         __HANDLE_INTERRUPTS__;          \
   880 			__HANDLE_INTERRUPTS__;          \
   881                         clearerr(f);                    \
   881 			clearerr(f);                    \
   882                         continue;                       \
   882 			continue;                       \
   883                     }                                   \
   883 		    }                                   \
   884                 } else {                                \
   884 		} else {                                \
   885                     (ret) = 0;                          \
   885 		    (ret) = 0;                          \
   886                 }                                       \
   886 		}                                       \
   887                 break;                                  \
   887 		break;                                  \
   888             }                                           \
   888 	    }                                           \
   889             DEBUGBUFFER(buf);                           \
   889 	    DEBUGBUFFER(buf);                           \
   890             (buf)[__offs++] = (ret);                    \
   890 	    (buf)[__offs++] = (ret);                    \
   891         }                                               \
   891 	}                                               \
   892         if (__offs > 0)                                 \
   892 	if (__offs > 0)                                 \
   893             (ret) = __offs;                             \
   893 	    (ret) = __offs;                             \
   894     } else {                                            \
   894     } else {                                            \
   895         int __offs = 0, __cnt;                                 \
   895 	int __offs = 0, __cnt;                                 \
   896         int fd = fileno(f);                             \
   896 	int fd = fileno(f);                             \
   897                                                         \
   897 							\
   898         while (__offs < (cnt)) {                        \
   898 	while (__offs < (cnt)) {                        \
   899             OBJ rA = __INST(readAhead);                 \
   899 	    OBJ rA = __INST(readAhead);                 \
   900             if (rA != nil) {                            \
   900 	    if (rA != nil) {                            \
   901                 DEBUGBUFFER(buf);                       \
   901 		DEBUGBUFFER(buf);                       \
   902                 (buf)[__offs] = __intVal(rA);           \
   902 		(buf)[__offs] = __intVal(rA);           \
   903                 __INST(readAhead) = nil;                \
   903 		__INST(readAhead) = nil;                \
   904                 __offs++;                               \
   904 		__offs++;                               \
   905             } else {                                    \
   905 	    } else {                                    \
   906                 CLEAR_ERRNO;                            \
   906 		CLEAR_ERRNO;                            \
   907                 __cnt = read(fd, (buf)+__offs, (cnt)-__offs);                 \
   907 		__cnt = read(fd, (buf)+__offs, (cnt)-__offs);                 \
   908                 DEBUGBUFFER(buf);                       \
   908 		DEBUGBUFFER(buf);                       \
   909                 if (__cnt <= 0) {                       \
   909 		if (__cnt <= 0) {                       \
   910                     if (__cnt < 0 && __threadErrno == EINTR) {  \
   910 		    if (__cnt < 0 && __threadErrno == EINTR) {  \
   911                         __HANDLE_INTERRUPTS__;          \
   911 			__HANDLE_INTERRUPTS__;          \
   912                         continue;                       \
   912 			continue;                       \
   913                     }                                   \
   913 		    }                                   \
   914                     break;                              \
   914 		    break;                              \
   915                 }                                       \
   915 		}                                       \
   916                 __offs += __cnt;                        \
   916 		__offs += __cnt;                        \
   917             }                                           \
   917 	    }                                           \
   918         }                                               \
   918 	}                                               \
   919         if (__offs > 0)                                 \
   919 	if (__offs > 0)                                 \
   920             (ret) = __offs;                             \
   920 	    (ret) = __offs;                             \
   921    }
   921    }
   922 
   922 
   923 
   923 
   924 /*
   924 /*
   925  * FNDELAY and O_NDELAY is deprecated, O_NONBLOCK is used in POSIX, XPG, etc...
   925  * FNDELAY and O_NDELAY is deprecated, O_NONBLOCK is used in POSIX, XPG, etc...
   926  */
   926  */
   927 
   927 
   928 #  if defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(O_NDELAY) || defined(FNDELAY))
   928 #  if defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(O_NDELAY) || defined(FNDELAY))
   929 #   define SETFLAGS(fd, flags) \
   929 #   define SETFLAGS(fd, flags) \
   930         fcntl(fd, F_SETFL, flags)
   930 	fcntl(fd, F_SETFL, flags)
   931 
   931 
   932 #   if defined(O_NONBLOCK)
   932 #   if defined(O_NONBLOCK)
   933 #    define __STX_NONBLOCK_FLAG O_NONBLOCK
   933 #    define __STX_NONBLOCK_FLAG O_NONBLOCK
   934 #   else
   934 #   else
   935 #    if defined(O_NDELAY)
   935 #    if defined(O_NDELAY)
   938 #     define __STX_NONBLOCK_FLAG FNDELAY
   938 #     define __STX_NONBLOCK_FLAG FNDELAY
   939 #    endif
   939 #    endif
   940 #   endif
   940 #   endif
   941 
   941 
   942 #   define SETNONBLOCKING(fd, oldFlags) \
   942 #   define SETNONBLOCKING(fd, oldFlags) \
   943         { \
   943 	{ \
   944             int flags = fcntl(fd, F_GETFL, 0); \
   944 	    int flags = fcntl(fd, F_GETFL, 0); \
   945             if (flags >= 0) { \
   945 	    if (flags >= 0) { \
   946                 fcntl(fd, F_SETFL, flags | __STX_NONBLOCK_FLAG); \
   946 		fcntl(fd, F_SETFL, flags | __STX_NONBLOCK_FLAG); \
   947             } \
   947 	    } \
   948             oldFlags = flags; \
   948 	    oldFlags = flags; \
   949         }
   949 	}
   950 #  else
   950 #  else
   951 #   define SETFLAGS(fd, flags) /* nothing */
   951 #   define SETFLAGS(fd, flags) /* nothing */
   952 #   define SETNONBLOCKING(fd, oldFlags) /* nothing */
   952 #   define SETNONBLOCKING(fd, oldFlags) /* nothing */
   953 #  endif
   953 #  endif
   954 
   954 
   955 #  define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
   955 #  define __READAVAILBYTES__(ret, f, buf, cnt, isBuffered, handleType) \
   956   {                                                     \
   956   {                                                     \
   957     int __offs = 0, __cnt;                              \
   957     int __offs = 0, __cnt;                              \
   958     int oldFlags;                                       \
   958     int oldFlags;                                       \
   959                                                         \
   959 							\
   960     (ret) = 0;                                          \
   960     (ret) = 0;                                          \
   961     SETNONBLOCKING(fileno(f), oldFlags);                \
   961     SETNONBLOCKING(fileno(f), oldFlags);                \
   962     if (isBuffered) {                                   \
   962     if (isBuffered) {                                   \
   963         while (__offs < (cnt)) {                        \
   963 	while (__offs < (cnt)) {                        \
   964             CLEAR_ERRNO;                                \
   964 	    CLEAR_ERRNO;                                \
   965             (ret) = getc(f);                            \
   965 	    (ret) = getc(f);                            \
   966             if ((ret) < 0) {                            \
   966 	    if ((ret) < 0) {                            \
   967                 if (ferror(f)) {                        \
   967 		if (ferror(f)) {                        \
   968                     if (__threadErrno == EINTR) {       \
   968 		    if (__threadErrno == EINTR) {       \
   969                         (ret) = 0;                      \
   969 			(ret) = 0;                      \
   970                         clearerr(f);                    \
   970 			clearerr(f);                    \
   971                         break;                          \
   971 			break;                          \
   972                     }                                   \
   972 		    }                                   \
   973                 } else {                                \
   973 		} else {                                \
   974                     (ret) = 0;                          \
   974 		    (ret) = 0;                          \
   975                 }                                       \
   975 		}                                       \
   976                 break;                                  \
   976 		break;                                  \
   977             }                                           \
   977 	    }                                           \
   978             (buf)[__offs++] = (ret);                    \
   978 	    (buf)[__offs++] = (ret);                    \
   979             DEBUGBUFFER(buf);                           \
   979 	    DEBUGBUFFER(buf);                           \
   980         }                                               \
   980 	}                                               \
   981         if (__offs > 0)                                 \
   981 	if (__offs > 0)                                 \
   982             (ret) = __offs;                             \
   982 	    (ret) = __offs;                             \
   983     } else {                                            \
   983     } else {                                            \
   984         int fd = fileno(f);                             \
   984 	int fd = fileno(f);                             \
   985                                                         \
   985 							\
   986         while (__offs < (cnt)) {                        \
   986 	while (__offs < (cnt)) {                        \
   987             OBJ rA = __INST(readAhead);                 \
   987 	    OBJ rA = __INST(readAhead);                 \
   988             if (rA != nil) {                            \
   988 	    if (rA != nil) {                            \
   989                 (buf)[__offs] = __intVal(rA);           \
   989 		(buf)[__offs] = __intVal(rA);           \
   990                 DEBUGBUFFER(buf);                       \
   990 		DEBUGBUFFER(buf);                       \
   991                 __INST(readAhead) = nil;                \
   991 		__INST(readAhead) = nil;                \
   992                 __offs++;                               \
   992 		__offs++;                               \
   993                 continue;                               \
   993 		continue;                               \
   994             }                                           \
   994 	    }                                           \
   995             CLEAR_ERRNO;                                \
   995 	    CLEAR_ERRNO;                                \
   996             __cnt = read(fd, (buf)+__offs, (cnt)-__offs); \
   996 	    __cnt = read(fd, (buf)+__offs, (cnt)-__offs); \
   997             DEBUGBUFFER(buf);                           \
   997 	    DEBUGBUFFER(buf);                           \
   998             if (__cnt > 0) {                            \
   998 	    if (__cnt > 0) {                            \
   999                 __offs += __cnt;                        \
   999 		__offs += __cnt;                        \
  1000             }                                           \
  1000 	    }                                           \
  1001             break;                                      \
  1001 	    break;                                      \
  1002         }                                               \
  1002 	}                                               \
  1003         if (__offs > 0)                                 \
  1003 	if (__offs > 0)                                 \
  1004             (ret) = __offs;                             \
  1004 	    (ret) = __offs;                             \
  1005     }                                                   \
  1005     }                                                   \
  1006     SETFLAGS(fileno(f), oldFlags);                      \
  1006     SETFLAGS(fileno(f), oldFlags);                      \
  1007   }
  1007   }
  1008 
  1008 
  1009 # endif /* use STDIO */
  1009 # endif /* use STDIO */
  1017   {                                                     \
  1017   {                                                     \
  1018     int __ooffs = obj_offs;                             \
  1018     int __ooffs = obj_offs;                             \
  1019     int __offs = 0;                                     \
  1019     int __offs = 0;                                     \
  1020     int __cnt;                                          \
  1020     int __cnt;                                          \
  1021     char *buf = (char *)(obj);                          \
  1021     char *buf = (char *)(obj);                          \
  1022                                                         \
  1022 							\
  1023     (ret) = 0;                                          \
  1023     (ret) = 0;                                          \
  1024     {                                                   \
  1024     {                                                   \
  1025         while (__offs < (cnt)) {                        \
  1025 	while (__offs < (cnt)) {                        \
  1026             OBJ rA = __INST(readAhead);                 \
  1026 	    OBJ rA = __INST(readAhead);                 \
  1027             if (rA != nil) {                            \
  1027 	    if (rA != nil) {                            \
  1028                 (buf)[__ooffs+__offs] = __intVal(rA);   \
  1028 		(buf)[__ooffs+__offs] = __intVal(rA);   \
  1029                 DEBUGBUFFER(buf);                       \
  1029 		DEBUGBUFFER(buf);                       \
  1030                 __INST(readAhead) = nil;                \
  1030 		__INST(readAhead) = nil;                \
  1031                 __offs++;                               \
  1031 		__offs++;                               \
  1032             } else {                                    \
  1032 	    } else {                                    \
  1033                 CLEAR_ERRNO;                            \
  1033 		CLEAR_ERRNO;                            \
  1034                 __cnt = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
  1034 		__cnt = READ(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
  1035                 DEBUGBUFFER(buf);                       \
  1035 		DEBUGBUFFER(buf);                       \
  1036                 if (__cnt <= 0) {                       \
  1036 		if (__cnt <= 0) {                       \
  1037                     if (__cnt < 0 && __threadErrno == EINTR) {  \
  1037 		    if (__cnt < 0 && __threadErrno == EINTR) {  \
  1038                         __HANDLE_INTERRUPTS__;          \
  1038 			__HANDLE_INTERRUPTS__;          \
  1039                         /* refetch */                   \
  1039 			/* refetch */                   \
  1040                         buf = (char *)(obj);   \
  1040 			buf = (char *)(obj);   \
  1041                         continue;                       \
  1041 			continue;                       \
  1042                     }                                   \
  1042 		    }                                   \
  1043                     break;                              \
  1043 		    break;                              \
  1044                 }                                       \
  1044 		}                                       \
  1045                 __offs += __cnt;                        \
  1045 		__offs += __cnt;                        \
  1046             }                                           \
  1046 	    }                                           \
  1047         }                                               \
  1047 	}                                               \
  1048         if (__offs > 0)                                 \
  1048 	if (__offs > 0)                                 \
  1049             (ret) = __offs;                             \
  1049 	    (ret) = __offs;                             \
  1050     }                                                   \
  1050     }                                                   \
  1051   }
  1051   }
  1052 
  1052 
  1053 # else /* use STDIO */
  1053 # else /* use STDIO */
  1054 
  1054 
  1056   {                                                     \
  1056   {                                                     \
  1057     int __ooffs = obj_offs;                             \
  1057     int __ooffs = obj_offs;                             \
  1058     int __offs = 0;                                     \
  1058     int __offs = 0;                                     \
  1059     int __cnt;                                          \
  1059     int __cnt;                                          \
  1060     char *buf = (char *)(obj);                          \
  1060     char *buf = (char *)(obj);                          \
  1061                                                         \
  1061 							\
  1062     (ret) = 0;                                          \
  1062     (ret) = 0;                                          \
  1063     if (isBuffered) {                                   \
  1063     if (isBuffered) {                                   \
  1064         while (__offs < (cnt)) {                        \
  1064 	while (__offs < (cnt)) {                        \
  1065             CLEAR_ERRNO;                                \
  1065 	    CLEAR_ERRNO;                                \
  1066             (ret) = getc(f);                            \
  1066 	    (ret) = getc(f);                            \
  1067             if ((ret) < 0) {                            \
  1067 	    if ((ret) < 0) {                            \
  1068                 if (ferror(f)) {                        \
  1068 		if (ferror(f)) {                        \
  1069                     if (__threadErrno == EINTR) {       \
  1069 		    if (__threadErrno == EINTR) {       \
  1070                         __HANDLE_INTERRUPTS__;          \
  1070 			__HANDLE_INTERRUPTS__;          \
  1071                         clearerr(f);                    \
  1071 			clearerr(f);                    \
  1072                         /* refetch */                   \
  1072 			/* refetch */                   \
  1073                         buf = (char *)(obj);   \
  1073 			buf = (char *)(obj);   \
  1074                         DEBUGBUFFER(buf);               \
  1074 			DEBUGBUFFER(buf);               \
  1075                         continue;                       \
  1075 			continue;                       \
  1076                     }                                   \
  1076 		    }                                   \
  1077                 } else {                                \
  1077 		} else {                                \
  1078                     (ret) = 0;                          \
  1078 		    (ret) = 0;                          \
  1079                 }                                       \
  1079 		}                                       \
  1080                 break;                                  \
  1080 		break;                                  \
  1081             }                                           \
  1081 	    }                                           \
  1082             (buf)[__ooffs+__offs] = (ret);              \
  1082 	    (buf)[__ooffs+__offs] = (ret);              \
  1083             DEBUGBUFFER(buf);                           \
  1083 	    DEBUGBUFFER(buf);                           \
  1084             __offs++;                                   \
  1084 	    __offs++;                                   \
  1085         }                                               \
  1085 	}                                               \
  1086         if (__offs > 0)                                 \
  1086 	if (__offs > 0)                                 \
  1087             (ret) = __offs;                             \
  1087 	    (ret) = __offs;                             \
  1088     } else {                                            \
  1088     } else {                                            \
  1089         int fd = fileno(f);                             \
  1089 	int fd = fileno(f);                             \
  1090                                                         \
  1090 							\
  1091         while (__offs < (cnt)) {                        \
  1091 	while (__offs < (cnt)) {                        \
  1092             OBJ rA = __INST(readAhead);                 \
  1092 	    OBJ rA = __INST(readAhead);                 \
  1093             if (rA != nil) {                            \
  1093 	    if (rA != nil) {                            \
  1094                 (buf)[__ooffs+__offs] = __intVal(rA);   \
  1094 		(buf)[__ooffs+__offs] = __intVal(rA);   \
  1095                 DEBUGBUFFER(buf);                       \
  1095 		DEBUGBUFFER(buf);                       \
  1096                 __INST(readAhead) = nil;                \
  1096 		__INST(readAhead) = nil;                \
  1097                 __offs++;                               \
  1097 		__offs++;                               \
  1098             } else {                                    \
  1098 	    } else {                                    \
  1099                 CLEAR_ERRNO;                            \
  1099 		CLEAR_ERRNO;                            \
  1100                 __cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
  1100 		__cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
  1101                 DEBUGBUFFER(buf);                       \
  1101 		DEBUGBUFFER(buf);                       \
  1102                 if (__cnt <= 0) {                       \
  1102 		if (__cnt <= 0) {                       \
  1103                     if (__cnt < 0 && __threadErrno == EINTR) {  \
  1103 		    if (__cnt < 0 && __threadErrno == EINTR) {  \
  1104                         __HANDLE_INTERRUPTS__;          \
  1104 			__HANDLE_INTERRUPTS__;          \
  1105                         /* refetch */                   \
  1105 			/* refetch */                   \
  1106                         buf = (char *)(obj);   \
  1106 			buf = (char *)(obj);   \
  1107                         continue;                       \
  1107 			continue;                       \
  1108                     }                                   \
  1108 		    }                                   \
  1109                     break;                              \
  1109 		    break;                              \
  1110                 }                                       \
  1110 		}                                       \
  1111                 __offs += __cnt;                        \
  1111 		__offs += __cnt;                        \
  1112             }                                           \
  1112 	    }                                           \
  1113         }                                               \
  1113 	}                                               \
  1114         if (__offs > 0)                                 \
  1114 	if (__offs > 0)                                 \
  1115             (ret) = __offs;                             \
  1115 	    (ret) = __offs;                             \
  1116     }                                                   \
  1116     }                                                   \
  1117   }
  1117   }
  1118 
  1118 
  1119 # define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)     \
  1119 # define __READAVAILBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)     \
  1120   {                                                     \
  1120   {                                                     \
  1121     int __ooffs = obj_offs;                             \
  1121     int __ooffs = obj_offs;                             \
  1122     int __offs = 0;                                     \
  1122     int __offs = 0;                                     \
  1123     int __cnt;                                          \
  1123     int __cnt;                                          \
  1124     char *buf = (char *)(obj);                          \
  1124     char *buf = (char *)(obj);                          \
  1125     int oldFlags;                                       \
  1125     int oldFlags;                                       \
  1126                                                         \
  1126 							\
  1127     (ret) = 0;                                          \
  1127     (ret) = 0;                                          \
  1128     SETNONBLOCKING(fileno(f), oldFlags);                \
  1128     SETNONBLOCKING(fileno(f), oldFlags);                \
  1129                                                         \
  1129 							\
  1130     if (isBuffered) {                                   \
  1130     if (isBuffered) {                                   \
  1131         while (__offs < (cnt)) {                        \
  1131 	while (__offs < (cnt)) {                        \
  1132             CLEAR_ERRNO;                                \
  1132 	    CLEAR_ERRNO;                                \
  1133             (ret) = getc(f);                            \
  1133 	    (ret) = getc(f);                            \
  1134             if ((ret) < 0) {                            \
  1134 	    if ((ret) < 0) {                            \
  1135                 if (ferror(f)) {                        \
  1135 		if (ferror(f)) {                        \
  1136                     if (__threadErrno == EINTR) {       \
  1136 		    if (__threadErrno == EINTR) {       \
  1137                         clearerr(f);                    \
  1137 			clearerr(f);                    \
  1138                         /* refetch */                   \
  1138 			/* refetch */                   \
  1139                         buf = (char *)(obj);   \
  1139 			buf = (char *)(obj);   \
  1140                         (ret) = 0;                      \
  1140 			(ret) = 0;                      \
  1141                         break;                          \
  1141 			break;                          \
  1142                     }                                   \
  1142 		    }                                   \
  1143                 } else {                                \
  1143 		} else {                                \
  1144                     (ret) = 0;                          \
  1144 		    (ret) = 0;                          \
  1145                 }                                       \
  1145 		}                                       \
  1146                 break;                                  \
  1146 		break;                                  \
  1147             }                                           \
  1147 	    }                                           \
  1148             (buf)[__ooffs+__offs] = (ret);              \
  1148 	    (buf)[__ooffs+__offs] = (ret);              \
  1149             DEBUGBUFFER(buf);                           \
  1149 	    DEBUGBUFFER(buf);                           \
  1150             __offs++;                                   \
  1150 	    __offs++;                                   \
  1151         }                                               \
  1151 	}                                               \
  1152         if (__offs > 0)                                 \
  1152 	if (__offs > 0)                                 \
  1153             (ret) = __offs;                             \
  1153 	    (ret) = __offs;                             \
  1154     } else {                                            \
  1154     } else {                                            \
  1155         int fd = fileno(f);                             \
  1155 	int fd = fileno(f);                             \
  1156                                                         \
  1156 							\
  1157         while (__offs < (cnt)) {                        \
  1157 	while (__offs < (cnt)) {                        \
  1158             OBJ rA = __INST(readAhead);                 \
  1158 	    OBJ rA = __INST(readAhead);                 \
  1159             if (rA != nil) {                            \
  1159 	    if (rA != nil) {                            \
  1160                 (buf)[__ooffs+__offs] = __intVal(rA);   \
  1160 		(buf)[__ooffs+__offs] = __intVal(rA);   \
  1161                 DEBUGBUFFER(buf);                       \
  1161 		DEBUGBUFFER(buf);                       \
  1162                 __INST(readAhead) = nil;                \
  1162 		__INST(readAhead) = nil;                \
  1163                 __offs++;                               \
  1163 		__offs++;                               \
  1164                 continue;                               \
  1164 		continue;                               \
  1165             }                                           \
  1165 	    }                                           \
  1166             CLEAR_ERRNO;                                \
  1166 	    CLEAR_ERRNO;                                \
  1167             __cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
  1167 	    __cnt = read(fd, (buf)+__ooffs+__offs, (cnt)-__offs); \
  1168             DEBUGBUFFER(buf);                           \
  1168 	    DEBUGBUFFER(buf);                           \
  1169             if (__cnt > 0) {                            \
  1169 	    if (__cnt > 0) {                            \
  1170                 __offs += __cnt;                        \
  1170 		__offs += __cnt;                        \
  1171             }                                           \
  1171 	    }                                           \
  1172             break;                                      \
  1172 	    break;                                      \
  1173         }                                               \
  1173 	}                                               \
  1174         if (__offs > 0)                                 \
  1174 	if (__offs > 0)                                 \
  1175             (ret) = __offs;                             \
  1175 	    (ret) = __offs;                             \
  1176     }                                                   \
  1176     }                                                   \
  1177     SETFLAGS(fileno(f), oldFlags);                      \
  1177     SETFLAGS(fileno(f), oldFlags);                      \
  1178   }
  1178   }
  1179 
  1179 
  1180 
  1180 
  1181 # endif /* use STDIO */
  1181 # endif /* use STDIO */
  1182 
  1182 
  1183 # ifdef NO_STDIO
  1183 # ifdef NO_STDIO
  1184 #  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)          \
  1184 #  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)          \
  1185         for (;;) {                                      \
  1185 	for (;;) {                                      \
  1186             CLEAR_ERRNO;                                \
  1186 	    CLEAR_ERRNO;                                \
  1187             (ret) = WRITE(f, buf, 1, handleType);                   \
  1187 	    (ret) = WRITE(f, buf, 1, handleType);                   \
  1188             if ((ret) >= 0 || __threadErrno != EINTR)           \
  1188 	    if ((ret) >= 0 || __threadErrno != EINTR)           \
  1189                 break;                                  \
  1189 		break;                                  \
  1190             __HANDLE_INTERRUPTS__;                      \
  1190 	    __HANDLE_INTERRUPTS__;                      \
  1191         }
  1191 	}
  1192 # else /* use STDIO */
  1192 # else /* use STDIO */
  1193 #  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)        \
  1193 #  define __WRITEBYTE__(ret, f, buf, isBuffered, handleType)        \
  1194     if (isBuffered) {                                   \
  1194     if (isBuffered) {                                   \
  1195         for (;;) {                                      \
  1195 	for (;;) {                                      \
  1196             CLEAR_ERRNO;                                \
  1196 	    CLEAR_ERRNO;                                \
  1197             ret = putc(*(buf), f);                      \
  1197 	    ret = putc(*(buf), f);                      \
  1198             if ((ret) >= 0) {                           \
  1198 	    if ((ret) >= 0) {                           \
  1199                 (ret) = 1;                              \
  1199 		(ret) = 1;                              \
  1200             } else if (ferror(f)) {                     \
  1200 	    } else if (ferror(f)) {                     \
  1201                 /* SOLARIS/SPARC (2.6) generates spurious errors with errno = 0 */ \
  1201 		/* SOLARIS/SPARC (2.6) generates spurious errors with errno = 0 */ \
  1202                 if (__threadErrno == EINTR || __threadErrno == 0) {     \
  1202 		if (__threadErrno == EINTR || __threadErrno == 0) {     \
  1203                     __HANDLE_INTERRUPTS__;              \
  1203 		    __HANDLE_INTERRUPTS__;              \
  1204                     clearerr(f);                        \
  1204 		    clearerr(f);                        \
  1205                     continue;                           \
  1205 		    continue;                           \
  1206                 }                                       \
  1206 		}                                       \
  1207             } else                                      \
  1207 	    } else                                      \
  1208                 (ret) = 0;                              \
  1208 		(ret) = 0;                              \
  1209             break;                                      \
  1209 	    break;                                      \
  1210         }                                               \
  1210 	}                                               \
  1211     } else {                                            \
  1211     } else {                                            \
  1212         for (;;) {                                      \
  1212 	for (;;) {                                      \
  1213             CLEAR_ERRNO;                                \
  1213 	    CLEAR_ERRNO;                                \
  1214             (ret) = write(fileno(f), buf, 1);           \
  1214 	    (ret) = write(fileno(f), buf, 1);           \
  1215             if ((ret) >= 0 || __threadErrno != EINTR)           \
  1215 	    if ((ret) >= 0 || __threadErrno != EINTR)           \
  1216                 break;                                  \
  1216 		break;                                  \
  1217             __HANDLE_INTERRUPTS__;                      \
  1217 	    __HANDLE_INTERRUPTS__;                      \
  1218         }                                               \
  1218 	}                                               \
  1219    }
  1219    }
  1220 # endif /* use STDIO */
  1220 # endif /* use STDIO */
  1221 
  1221 
  1222 /*
  1222 /*
  1223  * write_bytes from a c-buffer
  1223  * write_bytes from a c-buffer
  1225  */
  1225  */
  1226 # ifdef NO_STDIO
  1226 # ifdef NO_STDIO
  1227 #  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
  1227 #  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
  1228     (ret) = 0;                                          \
  1228     (ret) = 0;                                          \
  1229     {                                                   \
  1229     {                                                   \
  1230         int __offs = 0;                                 \
  1230 	int __offs = 0;                                 \
  1231         while (__offs < (cnt)) {                        \
  1231 	while (__offs < (cnt)) {                        \
  1232             CLEAR_ERRNO;                                \
  1232 	    CLEAR_ERRNO;                                \
  1233             ret = WRITE(f, (buf)+__offs, (cnt)-__offs, handleType); \
  1233 	    ret = WRITE(f, (buf)+__offs, (cnt)-__offs, handleType); \
  1234             if (ret <= 0) {                             \
  1234 	    if (ret <= 0) {                             \
  1235                 if (ret < 0 && __threadErrno == EINTR) {        \
  1235 		if (ret < 0 && __threadErrno == EINTR) {        \
  1236                     __HANDLE_INTERRUPTS__;              \
  1236 		    __HANDLE_INTERRUPTS__;              \
  1237                     continue;                           \
  1237 		    continue;                           \
  1238                 }                                       \
  1238 		}                                       \
  1239                 break;                                  \
  1239 		break;                                  \
  1240             }                                           \
  1240 	    }                                           \
  1241             __offs += (ret);                            \
  1241 	    __offs += (ret);                            \
  1242         }                                               \
  1242 	}                                               \
  1243         if (__offs > 0)                                 \
  1243 	if (__offs > 0)                                 \
  1244             (ret) = __offs;                             \
  1244 	    (ret) = __offs;                             \
  1245    }
  1245    }
  1246 # else /* use STDIO */
  1246 # else /* use STDIO */
  1247 #  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
  1247 #  define __WRITEBYTES__(ret, f, buf, cnt, isBuffered, handleType)    \
  1248     (ret) = 0;                                          \
  1248     (ret) = 0;                                          \
  1249     if (isBuffered) {                                   \
  1249     if (isBuffered) {                                   \
  1250         int __offs = 0;                                 \
  1250 	int __offs = 0;                                 \
  1251         while (__offs < (cnt)) {                        \
  1251 	while (__offs < (cnt)) {                        \
  1252             CLEAR_ERRNO;                                \
  1252 	    CLEAR_ERRNO;                                \
  1253             ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
  1253 	    ret = fwrite((buf)+__offs, 1, (cnt)-__offs, f);\
  1254             if ((ret) <= 0) {                            \
  1254 	    if ((ret) <= 0) {                            \
  1255                 if (ferror(f)) {                        \
  1255 		if (ferror(f)) {                        \
  1256                     if (__threadErrno == EINTR) {               \
  1256 		    if (__threadErrno == EINTR) {               \
  1257                         __HANDLE_INTERRUPTS__;          \
  1257 			__HANDLE_INTERRUPTS__;          \
  1258                         clearerr(f);                    \
  1258 			clearerr(f);                    \
  1259                         continue;                       \
  1259 			continue;                       \
  1260                     }                                   \
  1260 		    }                                   \
  1261                     break;                              \
  1261 		    break;                              \
  1262                 } else {                                \
  1262 		} else {                                \
  1263                     (ret) = 0;                          \
  1263 		    (ret) = 0;                          \
  1264                 }                                       \
  1264 		}                                       \
  1265             }                                           \
  1265 	    }                                           \
  1266             __offs += (ret);                            \
  1266 	    __offs += (ret);                            \
  1267         }                                               \
  1267 	}                                               \
  1268         if (__offs > 0)                                 \
  1268 	if (__offs > 0)                                 \
  1269             (ret) = __offs;                             \
  1269 	    (ret) = __offs;                             \
  1270     } else {                                            \
  1270     } else {                                            \
  1271         int __offs = 0;                                 \
  1271 	int __offs = 0;                                 \
  1272         while (__offs < (cnt)) {                        \
  1272 	while (__offs < (cnt)) {                        \
  1273             CLEAR_ERRNO;                                \
  1273 	    CLEAR_ERRNO;                                \
  1274             ret = write(fileno(f), (buf)+__offs, (cnt)-__offs);\
  1274 	    ret = write(fileno(f), (buf)+__offs, (cnt)-__offs);\
  1275             if (ret <= 0) {                             \
  1275 	    if (ret <= 0) {                             \
  1276                 if (ret < 0 && __threadErrno == EINTR) {        \
  1276 		if (ret < 0 && __threadErrno == EINTR) {        \
  1277                     __HANDLE_INTERRUPTS__;              \
  1277 		    __HANDLE_INTERRUPTS__;              \
  1278                     continue;                           \
  1278 		    continue;                           \
  1279                 }                                       \
  1279 		}                                       \
  1280                 break;                                  \
  1280 		break;                                  \
  1281             }                                           \
  1281 	    }                                           \
  1282             __offs += (ret);                            \
  1282 	    __offs += (ret);                            \
  1283         }                                               \
  1283 	}                                               \
  1284         if (__offs > 0)                                 \
  1284 	if (__offs > 0)                                 \
  1285             (ret) = __offs;                             \
  1285 	    (ret) = __offs;                             \
  1286    }
  1286    }
  1287 # endif /* use STDIO */
  1287 # endif /* use STDIO */
  1288 
  1288 
  1289 /*
  1289 /*
  1290  * write_bytes from an object
  1290  * write_bytes from an object
  1294 #  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
  1294 #  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
  1295   {                                                     \
  1295   {                                                     \
  1296     int __ooffs = obj_offs;                             \
  1296     int __ooffs = obj_offs;                             \
  1297     int __offs = 0;                                     \
  1297     int __offs = 0;                                     \
  1298     char *buf = (char *)(obj);                          \
  1298     char *buf = (char *)(obj);                          \
  1299                                                         \
  1299 							\
  1300     (ret) = 0;                                          \
  1300     (ret) = 0;                                          \
  1301     {                                                   \
  1301     {                                                   \
  1302         while (__offs < (cnt)) {                        \
  1302 	while (__offs < (cnt)) {                        \
  1303             CLEAR_ERRNO;                                \
  1303 	    CLEAR_ERRNO;                                \
  1304             ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
  1304 	    ret = WRITE(f, (buf)+__ooffs+__offs, (cnt)-__offs, handleType); \
  1305             if (ret <= 0) {                             \
  1305 	    if (ret <= 0) {                             \
  1306                 if (ret < 0 && __threadErrno == EINTR) { \
  1306 		if (ret < 0 && __threadErrno == EINTR) { \
  1307                     __HANDLE_INTERRUPTS__;              \
  1307 		    __HANDLE_INTERRUPTS__;              \
  1308                     /* refetch */                       \
  1308 		    /* refetch */                       \
  1309                     buf = (char *)(obj);       \
  1309 		    buf = (char *)(obj);       \
  1310                     continue;                           \
  1310 		    continue;                           \
  1311                 }                                       \
  1311 		}                                       \
  1312                 break;                                  \
  1312 		break;                                  \
  1313             }                                           \
  1313 	    }                                           \
  1314             __offs += (ret);                            \
  1314 	    __offs += (ret);                            \
  1315         }                                               \
  1315 	}                                               \
  1316         if (__offs > 0)                                 \
  1316 	if (__offs > 0)                                 \
  1317             (ret) = __offs;                             \
  1317 	    (ret) = __offs;                             \
  1318     }                                                   \
  1318     }                                                   \
  1319   }
  1319   }
  1320 # else /* use STDIO */
  1320 # else /* use STDIO */
  1321 #  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
  1321 #  define __WRITEBYTES_OBJ__(ret, f, obj, obj_offs, cnt, isBuffered, handleType)            \
  1322   {                                                     \
  1322   {                                                     \
  1323     int __ooffs = obj_offs;                             \
  1323     int __ooffs = obj_offs;                             \
  1324     int __offs = 0;                                     \
  1324     int __offs = 0;                                     \
  1325     char *buf = (char *)(obj);                          \
  1325     char *buf = (char *)(obj);                          \
  1326                                                         \
  1326 							\
  1327     (ret) = 0;                                          \
  1327     (ret) = 0;                                          \
  1328     if (isBuffered) {                                   \
  1328     if (isBuffered) {                                   \
  1329         while (__offs < (cnt)) {                        \
  1329 	while (__offs < (cnt)) {                        \
  1330             CLEAR_ERRNO;                                \
  1330 	    CLEAR_ERRNO;                                \
  1331             ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f);           \
  1331 	    ret = fwrite((buf)+__ooffs+__offs, 1, (cnt)-__offs, f);           \
  1332             if ((ret) <= 0) {                           \
  1332 	    if ((ret) <= 0) {                           \
  1333                 if (ferror(f)) {                        \
  1333 		if (ferror(f)) {                        \
  1334                     if (__threadErrno == EINTR) {       \
  1334 		    if (__threadErrno == EINTR) {       \
  1335                         __HANDLE_INTERRUPTS__;          \
  1335 			__HANDLE_INTERRUPTS__;          \
  1336                         /* refetch */                   \
  1336 			/* refetch */                   \
  1337                         buf = (char *)(obj);   \
  1337 			buf = (char *)(obj);   \
  1338                         clearerr(f);                    \
  1338 			clearerr(f);                    \
  1339                         continue;                       \
  1339 			continue;                       \
  1340                     }                                   \
  1340 		    }                                   \
  1341                     break;                              \
  1341 		    break;                              \
  1342                 } else {                                \
  1342 		} else {                                \
  1343                     (ret) = 0;                          \
  1343 		    (ret) = 0;                          \
  1344                 }                                       \
  1344 		}                                       \
  1345             }                                           \
  1345 	    }                                           \
  1346             __offs += (ret);                            \
  1346 	    __offs += (ret);                            \
  1347         }                                               \
  1347 	}                                               \
  1348         if (__offs > 0)                                 \
  1348 	if (__offs > 0)                                 \
  1349             (ret) = __offs;                             \
  1349 	    (ret) = __offs;                             \
  1350     } else {                                            \
  1350     } else {                                            \
  1351         while (__offs < (cnt)) {                        \
  1351 	while (__offs < (cnt)) {                        \
  1352             CLEAR_ERRNO;                                \
  1352 	    CLEAR_ERRNO;                                \
  1353             ret = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs);       \
  1353 	    ret = write(fileno(f), (buf)+__ooffs+__offs, (cnt)-__offs);       \
  1354             if (ret <= 0) {                             \
  1354 	    if (ret <= 0) {                             \
  1355                 if (ret < 0 && __threadErrno == EINTR){ \
  1355 		if (ret < 0 && __threadErrno == EINTR){ \
  1356                     __HANDLE_INTERRUPTS__;              \
  1356 		    __HANDLE_INTERRUPTS__;              \
  1357                     /* refetch */                       \
  1357 		    /* refetch */                       \
  1358                     buf = (char *)(obj);       \
  1358 		    buf = (char *)(obj);       \
  1359                     continue;                           \
  1359 		    continue;                           \
  1360                 }                                       \
  1360 		}                                       \
  1361                 break;                                  \
  1361 		break;                                  \
  1362             }                                           \
  1362 	    }                                           \
  1363             __offs += (ret);                            \
  1363 	    __offs += (ret);                            \
  1364         }                                               \
  1364 	}                                               \
  1365         if (__offs > 0)                                 \
  1365 	if (__offs > 0)                                 \
  1366             (ret) = __offs;                             \
  1366 	    (ret) = __offs;                             \
  1367     }                                                   \
  1367     }                                                   \
  1368   }
  1368   }
  1369 # endif /* use STDIO */
  1369 # endif /* use STDIO */
  1370 #endif /* unix */
  1370 #endif /* unix */
  1371 
  1371 
  3027     __INST(lastErrorNumber) = nil;
  3027     __INST(lastErrorNumber) = nil;
  3028     if ((__INST(handleType) == nil)
  3028     if ((__INST(handleType) == nil)
  3029      || (__INST(handleType) == @symbol(filePointer))
  3029      || (__INST(handleType) == @symbol(filePointer))
  3030      || (__INST(handleType) == @symbol(socketFilePointer))
  3030      || (__INST(handleType) == @symbol(socketFilePointer))
  3031      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3031      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3032         if ((fp = __INST(handle)) != nil) {
  3032 	if ((fp = __INST(handle)) != nil) {
  3033             f = __FILEVal(fp);
  3033 	    f = __FILEVal(fp);
  3034 #ifdef WIN32
  3034 #ifdef WIN32
  3035             __threadErrno = 0;
  3035 	    __threadErrno = 0;
  3036             ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fileno(f)));
  3036 	    ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fileno(f)));
  3037             if (ret) {
  3037 	    if (ret) {
  3038                 RETURN (self);
  3038 		RETURN (self);
  3039             }
  3039 	    }
  3040 #else
  3040 #else
  3041 
  3041 
  3042             __BEGIN_INTERRUPTABLE__
  3042 	    __BEGIN_INTERRUPTABLE__
  3043             do {
  3043 	    do {
  3044                 __threadErrno = 0;
  3044 		__threadErrno = 0;
  3045                 ret = fsync(fileno(f));
  3045 		ret = fsync(fileno(f));
  3046             } while ((ret < 0) && (__threadErrno == EINTR));
  3046 	    } while ((ret < 0) && (__threadErrno == EINTR));
  3047             __END_INTERRUPTABLE__
  3047 	    __END_INTERRUPTABLE__
  3048 
  3048 
  3049             if (ret >= 0) {
  3049 	    if (ret >= 0) {
  3050                 RETURN (self);
  3050 		RETURN (self);
  3051             }
  3051 	    }
  3052 #endif /* ! WIN32 */
  3052 #endif /* ! WIN32 */
  3053             __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3053 	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3054         }
  3054 	}
  3055     }
  3055     }
  3056 #endif /* ! __openVMS__ */
  3056 #endif /* ! __openVMS__ */
  3057 %}.
  3057 %}.
  3058     lastErrorNumber notNil ifTrue:[^ self ioError].
  3058     lastErrorNumber notNil ifTrue:[^ self ioError].
  3059     handle isNil ifTrue:[^ self errorNotOpen].
  3059     handle isNil ifTrue:[^ self errorNotOpen].
  3060 
  3060 
  3061     "
  3061     "
  3062         |f|
  3062 	|f|
  3063         f := 'x' asFilename writeStream.
  3063 	f := 'x' asFilename writeStream.
  3064         f nextPutAll:'hallo'; sync; syncData; close
  3064 	f nextPutAll:'hallo'; sync; syncData; close
  3065     "
  3065     "
  3066 !
  3066 !
  3067 
  3067 
  3068 syncData
  3068 syncData
  3069     "make sure, that the OS writes cached data to the disk.
  3069     "make sure, that the OS writes cached data to the disk.
  3080     __INST(lastErrorNumber) = nil;
  3080     __INST(lastErrorNumber) = nil;
  3081     if ((__INST(handleType) == nil)
  3081     if ((__INST(handleType) == nil)
  3082      || (__INST(handleType) == @symbol(filePointer))
  3082      || (__INST(handleType) == @symbol(filePointer))
  3083      || (__INST(handleType) == @symbol(socketFilePointer))
  3083      || (__INST(handleType) == @symbol(socketFilePointer))
  3084      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3084      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3085         OBJ fp;
  3085 	OBJ fp;
  3086         FILEPOINTER f;
  3086 	FILEPOINTER f;
  3087 
  3087 
  3088         if ((fp = __INST(handle)) != nil) {
  3088 	if ((fp = __INST(handle)) != nil) {
  3089             f = __FILEVal(fp);
  3089 	    f = __FILEVal(fp);
  3090 #ifdef WIN32
  3090 #ifdef WIN32
  3091             __threadErrno = 0;
  3091 	    __threadErrno = 0;
  3092             ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fileno(f)));
  3092 	    ret = __STX_API_NOINT_CALL1( "FlushFileBuffers", FlushFileBuffers, _get_osfhandle(fileno(f)));
  3093             if (ret) {
  3093 	    if (ret) {
  3094                 RETURN (self);
  3094 		RETURN (self);
  3095             }
  3095 	    }
  3096 #else
  3096 #else
  3097 
  3097 
  3098             __BEGIN_INTERRUPTABLE__
  3098 	    __BEGIN_INTERRUPTABLE__
  3099             do {
  3099 	    do {
  3100                 __threadErrno = 0;
  3100 		__threadErrno = 0;
  3101                 ret = fdatasync(fileno(f));
  3101 		ret = fdatasync(fileno(f));
  3102             } while ((ret < 0) && (__threadErrno == EINTR));
  3102 	    } while ((ret < 0) && (__threadErrno == EINTR));
  3103             __END_INTERRUPTABLE__
  3103 	    __END_INTERRUPTABLE__
  3104 
  3104 
  3105             if (ret >= 0) {
  3105 	    if (ret >= 0) {
  3106                 RETURN (self);
  3106 		RETURN (self);
  3107             }
  3107 	    }
  3108 #endif /* ! WIN32 */
  3108 #endif /* ! WIN32 */
  3109             __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3109 	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3110         }
  3110 	}
  3111     }
  3111     }
  3112 #endif /* ! __openVMS__ */
  3112 #endif /* ! __openVMS__ */
  3113 %}.
  3113 %}.
  3114     lastErrorNumber notNil ifTrue:[^ self ioError].
  3114     lastErrorNumber notNil ifTrue:[^ self ioError].
  3115     handle isNil ifTrue:[^ self errorNotOpen].
  3115     handle isNil ifTrue:[^ self errorNotOpen].
  3116 
  3116 
  3117     "if notdef HAS_DATASYNC, fall back"
  3117     "if notdef HAS_DATASYNC, fall back"
  3118     self sync.
  3118     self sync.
  3119 
  3119 
  3120     "
  3120     "
  3121         |f|
  3121 	|f|
  3122         f := 'x' asFilename writeStream.
  3122 	f := 'x' asFilename writeStream.
  3123         f nextPutAll:'hallo'; sync; syncData; close
  3123 	f nextPutAll:'hallo'; sync; syncData; close
  3124     "
  3124     "
  3125 !
  3125 !
  3126 
  3126 
  3127 truncateTo:newSize
  3127 truncateTo:newSize
  3128     "truncate the underlying OS file to newSize.
  3128     "truncate the underlying OS file to newSize.
  3300     __INST(lastErrorNumber) = nil;
  3300     __INST(lastErrorNumber) = nil;
  3301     if ((__INST(handleType) == nil)
  3301     if ((__INST(handleType) == nil)
  3302      || (__INST(handleType) == @symbol(filePointer))
  3302      || (__INST(handleType) == @symbol(filePointer))
  3303      || (__INST(handleType) == @symbol(socketFilePointer))
  3303      || (__INST(handleType) == @symbol(socketFilePointer))
  3304      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3304      || (__INST(handleType) == @symbol(pipeFilePointer))) {
  3305         if (((fp = __INST(handle)) != nil)
  3305 	if (((fp = __INST(handle)) != nil)
  3306             && (__INST(mode) != @symbol(writeonly))
  3306 	    && (__INST(mode) != @symbol(writeonly))
  3307             && __bothSmallInteger(count, start)
  3307 	    && __bothSmallInteger(count, start)
  3308         ) {
  3308 	) {
  3309             f = __FILEVal(fp);
  3309 	    f = __FILEVal(fp);
  3310 
  3310 
  3311             cnt = __intVal(count);
  3311 	    cnt = __intVal(count);
  3312             offs = __intVal(start) - 1;
  3312 	    offs = __intVal(start) - 1;
  3313 
  3313 
  3314             if (__isExternalBytesLike(anObject)) {
  3314 	    if (__isExternalBytesLike(anObject)) {
  3315                 OBJ sz;
  3315 		OBJ sz;
  3316 
  3316 
  3317                 nInstBytes = 0;
  3317 		nInstBytes = 0;
  3318                 extPtr = (char *)(__externalBytesAddress(anObject));
  3318 		extPtr = (char *)(__externalBytesAddress(anObject));
  3319                 if (extPtr == NULL) goto bad;
  3319 		if (extPtr == NULL) goto bad;
  3320                 sz = __externalBytesSize(anObject);
  3320 		sz = __externalBytesSize(anObject);
  3321                 if (__isSmallInteger(sz)) {
  3321 		if (__isSmallInteger(sz)) {
  3322                     objSize = __intVal(sz);
  3322 		    objSize = __intVal(sz);
  3323                 } else {
  3323 		} else {
  3324                     objSize = 0; /* unknown */
  3324 		    objSize = 0; /* unknown */
  3325                 }
  3325 		}
  3326             } else {
  3326 	    } else {
  3327                 OBJ oClass;
  3327 		OBJ oClass;
  3328 
  3328 
  3329                 oClass = __Class(anObject);
  3329 		oClass = __Class(anObject);
  3330                 switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
  3330 		switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
  3331                     case BYTEARRAY:
  3331 		    case BYTEARRAY:
  3332                     case WORDARRAY:
  3332 		    case WORDARRAY:
  3333                     case LONGARRAY:
  3333 		    case LONGARRAY:
  3334                     case SWORDARRAY:
  3334 		    case SWORDARRAY:
  3335                     case SLONGARRAY:
  3335 		    case SLONGARRAY:
  3336                     case FLOATARRAY:
  3336 		    case FLOATARRAY:
  3337                     case DOUBLEARRAY:
  3337 		    case DOUBLEARRAY:
  3338                         break;
  3338 			break;
  3339                     default:
  3339 		    default:
  3340                         goto bad;
  3340 			goto bad;
  3341                 }
  3341 		}
  3342                 extPtr = (char *)0;
  3342 		extPtr = (char *)0;
  3343                 nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
  3343 		nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
  3344                 nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
  3344 		nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
  3345                 objSize = __Size(anObject) - nInstBytes;
  3345 		objSize = __Size(anObject) - nInstBytes;
  3346             }
  3346 	    }
  3347 
  3347 
  3348             if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
  3348 	    if ((offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs))) {
  3349                 _buffered = (__INST(buffered) == true);
  3349 		_buffered = (__INST(buffered) == true);
  3350                 if (_buffered) {
  3350 		if (_buffered) {
  3351                     __READING__(f);
  3351 		    __READING__(f);
  3352                 }
  3352 		}
  3353 
  3353 
  3354                 if (extPtr) {
  3354 		if (extPtr) {
  3355                     __READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType));
  3355 		    __READAVAILBYTES__(ret, f, extPtr+offs, cnt, _buffered, __INST(handleType));
  3356                 } else {
  3356 		} else {
  3357                     /*
  3357 		    /*
  3358                      * on interrupt, anObject may be moved to another location.
  3358 		     * on interrupt, anObject may be moved to another location.
  3359                      * So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__,
  3359 		     * So we pass (char *)__InstPtr(anObject) + nInstBytes + offs to the macro __READ_BYTES__,
  3360                      * to get a new address.
  3360 		     * to get a new address.
  3361                      */
  3361 		     */
  3362                     offs += nInstBytes;
  3362 		    offs += nInstBytes;
  3363                     __READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType));
  3363 		    __READAVAILBYTES_OBJ__(ret, f, anObject, offs, cnt, _buffered, __INST(handleType));
  3364                 }
  3364 		}
  3365 #ifdef PRE_22_jan_2000
  3365 #ifdef PRE_22_jan_2000
  3366                 if (ret > 0)
  3366 		if (ret > 0)
  3367 #else
  3367 #else
  3368                 /* 0 is NOT an EOF condition here ... */
  3368 		/* 0 is NOT an EOF condition here ... */
  3369                 if (ret >= 0)
  3369 		if (ret >= 0)
  3370 #endif
  3370 #endif
  3371                 {
  3371 		{
  3372                     if (__isSmallInteger(__INST(position))) {
  3372 		    if (__isSmallInteger(__INST(position))) {
  3373                         INT np = __intVal(__INST(position)) + ret;
  3373 			INT np = __intVal(__INST(position)) + ret;
  3374                         OBJ t;
  3374 			OBJ t;
  3375 
  3375 
  3376                         t = __MKINT(np); __INST(position) = t; __STORE(self, t);
  3376 			t = __MKINT(np); __INST(position) = t; __STORE(self, t);
  3377                     } else {
  3377 		    } else {
  3378                         __INST(position) = nil; /* i.e. do not know */
  3378 			__INST(position) = nil; /* i.e. do not know */
  3379                     }
  3379 		    }
  3380                     RETURN (__mkSmallInteger(ret));
  3380 		    RETURN (__mkSmallInteger(ret));
  3381                 }
  3381 		}
  3382 #ifdef PRE_22_jan_2000
  3382 #ifdef PRE_22_jan_2000
  3383                 if (ret == 0) {
  3383 		if (ret == 0) {
  3384                     __INST(hitEOF) = true;
  3384 		    __INST(hitEOF) = true;
  3385                 } else /* ret < 0 */
  3385 		} else /* ret < 0 */
  3386 #endif
  3386 #endif
  3387                 {
  3387 		{
  3388                     __INST(position) = nil;
  3388 		    __INST(position) = nil;
  3389                     __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3389 		    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  3390                 }
  3390 		}
  3391             }
  3391 	    }
  3392         }
  3392 	}
  3393     }
  3393     }
  3394 bad: ;
  3394 bad: ;
  3395 %}.
  3395 %}.
  3396     (hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0].
  3396     (hitEOF and:[self pastEndRead isNil]) ifTrue:[^ 0].
  3397     lastErrorNumber notNil ifTrue:[^ self readError].
  3397     lastErrorNumber notNil ifTrue:[^ self readError].
  4307 	    FILEPOINTER f;
  4307 	    FILEPOINTER f;
  4308 	    int rslt;
  4308 	    int rslt;
  4309 
  4309 
  4310 	    f = __FILEVal(fp);
  4310 	    f = __FILEVal(fp);
  4311 	    if (@global(FileOpenTrace) == true) {
  4311 	    if (@global(FileOpenTrace) == true) {
  4312 		fprintf(stderr, "fclose [ExternalStream] %x\n", f);
  4312 		fprintf(stderr, "fclose [ExternalStream] %"_lx_"\n", (INT)f);
  4313 	    }
  4313 	    }
  4314 #ifdef WIN32
  4314 #ifdef WIN32
  4315 	    if (__INST(mode) != @symbol(readonly)) {
  4315 	    if (__INST(mode) != @symbol(readonly)) {
  4316 		// do a fflush() first, so that fclose() doesn't block
  4316 		// do a fflush() first, so that fclose() doesn't block
  4317 		// we suspect, that EINTR causes problems in fclose()
  4317 		// we suspect, that EINTR causes problems in fclose()
  4379     FILE *fdopen();
  4379     FILE *fdopen();
  4380     int fd = -2;
  4380     int fd = -2;
  4381 
  4381 
  4382     if (__isStringLike(openmode)) {
  4382     if (__isStringLike(openmode)) {
  4383 #ifdef WIN32
  4383 #ifdef WIN32
  4384         __stxWrapApiEnterCritical();
  4384 	__stxWrapApiEnterCritical();
  4385 #endif
  4385 #endif
  4386         if (__isSmallInteger(aFileDescriptor)) {
  4386 	if (__isSmallInteger(aFileDescriptor)) {
  4387             fd = __intVal(aFileDescriptor);
  4387 	    fd = __intVal(aFileDescriptor);
  4388         }
  4388 	}
  4389 #ifdef WIN32
  4389 #ifdef WIN32
  4390         else if (__isExternalAddressLike(aFileDescriptor)) {
  4390 	else if (__isExternalAddressLike(aFileDescriptor)) {
  4391             fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
  4391 	    fd = _open_osfhandle((long)__externalAddressVal(aFileDescriptor), O_BINARY);
  4392             if (fd < 0) {
  4392 	    if (fd < 0) {
  4393                 if (__threadErrno == 0) {
  4393 		if (__threadErrno == 0) {
  4394                     // no more file descriptors
  4394 		    // no more file descriptors
  4395                     __threadErrno = EMFILE;
  4395 		    __threadErrno = EMFILE;
  4396                 }
  4396 		}
  4397                 error = __mkSmallInteger(__threadErrno);
  4397 		error = __mkSmallInteger(__threadErrno);
  4398                 __stxWrapApiLeaveCritical();
  4398 		__stxWrapApiLeaveCritical();
  4399                 goto out;
  4399 		goto out;
  4400             }
  4400 	    }
  4401         }
  4401 	}
  4402 #endif
  4402 #endif
  4403         f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
  4403 	f = (FILEPOINTER) fdopen(fd, (char *)__stringVal(openmode));
  4404 #ifdef WIN32
  4404 #ifdef WIN32
  4405         __stxWrapApiLeaveCritical();
  4405 	__stxWrapApiLeaveCritical();
  4406 #endif
  4406 #endif
  4407         if (f == NULL) {
  4407 	if (f == NULL) {
  4408             error =__mkSmallInteger(__threadErrno);
  4408 	    error =__mkSmallInteger(__threadErrno);
  4409         } else {
  4409 	} else {
  4410             if (@global(FileOpenTrace) == true) {
  4410 	    if (@global(FileOpenTrace) == true) {
  4411                 fprintf(stderr, "fdopen [ExternalStream] %d (%x) -> %x\n", fd, fd, f);
  4411 		fprintf(stderr, "fdopen [ExternalStream] %d (%"_lx_") -> %x\n", fd, fd, (INT)f);
  4412             }
  4412 	    }
  4413 
  4413 
  4414             fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4414 	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4415             retVal = self;
  4415 	    retVal = self;
  4416         }
  4416 	}
  4417     }
  4417     }
  4418 out:;
  4418 out:;
  4419 %}.
  4419 %}.
  4420     retVal notNil ifTrue:[
  4420     retVal notNil ifTrue:[
  4421         position := 0.
  4421 	position := 0.
  4422         buffered isNil ifTrue:[
  4422 	buffered isNil ifTrue:[
  4423             buffered := true.       "default is buffered"
  4423 	    buffered := true.       "default is buffered"
  4424         ].
  4424 	].
  4425         handleType := handleTypeSymbol.
  4425 	handleType := handleTypeSymbol.
  4426         Lobby register:self.
  4426 	Lobby register:self.
  4427         ^ retVal
  4427 	^ retVal
  4428     ].
  4428     ].
  4429     error notNil ifTrue:[
  4429     error notNil ifTrue:[
  4430         OperatingSystem closeFd:aFileDescriptor.
  4430 	OperatingSystem closeFd:aFileDescriptor.
  4431         lastErrorNumber := error.
  4431 	lastErrorNumber := error.
  4432         position := nil.
  4432 	position := nil.
  4433         "
  4433 	"
  4434          the open failed for some reason ...
  4434 	 the open failed for some reason ...
  4435         "
  4435 	"
  4436         ^ self openError:error
  4436 	^ self openError:error
  4437     ].
  4437     ].
  4438 !
  4438 !
  4439 
  4439 
  4440 dupFd
  4440 dupFd
  4441     "duplicate my file descriptor"
  4441     "duplicate my file descriptor"
  4480     if (__isStringLike(aPath) && __isStringLike(openModeString)) {
  4480     if (__isStringLike(aPath) && __isStringLike(openModeString)) {
  4481 #ifdef WIN32
  4481 #ifdef WIN32
  4482 # if 1
  4482 # if 1
  4483        f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4483        f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4484 # else
  4484 # else
  4485         __BEGIN_INTERRUPTABLE__
  4485 	__BEGIN_INTERRUPTABLE__
  4486         do {
  4486 	do {
  4487             f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4487 	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4488         } while ((f == NULL) && (__threadErrno == EINTR));
  4488 	} while ((f == NULL) && (__threadErrno == EINTR));
  4489         __END_INTERRUPTABLE__
  4489 	__END_INTERRUPTABLE__
  4490 # endif
  4490 # endif
  4491 #else /* UNIX */
  4491 #else /* UNIX */
  4492         __BEGIN_INTERRUPTABLE__
  4492 	__BEGIN_INTERRUPTABLE__
  4493         do {
  4493 	do {
  4494             f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4494 	    f = fopen((char *) __stringVal(aPath), (char *) __stringVal(openModeString));
  4495         } while ((f == NULL) && (__threadErrno == EINTR));
  4495 	} while ((f == NULL) && (__threadErrno == EINTR));
  4496         __END_INTERRUPTABLE__
  4496 	__END_INTERRUPTABLE__
  4497 #endif /* UNIX */
  4497 #endif /* UNIX */
  4498         if (f == NULL) {
  4498 	if (f == NULL) {
  4499             __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  4499 	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
  4500         } else {
  4500 	} else {
  4501             if (@global(FileOpenTrace) == true) {
  4501 	    if (@global(FileOpenTrace) == true) {
  4502                 fprintf(stderr, "fopen %s [ExternalStream] -> %x\n", __stringVal(aPath), f);
  4502 		fprintf(stderr, "fopen %s [ExternalStream] -> %"_lx_"\n", __stringVal(aPath), (INT)f);
  4503             }
  4503 	    }
  4504             fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4504 	    fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4505             __INST(handleType) = @symbol(filePointer);
  4505 	    __INST(handleType) = @symbol(filePointer);
  4506             ok = true;
  4506 	    ok = true;
  4507         }
  4507 	}
  4508     }
  4508     }
  4509 %}.
  4509 %}.
  4510     ok ifFalse:[
  4510     ok ifFalse:[
  4511         "
  4511 	"
  4512          the open failed for some reason ...
  4512 	 the open failed for some reason ...
  4513         "
  4513 	"
  4514         ^ self openError:lastErrorNumber.
  4514 	^ self openError:lastErrorNumber.
  4515     ].
  4515     ].
  4516     position := 0.
  4516     position := 0.
  4517     Lobby register:self.
  4517     Lobby register:self.
  4518 !
  4518 !
  4519 
  4519 
  4593 #ifdef WIN32
  4593 #ifdef WIN32
  4594     __stxWrapApiLeaveCritical();
  4594     __stxWrapApiLeaveCritical();
  4595 #endif
  4595 #endif
  4596     if (f != NULL) {
  4596     if (f != NULL) {
  4597 	if (@global(FileOpenTrace) == true) {
  4597 	if (@global(FileOpenTrace) == true) {
  4598 	    fprintf(stderr, "fdopen [ExternalStream] %d -> %x\n", fd, f);
  4598 	    fprintf(stderr, "fdopen [ExternalStream] %d -> %"_lx_"\n", fd, (INT)f);
  4599 	}
  4599 	}
  4600 	fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4600 	fp = __MKFILEPOINTER(f); __INST(handle) = fp; __STORE(self, fp);
  4601 	__INST(handleType) = @symbol(filePointer);
  4601 	__INST(handleType) = @symbol(filePointer);
  4602 	RETURN (self);
  4602 	RETURN (self);
  4603     }
  4603     }
  5743 nextPutAllUnicode:aString
  5743 nextPutAllUnicode:aString
  5744     "normal streams can not handle multi-byte characters, so convert them to utf8.
  5744     "normal streams can not handle multi-byte characters, so convert them to utf8.
  5745      This is needed, so that you can do ('something' asUnicode16String errorPrintCR)"
  5745      This is needed, so that you can do ('something' asUnicode16String errorPrintCR)"
  5746 
  5746 
  5747     aString do:[:eachCharacter|
  5747     aString do:[:eachCharacter|
  5748         self nextPutUtf8:eachCharacter.
  5748 	self nextPutUtf8:eachCharacter.
  5749     ].
  5749     ].
  5750 
  5750 
  5751     "
  5751     "
  5752         'Bönnigheim' asUnicode16String errorPrintCR
  5752 	'Bönnigheim' asUnicode16String errorPrintCR
  5753     "
  5753     "
  5754 !
  5754 !
  5755 
  5755 
  5756 nextPutUnicode:aCharacter
  5756 nextPutUnicode:aCharacter
  5757     "normal streams can not handle multi-byte characters, so convert them to utf8"
  5757     "normal streams can not handle multi-byte characters, so convert them to utf8"
  5760 ! !
  5760 ! !
  5761 
  5761 
  5762 !ExternalStream class methodsFor:'documentation'!
  5762 !ExternalStream class methodsFor:'documentation'!
  5763 
  5763 
  5764 version
  5764 version
  5765     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.364 2013-06-03 18:41:42 cg Exp $'
  5765     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.365 2013-06-26 11:04:06 cg Exp $'
  5766 !
  5766 !
  5767 
  5767 
  5768 version_CVS
  5768 version_CVS
  5769     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.364 2013-06-03 18:41:42 cg Exp $'
  5769     ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.365 2013-06-26 11:04:06 cg Exp $'
  5770 ! !
  5770 ! !
  5771 
  5771 
  5772 
  5772 
  5773 ExternalStream initialize!
  5773 ExternalStream initialize!