Convert-Binary-C

 view release on metacpan or  search on metacpan

tests/include/pdclib/functions/stdio/freopen.c  view on Meta::CPAN

        }

        /* Close handle */
        _PDCLIB_close( stream->handle );

        /* Remove stream from list */
        _PDCLIB_getstream( stream );

        /* Delete tmpfile() */
        if ( stream->status & _PDCLIB_DELONCLOSE )
        {
            /* Have to switch here; stream->filename may have moved to
               filename after failed in-place mode change above.
            */
            _PDCLIB_remove( ( stream->filename == NULL ) ? filename : stream->filename );
            stream->status &= ~_PDCLIB_DELONCLOSE;
        }

        /* Free buffer */
        if ( stream->status & _PDCLIB_FREEBUFFER )
        {
            free( stream->buffer );
        }

        if ( filename == NULL )
        {
            /* Input was filename NULL, stream->filename NULL.
               No filename means there is nothing to reopen. In-place
               mode change was already attempted (and failed) above.
            */
            return NULL;
        }
        else
        {
            /* We have a filename, either from input or (if filename
               was NULL) from stream. We will attempt the re-open with
               that, and will retrieve _PDCLIB_realpath() from that.
               So stream->filename is no longer needed.
            */
            free( stream->filename );
        }
    }
    else
    {
        /* Not a valid stream. As _PDCLIB_init_file_t() cannot tell the
           difference, only knows that it has been called by freopen()
           (by the non-NULL parameter), we need to initialize the mutex
           here (so that either way, _PDCLIB_init_file_t() gets a pre-
           initialized mutex).
        */
#ifndef __STDC_NO_THREADS__
        if ( mtx_init( &stream->mtx, mtx_plain | mtx_recursive ) != thrd_success )
        {
            /* Could not initialize stream mutex */
            _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx );
            return NULL;
        }

#endif

        /* Locking the mutex, so we come out of the if-else with a locked
           mutex either way.
        */
        _PDCLIB_LOCK( stream->mtx );
    }

    /* Stream is closed, or never was open (even though its mutex exists
       and is locked) at this point.
       Now we check if we have the whereabouts to open it.
    */

    if ( filemode == 0 )
    {
        /* Mode invalid */
        _PDCLIB_UNLOCK( stream->mtx );
#ifndef __STDC_NO_THREADS__
        mtx_destroy( &stream->mtx );
#endif
        free( stream->filename );
        free( stream );
        _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx );
        return NULL;
    }

    if ( filename == NULL || filename[0] == '\0' )
    {
        /* No filename available (standard stream?) */
        _PDCLIB_UNLOCK( stream->mtx );
#ifndef __STDC_NO_THREADS__
        mtx_destroy( &stream->mtx );
#endif
        free( stream->filename );
        free( stream );
        _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx );
        return NULL;
    }

    /* (Re-)initializing the structure. */
    if ( _PDCLIB_init_file_t( stream ) == NULL )
    {
        /* Re-init failed. */
        _PDCLIB_UNLOCK( stream->mtx );
#ifndef __STDC_NO_THREADS__
        mtx_destroy( &stream->mtx );
#endif
        free( stream->filename );
        free( stream );
        _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx );
        return NULL;
    }

    /* Resetting buffer mode and filemode */
    stream->status |= filemode | _IOLBF;

    /* Attempt open */
    if ( ( stream->handle = _PDCLIB_open( filename, stream->status ) ) == _PDCLIB_NOHANDLE )
    {
        /* OS open() failed */
        _PDCLIB_UNLOCK( stream->mtx );
#ifndef __STDC_NO_THREADS__
        mtx_destroy( &stream->mtx );
#endif
        free( stream->filename );
        free( stream->buffer );
        free( stream );
        _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx );
        return NULL;
    }



( run in 1.770 second using v1.01-cache-2.11-cpan-39bf76dae61 )