Exemplo n.º 1
0
void __purgefp( void )
{
    __stream_link       *next;

    _AccessIOB();
    while( _RWD_cstream != NULL ) {
        next = _RWD_cstream->next;
        lib_free( _RWD_cstream );
        _RWD_cstream = next;
    }
    _ReleaseIOB();
}
Exemplo n.º 2
0
_WCRTLINK int fclose( FILE *fp )
{
    __stream_link       *link;

    _AccessIOB();
    for( link = _RWD_ostream; link != NULL; link = link->next ) {
        if( link->stream == fp ) {
            _ReleaseIOB();
            return( __shutdown_stream( fp, 1 ) );
        }
    }
    _ReleaseIOB();
    return( -1 );     /* file not open */
}
Exemplo n.º 3
0
FILE *__allocfp( void )
{
    FILE                *end;
    FILE                *fp;
    __stream_link       *link;
    unsigned            flags;

    _AccessIOB();
    /* Try and take one off the recently closed list */
    link = _RWD_cstream;
    if( link != NULL ) {
        _RWD_cstream = link->next;
        fp = link->stream;
        flags = fp->_flag;
        goto got_one;
    }
    /* See if there is a static FILE structure available. */
    end = &_RWD_iob[_NFILES];
    for( fp = _RWD_iob; fp < end; ++fp ) {
        if( (fp->_flag & (_READ | _WRITE)) == 0 ) {
            link = lib_malloc( sizeof( __stream_link ) );
            if( link == NULL )
                goto no_mem;
            flags = 0;
            goto got_one;
        }
    }
    /* Allocate a new dynamic structure */
    link = lib_malloc( sizeof( __stream_link ) + sizeof( FILE ) );
    if( link == NULL )
        goto no_mem;
    fp = (FILE *)(link + 1);
    flags = _DYNAMIC;
got_one:
    memset( fp, 0, sizeof( *fp ) );
    fp->_flag = flags;
    link->stream = fp;
    link->stream->_link = link;     /* point back to link structure */
    link->next = _RWD_ostream;
    _RWD_ostream = link;
    _ReleaseIOB();
    return( fp );
no_mem:
    _RWD_errno = ENOMEM;
    _ReleaseIOB();
    return( NULL );     /* no free slots */
}
Exemplo n.º 4
0
static FILE *close_file( FILE *fp )
{
    __stream_link * link;
    __stream_link **owner;

    _AccessIOB();
    /* See if the file pointer is a currently open file. */
    link = _RWD_ostream;
    for( ;; ) {
        if( link == NULL ) break;
        if( link->stream == fp ) {
            if( fp->_flag & (_READ|_WRITE) ) {
                __doclose( fp, 1 );
            }
            _ReleaseIOB();
            return( fp );
        }
        link = link->next;
    }
    /*
       It's not on the list of open files, so check the list of
       recently closed ones.
    */
    owner = &_RWD_cstream;
    for( ;; ) {
        link = *owner;
        if( link == NULL ) break;
        if( link->stream == fp ) {
            /* remove from closed list and put on open */
            *owner = link->next;
            link->next = _RWD_ostream;
            _RWD_ostream = link;
            _ReleaseIOB();
            return( fp );
        }
        owner = &link->next;
    }
    /* We ain't seen that file pointer ever. Leave things be. */
    __set_errno( EBADF );
    _ReleaseIOB();
    return( NULL );
}
Exemplo n.º 5
0
int __flushall( int mask )
{
    __stream_link   *link;
    FILE            *fp;
    int             number_of_open_files;

    _AccessIOB();
    number_of_open_files = 0;
    for( link = _RWD_ostream; link != NULL; link = link->next ) {
        fp = link->stream;
        if( fp->_flag & mask ) {      /* if file is a candidate */
            ++number_of_open_files;
            if( fp->_flag & _DIRTY ) {
                __flush( fp );
            }
        }
    }
    _ReleaseIOB();
    return( number_of_open_files );
}
Exemplo n.º 6
0
void __freefp( FILE * fp )
{
    __stream_link       **owner;
    __stream_link       *link;

    _AccessIOB();
    owner = &_RWD_ostream;
    for( ;; ) {
        link = *owner;
        if( link == NULL )
            return;
        if( link->stream == fp )
            break;
        owner = &link->next;
    }
    fp->_flag |= _READ | _WRITE;
    (*owner) = link->next;
    link->next = _RWD_cstream;
    _RWD_cstream = link;
    _ReleaseIOB();
}