ExternalStream.st
changeset 447 7e27756077fa
parent 443 fae13c0f1512
child 454 344ccfa720d9
equal deleted inserted replaced
446:9005802b114a 447:7e27756077fa
    22 
    22 
    23 ExternalStream comment:'
    23 ExternalStream comment:'
    24 COPYRIGHT (c) 1988 by Claus Gittinger
    24 COPYRIGHT (c) 1988 by Claus Gittinger
    25 	      All Rights Reserved
    25 	      All Rights Reserved
    26 
    26 
    27 $Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.56 1995-10-23 16:53:43 cg Exp $
    27 $Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.57 1995-10-27 12:19:30 cg Exp $
    28 '!
    28 '!
    29 
    29 
    30 !ExternalStream primitiveDefinitions!
    30 !ExternalStream primitiveDefinitions!
    31 
    31 
    32 %{
    32 %{
    71 #endif
    71 #endif
    72 
    72 
    73 %}
    73 %}
    74 ! !
    74 ! !
    75 
    75 
       
    76 !ExternalStream primitiveFunctions!
       
    77 %{
       
    78 
       
    79 static int 
       
    80 __fwrite(file, cp, len, buffered)
       
    81     FILE *file;
       
    82     char *cp;
       
    83 {
       
    84 	int cnt;
       
    85 
       
    86 #ifdef LINUX
       
    87         errno = 0;
       
    88 
       
    89         /*
       
    90          * stdio library has a bug if interrupted
       
    91          * therefore, we go directly into write()
       
    92          */
       
    93         if (! buffered) {
       
    94             int cc, rest;
       
    95 
       
    96             cnt = 0;
       
    97             rest = len;
       
    98             do {
       
    99                 cc = write(fileno(file), cp, rest);
       
   100                 if (cc >= 0) {
       
   101                     cp += cc;
       
   102                     rest -= cc;
       
   103                     cnt += cc;
       
   104                     errno = EINTR; /* kludge */
       
   105                 }
       
   106             } while ((cnt != len) && (errno == EINTR));
       
   107         } else {
       
   108             cnt = fwrite(cp, 1, len, file);
       
   109             if (errno == EINTR) errno = 0;
       
   110         }
       
   111 #else
       
   112         errno = 0;
       
   113         do {
       
   114             cnt = fwrite(cp, 1, len, file);
       
   115             if (cnt != len) {
       
   116                 if (cnt >= 0) {
       
   117                     cp += cnt;
       
   118                     len -= cnt;
       
   119 # ifdef HPUX
       
   120                     clearerr(file);
       
   121 # endif
       
   122                 }
       
   123             }
       
   124         } while ((cnt != len) && (errno == EINTR));
       
   125         if (! buffered) {
       
   126             fflush(file);
       
   127         }
       
   128 #endif /* LINUX */
       
   129 
       
   130 	return cnt;
       
   131 }
       
   132 
       
   133 %}
       
   134 ! !
       
   135 
    76 !ExternalStream class methodsFor:'documentation'!
   136 !ExternalStream class methodsFor:'documentation'!
    77 
   137 
    78 copyright
   138 copyright
    79 "
   139 "
    80  COPYRIGHT (c) 1988 by Claus Gittinger
   140  COPYRIGHT (c) 1988 by Claus Gittinger
    89 "
   149 "
    90 !
   150 !
    91 
   151 
    92 version
   152 version
    93 "
   153 "
    94 $Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.56 1995-10-23 16:53:43 cg Exp $
   154 $Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.57 1995-10-27 12:19:30 cg Exp $
    95 "
   155 "
    96 !
   156 !
    97 
   157 
    98 documentation
   158 documentation
    99 "
   159 "
  1361      && (_INST(mode) != @symbol(readonly))) {
  1421      && (_INST(mode) != @symbol(readonly))) {
  1362 	if (__isSmallInteger(aByteValue)) {
  1422 	if (__isSmallInteger(aByteValue)) {
  1363 	    c = __intVal(aByteValue);
  1423 	    c = __intVal(aByteValue);
  1364 	    f = MKFD(fp);
  1424 	    f = MKFD(fp);
  1365 	    __BEGIN_INTERRUPTABLE__
  1425 	    __BEGIN_INTERRUPTABLE__
  1366 #ifdef OLD
  1426 /*
  1367 	    if (_INST(buffered) == false) {
  1427  *#ifdef OLD
  1368 		cnt = write(fileno(f), &c, 1);
  1428  *	    if (_INST(buffered) == false) {
  1369 	    } else 
  1429  *		cnt = write(fileno(f), &c, 1);
  1370 #endif
  1430  *	    } else 
  1371 	    {
  1431  *#endif
  1372 		__WRITING__(f)
  1432  *	    {
  1373 		cnt = fwrite(&c, 1, 1, f);
  1433  *		__WRITING__(f)
  1374 #ifndef OLD
  1434  *		cnt = fwrite(&c, 1, 1, f);
  1375 		if (_INST(buffered) == false) {
  1435  *#ifndef OLD
  1376 		    fflush(f);
  1436  *		if (_INST(buffered) == false) {
  1377 		}
  1437  *		    fflush(f);
  1378 #endif
  1438  *		}
  1379 	    }
  1439  *#endif
       
  1440  *	    }
       
  1441  */
       
  1442  	    __WRITING__(f)
       
  1443 	    cnt = __fwrite(f, &c, 1, (_INST(buffered) == true));
  1380 	    __END_INTERRUPTABLE__
  1444 	    __END_INTERRUPTABLE__
  1381 
  1445 
  1382 	    if (cnt == 1) {
  1446 	    if (cnt == 1) {
  1383 		pos = _INST(position);
  1447 		pos = _INST(position);
  1384 		if (pos != nil)
  1448 		if (pos != nil)
  1460 	    nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
  1524 	    nInstBytes = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
  1461 	    objSize = __Size(anObject) - nInstBytes;
  1525 	    objSize = __Size(anObject) - nInstBytes;
  1462 	    if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
  1526 	    if ( (offs >= 0) && (cnt >= 0) && (objSize >= (cnt + offs)) ) {
  1463 		cp = (char *)__InstPtr(anObject) + nInstBytes + offs;
  1527 		cp = (char *)__InstPtr(anObject) + nInstBytes + offs;
  1464 		__BEGIN_INTERRUPTABLE__
  1528 		__BEGIN_INTERRUPTABLE__
  1465 #ifdef OLD
  1529 /*
  1466 		if (_INST(buffered) == false) {
  1530  *#ifdef OLD
  1467 		    cnt = write(fileno(f), cp, cnt);
  1531  *		if (_INST(buffered) == false) {
  1468 		} else
  1532  *		    cnt = write(fileno(f), cp, cnt);
  1469 #endif
  1533  *		} else
  1470 		{
  1534  *#endif
  1471 		    __WRITING__(f)
  1535  *		{
  1472 		    cnt = fwrite(cp, 1, cnt, f);
  1536  *		    __WRITING__(f)
  1473 		}
  1537  *		    cnt = fwrite(cp, 1, cnt, f);
  1474 #ifndef OLD
  1538  *		}
  1475 		if (_INST(buffered) == false) {
  1539  *#ifndef OLD
  1476 		    fflush(f);
  1540  *		if (_INST(buffered) == false) {
  1477 		}
  1541  *		    fflush(f);
  1478 #endif
  1542  *		}
       
  1543  *#endif
       
  1544  */
       
  1545  	        __WRITING__(f)
       
  1546 		cnt = __fwrite(f, cp, cnt, (_INST(buffered) == true));
       
  1547 
  1479 		__END_INTERRUPTABLE__
  1548 		__END_INTERRUPTABLE__
  1480 
  1549 
  1481 		if (cnt >= 0) {
  1550 		if (cnt >= 0) {
  1482 		    pos = _INST(position);
  1551 		    pos = _INST(position);
  1483 		    if (pos != nil)
  1552 		    if (pos != nil)
  1889 	}
  1958 	}
  1890 	if (cp != NULL) {
  1959 	if (cp != NULL) {
  1891 	    f = MKFD(fp);
  1960 	    f = MKFD(fp);
  1892 
  1961 
  1893 	    __BEGIN_INTERRUPTABLE__
  1962 	    __BEGIN_INTERRUPTABLE__
       
  1963 #ifdef NONONO
       
  1964 
  1894 #ifdef OLD
  1965 #ifdef OLD
  1895 	    if (_INST(buffered) == false) {
  1966 	    if (_INST(buffered) == false) {
  1896 		cnt = write(fileno(f), cp, len);
  1967 		cnt = write(fileno(f), cp, len);
  1897 	    } else 
  1968  	    } else 
  1898 #endif
  1969 #endif
  1899 	    { 
  1970 	    { 
  1900 		__WRITING__(f)
  1971 		__WRITING__(f)
       
  1972 
  1901 #ifdef LINUX
  1973 #ifdef LINUX
  1902 		errno = 0;
  1974 		errno = 0;
  1903 
  1975 
  1904 		/*
  1976 		/*
  1905 		 * stdio library has a bug if interrupted
  1977 		 * stdio library has a bug if interrupted
  1944 	    }
  2016 	    }
  1945 
  2017 
  1946 	    if (_INST(buffered) == false) {
  2018 	    if (_INST(buffered) == false) {
  1947 		fflush(f);
  2019 		fflush(f);
  1948 	    }
  2020 	    }
  1949 
  2021 #endif
       
  2022 
       
  2023 	    __WRITING__(f)
       
  2024 	    cnt = __fwrite(f, cp, len, (_INST(buffered) == true));
  1950 	    __END_INTERRUPTABLE__
  2025 	    __END_INTERRUPTABLE__
       
  2026 
  1951 	    if (cnt == len) {
  2027 	    if (cnt == len) {
  1952 		pos = _INST(position);
  2028 		pos = _INST(position);
  1953 		if (pos != nil) {
  2029 		if (pos != nil) {
  1954 		    _INST(position) = __MKSMALLINT(__intVal(pos) + len);
  2030 		    _INST(position) = __MKSMALLINT(__intVal(pos) + len);
  1955 		}
  2031 		}
  2012 		    index2 = len;
  2088 		    index2 = len;
  2013 
  2089 
  2014 		__BEGIN_INTERRUPTABLE__
  2090 		__BEGIN_INTERRUPTABLE__
  2015 		len = index2 - index1 + 1;
  2091 		len = index2 - index1 + 1;
  2016 
  2092 
  2017 		__WRITING__(f)
  2093 /*
  2018                 
  2094  *		__WRITING__(f)
  2019 		do {
  2095  *               
  2020 		    cnt = fwrite(cp + index1 - 1, 1, len, f);
  2096  *		do {
  2021 		    if (cnt != len) {
  2097  *		    cnt = fwrite(cp + index1 - 1, 1, len, f);
  2022 			if (cnt >= 0) {
  2098  *		    if (cnt != len) {
  2023 			    if (errno == EINTR) {
  2099  *			if (cnt >= 0) {
  2024 				cp += cnt;
  2100  *			    if (errno == EINTR) {
  2025 				len -= cnt;
  2101  *				cp += cnt;
  2026 			    }
  2102  *				len -= cnt;
  2027 			}
  2103  *			    }
  2028 		    }
  2104  *			}
  2029 		} while ((cnt != len) && (errno == EINTR));
  2105  *		    }
  2030 
  2106  *		} while ((cnt != len) && (errno == EINTR));
  2031 		if (_INST(buffered) == false) {
  2107  *
  2032 		    fflush(f);
  2108  *		if (_INST(buffered) == false) {
  2033 		}
  2109  *		    fflush(f);
       
  2110  *		}
       
  2111  */
       
  2112 	        __WRITING__(f)
       
  2113 	        cnt = __fwrite(f, cp+index1-1, len, (_INST(buffered) == true));
  2034 
  2114 
  2035 		__END_INTERRUPTABLE__
  2115 		__END_INTERRUPTABLE__
  2036 		if (cnt == len) {
  2116 		if (cnt == len) {
  2037 		    if (_INST(position) != nil) {
  2117 		    if (_INST(position) != nil) {
  2038 			_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len);
  2118 			_INST(position) = __MKSMALLINT(__intVal(_INST(position)) + len);
  2176 			len = 1;
  2256 			len = 1;
  2177 			*rslt = ch;
  2257 			*rslt = ch;
  2178 		    }
  2258 		    }
  2179 		} else {
  2259 		} else {
  2180 		    do {
  2260 		    do {
       
  2261 			errno = 0;
  2181 			len = read(fd, rslt, 1);
  2262 			len = read(fd, rslt, 1);
  2182 		    } while ((len < 0) && (errno == EINTR));
  2263 		    } while ((len < 0) && (errno == EINTR));
  2183 		}
  2264 		}
  2184 		if (len <= 0) {
  2265 		if (len <= 0) {
  2185 		    if (rslt == buffer) {
  2266 		    if (rslt == buffer) {
  2251 		f = MKFD(fp);
  2332 		f = MKFD(fp);
  2252 		s = (char *) _stringVal(aString);
  2333 		s = (char *) _stringVal(aString);
  2253 		len = _stringSize(aString);
  2334 		len = _stringSize(aString);
  2254 
  2335 
  2255 		__BEGIN_INTERRUPTABLE__
  2336 		__BEGIN_INTERRUPTABLE__
  2256 #ifdef OLD
  2337 /*
  2257 		if (_INST(buffered) == false) {
  2338  *#ifdef OLD
  2258 		    cnt = write(fileno(f), s, len);
  2339  *		if (_INST(buffered) == false) {
  2259 		} else 
  2340  *		    cnt = write(fileno(f), s, len);
  2260 #endif
  2341  *		} else 
  2261 		{ 
  2342  *#endif
  2262 		    __WRITING__(f)
  2343  *		{ 
  2263 		    cnt = fwrite(s, 1, len, f);
  2344  *		    __WRITING__(f)
  2264 		}
  2345  *		    cnt = fwrite(s, 1, len, f);
       
  2346  *		}
       
  2347  *		if (cnt == len) {
       
  2348  *#ifdef OLD
       
  2349  *		    if (_INST(buffered) == false) {
       
  2350  *			cnt = write(fileno(f), "\n", 1);
       
  2351  *		    } else 
       
  2352  *#endif
       
  2353  *		    { 
       
  2354  *			cnt = fwrite("\n", 1, 1, f);
       
  2355  *		    }
       
  2356  *#ifndef OLD
       
  2357  *		    if (_INST(buffered) == false) {
       
  2358  *			if (fflush(f) == EOF) goto end;
       
  2359  *		    }
       
  2360  *#endif
       
  2361  *		    if (cnt == 1) {
       
  2362  *			pos = _INST(position);
       
  2363  *			if (pos != nil) {
       
  2364  *			    _INST(position) = __MKSMALLINT(__intVal(pos)+len+1);
       
  2365  *			}
       
  2366  *			__END_INTERRUPTABLE__
       
  2367  *			RETURN ( self );
       
  2368  *		    }
       
  2369  *		}
       
  2370  */
       
  2371 		__WRITING__(f)
       
  2372 		cnt = __fwrite(f, s, len, (_INST(buffered) == true));
  2265 		if (cnt == len) {
  2373 		if (cnt == len) {
  2266 #ifdef OLD
  2374 		    cnt = __fwrite(f, "\n", 1, (_INST(buffered) == true));
  2267 		    if (_INST(buffered) == false) {
       
  2268 			cnt = write(fileno(f), "\n", 1);
       
  2269 		    } else 
       
  2270 #endif
       
  2271 		    { 
       
  2272 			cnt = fwrite("\n", 1, 1, f);
       
  2273 		    }
       
  2274 #ifndef OLD
       
  2275 		    if (_INST(buffered) == false) {
       
  2276 			if (fflush(f) == EOF) goto end;
       
  2277 		    }
       
  2278 #endif
       
  2279 		    if (cnt == 1) {
  2375 		    if (cnt == 1) {
  2280 			pos = _INST(position);
  2376 			pos = _INST(position);
  2281 			if (pos != nil) {
  2377 			if (pos != nil) {
  2282 			    _INST(position) = __MKSMALLINT(__intVal(pos)+len+1);
  2378 			    _INST(position) = __MKSMALLINT(__intVal(pos)+len+1);
  2283 			}
  2379 			}
  2284 			__END_INTERRUPTABLE__
  2380 			__END_INTERRUPTABLE__
  2285 			RETURN ( self );
  2381 			RETURN ( self );
  2286 		    }
  2382 		    }
  2287 		}
  2383 		}
       
  2384 		
  2288 end:
  2385 end:
  2289 		__END_INTERRUPTABLE__
  2386 		__END_INTERRUPTABLE__
  2290 		_INST(lastErrorNumber) = __MKSMALLINT(errno);
  2387 		_INST(lastErrorNumber) = __MKSMALLINT(errno);
  2291 	    }
  2388 	    }
  2292 	}
  2389 	}