Path: blob/master/waterbox/libc/functions/stdio/freopen.c
2 views
/* freopen( const char *, const char *, FILE * )12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#include <stdio.h>78#ifndef REGTEST9#include "_PDCLIB_io.h"10#include "_PDCLIB_glue.h"11#include <stdlib.h>12#include <string.h>1314FILE * freopen(15const char * _PDCLIB_restrict filename,16const char * _PDCLIB_restrict mode,17FILE * _PDCLIB_restrict stream18)19{20_PDCLIB_flockfile( stream );2122unsigned int status = stream->status &23( _IONBF | _IOLBF | _IOFBF | _PDCLIB_FREEBUFFER24| _PDCLIB_DELONCLOSE | _PDCLIB_STATIC );2526/* TODO: This function can change wide orientation of a stream */27if ( stream->status & _PDCLIB_FWRITE )28{29_PDCLIB_flushbuffer( stream );30}31if ( ( filename == NULL ) && ( stream->filename == NULL ) )32{33/* TODO: Special handling for mode changes on std-streams */34_PDCLIB_funlockfile( stream );35return NULL;36}37stream->ops->close(stream->handle);3839/* TODO: It is not nice to do this on a stream we just closed.40It does not matter with the current implementation of clearerr(),41but it might start to matter if someone replaced that implementation.42*/43_PDCLIB_clearerr_unlocked( stream );44/* The new filename might not fit the old buffer */45if ( filename == NULL )46{47/* Use previous filename */48filename = stream->filename;49}50else if ( ( stream->filename != NULL ) && ( strlen( stream->filename ) >= strlen( filename ) ) )51{52/* Copy new filename into existing buffer */53strcpy( stream->filename, filename );54}55else56{57/* Allocate new buffer */58if ( ( stream->filename = (char *)malloc( strlen( filename ) ) ) == NULL )59{60_PDCLIB_funlockfile( stream );61return NULL;62}63strcpy( stream->filename, filename );64}65if ( ( mode == NULL ) || ( filename[0] == '\0' ) )66{67_PDCLIB_funlockfile( stream );68return NULL;69}70if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 )71{72_PDCLIB_funlockfile( stream );73return NULL;74}75/* Re-add the flags we saved above */76stream->status |= status;77stream->bufidx = 0;78stream->bufend = 0;79stream->ungetidx = 0;80/* TODO: Setting mbstate */81if ( ! _PDCLIB_open( &stream->handle, &stream->ops, filename,82stream->status ) )83{84_PDCLIB_funlockfile( stream );85return NULL;86}87_PDCLIB_funlockfile( stream );88return stream;89}9091#endif9293#ifdef TEST94#include "_PDCLIB_test.h"9596int main( void )97{98FILE * fin;99FILE * fout;100TESTCASE( ( fin = fopen( testfile1, "wb+" ) ) != NULL );101TESTCASE( fputc( 'x', fin ) == 'x' );102TESTCASE( fclose( fin ) == 0 );103TESTCASE( ( fin = freopen( testfile1, "rb", stdin ) ) != NULL );104TESTCASE( getchar() == 'x' );105106TESTCASE( ( fout = freopen( testfile2, "wb+", stdout ) ) != NULL );107TESTCASE( putchar( 'x' ) == 'x' );108rewind( fout );109TESTCASE( fgetc( fout ) == 'x' );110111TESTCASE( fclose( fin ) == 0 );112TESTCASE( fclose( fout ) == 0 );113TESTCASE( remove( testfile1 ) == 0 );114TESTCASE( remove( testfile2 ) == 0 );115116return TEST_RESULTS;117}118119#endif120121122