FileStream.st
changeset 13 62303f84ff5f
parent 10 4f1f9a91e406
child 31 75f2b9f78be2
equal deleted inserted replaced
12:8e03bd717355 13:62303f84ff5f
    22               All Rights Reserved
    22               All Rights Reserved
    23 
    23 
    24 This class provides access to the operating systems underlying file
    24 This class provides access to the operating systems underlying file
    25 system (i.e. its an interface to the stdio library).
    25 system (i.e. its an interface to the stdio library).
    26 
    26 
    27 $Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.5 1993-11-08 02:30:24 claus Exp $
    27 $Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.6 1993-12-11 00:48:19 claus Exp $
    28 '!
    28 '!
    29 
    29 
    30 %{
    30 %{
    31 #include <stdio.h>
    31 #include <stdio.h>
    32 #include <errno.h>
    32 #include <errno.h>
    41 # include <sys/types.h>
    41 # include <sys/types.h>
    42 # include <sys/stat.h>
    42 # include <sys/stat.h>
    43 #endif
    43 #endif
    44 
    44 
    45 #ifndef SEEK_SET
    45 #ifndef SEEK_SET
    46 # define SEEK_SET	0
    46 # define SEEK_SET       0
    47 #endif
    47 #endif
    48 #ifndef SEEK_CUR
    48 #ifndef SEEK_CUR
    49 # define SEEK_CUR	1
    49 # define SEEK_CUR       1
    50 #endif
    50 #endif
    51 #ifndef SEEK_END
    51 #ifndef SEEK_END
    52 # define SEEK_END	2
    52 # define SEEK_END       2
    53 #endif
    53 #endif
    54 
    54 
    55 %}
    55 %}
    56 
    56 
    57 !FileStream class methodsFor:'instance creation'!
    57 !FileStream class methodsFor:'instance creation'!
   269     extern errno;
   269     extern errno;
   270 
   270 
   271     if (_INST(filePointer) == nil) {
   271     if (_INST(filePointer) == nil) {
   272         path = _INST(pathName);
   272         path = _INST(pathName);
   273         if (path != nil 
   273         if (path != nil 
   274 	 && ((_qClass(path)==String) || (_qClass(path) == Filename))) {
   274          && ((_qClass(path)==String) || (_qClass(path) == Filename))) {
   275 	    do {
   275             do {
       
   276 #ifdef LINUX
       
   277                 /* LINUX returns a non-NULL f even when interrupted */
       
   278                 errno = 0;
   276                 f = (FILE *)fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
   279                 f = (FILE *)fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
   277 	    } while ((f == NULL) && (errno == EINTR));
   280                 if (errno == EINTR)
       
   281                     f = NULL;
       
   282 #else
       
   283 
       
   284                 f = (FILE *)fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
       
   285 #endif
       
   286             } while ((f == NULL) && (errno == EINTR));
   278             if (f == NULL) {
   287             if (f == NULL) {
   279                 ErrorNumber = _MKSMALLINT(errno);
   288                 ErrorNumber = _MKSMALLINT(errno);
   280                 _INST(position) = nil;
   289                 _INST(position) = nil;
   281             } else {
   290             } else {
   282                 _INST(filePointer) = _MKSMALLINT((int)f);
   291                 _INST(filePointer) = _MKSMALLINT((int)f);
   286         }
   295         }
   287     }
   296     }
   288 %}
   297 %}
   289 .
   298 .
   290     retVal notNil ifTrue:[
   299     retVal notNil ifTrue:[
   291 	buffered := true.	"default is buffered"
   300         buffered := true.       "default is buffered"
   292         lobby register:self
   301         lobby register:self
   293     ].
   302     ].
   294     ^ retVal
   303     ^ retVal
   295 !
   304 !
   296 
   305 
   340     "sent after snapin to reopen streams"
   349     "sent after snapin to reopen streams"
   341 
   350 
   342     filePointer notNil ifTrue:[
   351     filePointer notNil ifTrue:[
   343         "it was open, when snapped-out"
   352         "it was open, when snapped-out"
   344         filePointer := nil.
   353         filePointer := nil.
       
   354         lobby unregister:self.
   345         self open.
   355         self open.
   346         filePointer isNil ifTrue:[
   356         filePointer isNil ifTrue:[
       
   357             "this happens, if after a restart, the file is no longer accessable ..."
       
   358 
   347             ('could not reopen file: ', pathName) printNewline.
   359             ('could not reopen file: ', pathName) printNewline.
   348 	    lobby unregister:self
       
   349         ] ifFalse:[
   360         ] ifFalse:[
   350             self position:position.
   361             self position:position.
   351 	    lobby changed:self
       
   352         ]
   362         ]
   353     ]
   363     ]
   354 !
   364 !
   355 
   365 
   356 size
   366 size
   375     extern errno;
   385     extern errno;
   376     extern OBJ ErrorNumber;
   386     extern OBJ ErrorNumber;
   377 
   387 
   378     if (_INST(filePointer) != nil) {
   388     if (_INST(filePointer) != nil) {
   379         f = (FILE *)_intVal(_INST(filePointer));
   389         f = (FILE *)_intVal(_INST(filePointer));
   380 	do {
   390         do {
   381             ret = fstat(fileno(f), &buf);
   391             ret = fstat(fileno(f), &buf);
   382 	} while ((ret < 0) && (errno == EINTR));
   392         } while ((ret < 0) && (errno == EINTR));
   383         if (ret >= 0) {
   393         if (ret >= 0) {
   384             RETURN ( _MKSMALLINT(buf.st_size) );
   394             RETURN ( _MKSMALLINT(buf.st_size) );
   385         }
   395         }
   386 	ErrorNumber = _MKSMALLINT(errno);
   396         ErrorNumber = _MKSMALLINT(errno);
   387     }
   397     }
   388 #endif
   398 #endif
   389 %}
   399 %}
   390 .
   400 .
   391     "could add a fall-back here:
   401     "could add a fall-back here:
   411     extern errno;
   421     extern errno;
   412     extern OBJ ErrorNumber;
   422     extern OBJ ErrorNumber;
   413 
   423 
   414     if (_INST(filePointer) != nil) {
   424     if (_INST(filePointer) != nil) {
   415         f = (FILE *)_intVal(_INST(filePointer));
   425         f = (FILE *)_intVal(_INST(filePointer));
   416 	do {
   426         do {
   417 	    if (_INST(buffered) == true) {
   427             if (_INST(buffered) == true) {
   418                 currentPosition = ftell(f);
   428                 currentPosition = ftell(f);
   419 	    } else {
   429             } else {
   420 	        currentPosition = (long)lseek(fileno(f), SEEK_CUR, 0);
   430                 currentPosition = (long)lseek(fileno(f), SEEK_CUR, 0);
   421 	    }
   431             }
   422 	} while ((currentPosition < 0) && (errno == EINTR));
   432         } while ((currentPosition < 0) && (errno == EINTR));
   423         if (currentPosition >= 0) {
   433         if (currentPosition >= 0) {
   424             /*
   434             /*
   425              * notice: Smalltalk index starts at 1
   435              * notice: Smalltalk index starts at 1
   426              */
   436              */
   427             RETURN ( _MKSMALLINT(currentPosition + 1) );
   437             RETURN ( _MKSMALLINT(currentPosition + 1) );
   428         }
   438         }
   429 	ErrorNumber = _MKSMALLINT(errno);
   439         ErrorNumber = _MKSMALLINT(errno);
   430     }
   440     }
   431 %}
   441 %}
   432 .
   442 .
   433     filePointer isNil ifTrue:[^ self errorNotOpen].
   443     filePointer isNil ifTrue:[^ self errorNotOpen].
   434     ^ self primitiveFailed
   444     ^ self primitiveFailed
   448         if (_isSmallInteger(newPos)) {
   458         if (_isSmallInteger(newPos)) {
   449             f = (FILE *)_intVal(_INST(filePointer));
   459             f = (FILE *)_intVal(_INST(filePointer));
   450             /*
   460             /*
   451              * notice: Smalltalk index starts at 1
   461              * notice: Smalltalk index starts at 1
   452              */
   462              */
   453 	    do {
   463             do {
   454 	        if (_INST(buffered) == true) {
   464                 if (_INST(buffered) == true) {
   455                     ret = fseek(f, _intVal(newPos) - 1, 0);
   465                     ret = fseek(f, _intVal(newPos) - 1, 0);
   456                 } else {
   466                 } else {
   457                     ret = lseek(fileno(f), (long)_intVal(newPos) - 1, SEEK_SET);
   467                     ret = lseek(fileno(f), (long)_intVal(newPos) - 1, SEEK_SET);
   458 		}
   468                 }
   459 	    } while ((ret < 0) && (errno == EINTR));
   469             } while ((ret < 0) && (errno == EINTR));
   460             if (ret >= 0) {
   470             if (ret >= 0) {
   461                 _INST(position) = newPos;
   471                 _INST(position) = newPos;
   462                 RETURN ( self );
   472                 RETURN ( self );
   463             }
   473             }
   464             ErrorNumber = _MKSMALLINT(errno);
   474             ErrorNumber = _MKSMALLINT(errno);