safeio.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 844 行 · 第 1/2 页
C
844 行
/***************************************************************************/
char cur_mode[10];
char *p;
char rbuff[10];
size_t maxsize = sizeof( rbuff );
int violations = NumViolations;
if( fp == stdout ) {
strcpy( cur_mode, "stdout" );
} else if( fp == stderr ) {
strcpy( cur_mode, "stderr" );
}
/***********************************************************************/
/* write testdata for gets_s */
/***********************************************************************/
VERIFY( fprintf( *my_fp, "%s\n", "LENGTH08" ) > 0 );
VERIFY( fprintf( *my_fp, "%s\n\n", "LENGTH16--------" ) > 0 );
VERIFY( fprintf( *my_fp, "%s\n", "LENGTH009" ) > 0 );
fflush( *my_fp );
EXPECT( freopen( CONSOLE_IN, "r+t", *my_fp ) != NULL );
VERIFY( (*my_fp = freopen( filename, "rt", stdin )) != NULL );
fseek( *my_fp, 0, SEEK_SET );
/***********************************************************************/
/* now test get_s */
/***********************************************************************/
p = gets_s( rbuff, maxsize ); /* normal input */
VERIFYS( p != NULL );
VERIFYS( strcmp( p ,"LENGTH08" ) == 0 );
VERIFYS( NumViolations == violations );
p = gets_s( rbuff, maxsize ); /* no nl within maxsize chars*/
VERIFYS( p == NULL );
VERIFYS( rbuff[ 0 ] == '\0' );
VERIFYS( NumViolations == ++violations );
p = gets_s( rbuff, maxsize ); /* empty line */
VERIFYS( p != NULL );
VERIFYS( *p == '\0' );
VERIFYS( NumViolations == violations );
p = gets_s( rbuff, maxsize ); /* normal input */
VERIFYS( p != NULL );
VERIFYS( strcmp( p ,"LENGTH009" ) == 0 );
VERIFYS( NumViolations == violations );
/* Test runtime-constraints for gets_s */
p = gets_s( NULL, maxsize );
VERIFYS( p == NULL );
VERIFYS( NumViolations == ++violations );
p = gets_s( rbuff, 0 );
VERIFYS( p == NULL );
VERIFYS( NumViolations == ++violations );
#if RSIZE_MAX != SIZE_MAX
p = gets_s( rbuff, ~0 );
VERIFYS( p == NULL );
VERIFYS( NumViolations == ++violations );
#endif
return( 1 );
}
int Test_Safeios( FILE *fp )
/****************************/
{
char cur_mode[10] = "safeio";
char filename[ L_tmpnam ];
FILE *my_fp;
EXPECT( tmpnam( filename ) != NULL );
EXPECT( (my_fp = freopen( filename, "a+t", fp )) != NULL );
/* A separate function is used to assure the deletion of the test file. */
Test_Safeio( fp, &my_fp, filename ); /* for gets_s() */
EXPECT( (fp = freopen( CONSOLE_IN, "r+t", my_fp )) != NULL );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_File_Errors( void )
/**************************/
{
char cur_mode[10] = "error";
char filename[ L_tmpnam ], filename2[ L_tmpnam ];
FILE *fp, *fp2;
/* get a temporary file */
EXPECT( tmpnam( filename ) != NULL );
EXPECT( ( fp = fopen( filename, "wt" ) ) != NULL );
/* test the error routines */
EXPECT( fgetc( fp ) == EOF );
EXPECT( fgetc( fp ) == EOF );
EXPECT( ferror( fp ) != 0 );
/* test perror */
EXPECT( tmpnam( filename2 ) != NULL );
EXPECT( ( fp2 = freopen( filename2, "a+t", stderr ) ) != NULL );
perror( "" );
fflush( stderr );
EXPECT( fgetc( fp2 ) == EOF );
EXPECT( fclose( fp2 ) == 0 );
EXPECT( remove( filename2 ) == 0 );
/* test clearerr() */
clearerr( fp );
EXPECT( ferror( fp ) == 0 );
/* cleanup */
EXPECT( fclose( fp ) == 0 );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_Flushes( void )
/**********************/
{
char cur_mode[10] = "flushes";
FILE *fpr, *fpw;
char filename[ L_tmpnam ];
VERIFY( tmpnam( filename ) != NULL );
EXPECT( (fpw = fopen( filename, "w" )) != NULL );
EXPECT( (fpr = fopen( filename, "r" )) != NULL );
/* Test fflush() */
EXPECT( fputc( 'z', fpw ) );
EXPECT( fgetc( fpr ) == EOF );
EXPECT( fflush( fpw ) == 0 );
EXPECT( fgetc( fpr ) != EOF );
/* Test flushall() */
EXPECT( fputc( 'z', fpw ) );
EXPECT( fgetc( fpr ) == EOF );
EXPECT( flushall() );
EXPECT( fgetc( fpr ) != EOF );
EXPECT( fclose( fpr ) == 0 );
EXPECT( fclose( fpw ) == 0 );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_fdopen( void )
/*********************/
{
int handle;
FILE *fp;
char filename[ L_tmpnam ];
char cur_mode[10] = "fdopen";
VERIFY( tmpnam( filename ) != NULL );
VERIFY( (fp = fopen( filename, "w+t" )) != NULL );
EXPECT( fputc( 'y', fp ) != EOF );
EXPECT( fclose( fp ) == 0 );
handle = open( filename, O_RDONLY | O_TEXT );
VERIFY( handle != -1 );
EXPECT( (fp = fdopen( handle, "r" )) != NULL );
if( fp != NULL ) {
EXPECT( fgetc( fp ) == 'y' );
fclose( fp );
} else {
close( handle );
}
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_setbuf( void )
/*********************/
{
FILE *fp;
char buffer[ BUFSIZ ];
char filename[ L_tmpnam ];
char cur_mode[10] = "setbuf";
VERIFY( tmpnam( filename ) != NULL );
VERIFY( (fp = fopen( filename, "w" )) != NULL );
setbuf( fp, buffer );
fputc( 'a', fp );
fputc( 'z', fp );
EXPECT( buffer[0] == 'a' );
EXPECT( buffer[1] == 'z' );
fclose( fp );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_setvbuf( void )
/**********************/
{
FILE *fp;
char buff1[ BUFSIZ ];
char filename[ L_tmpnam ];
char cur_mode[10] = "setvbuf";
/* open the tmpfile */
VERIFY( tmpnam( filename ) != NULL );
VERIFY( (fp = fopen( filename, "w" )) != NULL );
/* setvbuf is tested in only one mode */
setvbuf( fp, buff1, _IOFBF, BUFSIZ );
fclose( fp );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
int Test_ungetc( void )
/*********************/
{
FILE *fp;
char filename[ L_tmpnam ];
char cur_mode[10] = "ungetc";
/* open the tmpfile */
VERIFY( tmpnam( filename ) != NULL );
VERIFY( (fp = fopen( filename, "w+t" )) != NULL );
/* Test of ungetc() */
VERIFY( 'z' == ungetc( 'z', fp ) );
VERIFY( 'z' == getc( fp ) );
fclose( fp );
EXPECT( remove( filename ) == 0 );
return( 1 );
}
void Test_rt_constraints_fopen( void )
/************************************/
{
errno_t rc;
FILE * streamptr;
FILE * newstreamptr;
int violations = NumViolations;
char filename[] = "TESTFILE";
rc = fopen_s( NULL, filename, "r" );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
rc = fopen_s( &streamptr, NULL, "r" );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
VERIFYS( streamptr == NULL );
rc = fopen_s( &streamptr, filename, NULL );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
VERIFYS( streamptr == NULL );
rc = fopen_s( &streamptr, filename, "r" );
VERIFYS( NumViolations == violations ); /* no rt constraint */
VERIFYS( rc != 0 );
VERIFYS( streamptr == NULL );
/***********************************************************/
/* open testfile */
rc = fopen_s( &streamptr, filename, "w" );
VERIFYS( NumViolations == violations );
VERIFYS( rc == 0 );
/* reopen testfile */
rc = freopen_s( &newstreamptr, filename, "a", streamptr );
VERIFYS( NumViolations == violations );
VERIFYS( rc == 0 );
VERIFYS( newstreamptr != NULL );
/* reopen without filename allowed by os/2 vac3.xx */
/* disliked by NT VC++ */
rc = freopen_s( &streamptr, "", "w", newstreamptr );
VERIFYS( NumViolations == violations );
VERIFYS( rc != 0 );
VERIFYS( streamptr == NULL );
/* open testfile */
rc = fopen_s( &streamptr, filename, "w" );
VERIFYS( NumViolations == violations );
VERIFYS( rc == 0 );
/* newstreamptr NULL */
rc = freopen_s( NULL, filename, "a", streamptr );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
VERIFYS( streamptr != NULL );
/* mode NULL */
rc = freopen_s( &newstreamptr, filename, NULL, streamptr );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
VERIFYS( newstreamptr == NULL );
VERIFYS( streamptr != NULL );
/* old stream NULL */
rc = freopen_s( &newstreamptr, filename, "a", NULL );
VERIFYS( NumViolations == ++violations );
VERIFYS( rc != 0 );
VERIFYS( newstreamptr == NULL );
fclose( streamptr );
unlink( filename );
return;
}
int main( int argc, char *argv[] )
/********************************/
{
int cur_omode; /* current open mode */
int cur_update; /* current update mode */
int cur_ftype; /* current file type */
char cur_mode[MAX_MODE + 1]; /* actual file mode paramater */
TestFile *cur_test;
int old_stdout_fd;
FILE *old_stdout;
/*** Initialize ***/
strcpy( ProgramName, strlwr( argv[0] ) ); /* store filename */
#ifdef __SW_BW
con = fopen( "tmp.log", "a" );
#else
con = fopen( CONSOLE_OUT, "w" );
#endif
VERIFY( con != NULL );
/***********************************************************************/
/* set constraint-handler */
/***********************************************************************/
set_constraint_handler_s( my_constraint_handler );
/***********************************************************************/
/* Test runtime-constraints fopen_s freopen_s */
/***********************************************************************/
Test_rt_constraints_fopen();
/******************/
/* Start of tests */
/******************/
/* The following tests are run for each file mode type */
for( cur_omode = 0; cur_omode < NUM_OMODES; cur_omode++ ) {
for( cur_update = 0; cur_update < 2; cur_update++ ) {
for( cur_ftype = 0; cur_ftype < NUM_FTYPES; cur_ftype++ ) {
/* Get the mode string */
Mode_Get_s( cur_mode, cur_omode, cur_update, cur_ftype);
/* Test fopen, fclose, and ferror */
cur_test = TestFile_Get( cur_mode );
TestFile_Destroy( cur_test );
/* Test standard file input and output functions */
// Test_File_IO( cur_mode );
/* Test more of the standard functions */
Test_File_IO_More( cur_mode );
} /* cur_ftype */
} /* cur_update */
} /* cur_omode */
Test_Temp_IO( ); /* tmpfile based tests */
/* Create a clone of stdout for later use; must be done after
* Test_Temp_IO() which calls fcloseall(). This needs to be done
* for output redirection to work.
*/
old_stdout_fd = fileno( stdout );
EXPECT( (old_stdout_fd = dup( old_stdout_fd )) != -1 );
EXPECT( (old_stdout = fdopen( old_stdout_fd, "wt" )) != NULL );
Test_Safeios( stdout );
fprintf( old_stdout, "Tests completed (%s).\n", strlwr( argv[0] ) );
fclose( old_stdout );
fclose( con );
//Status_Print( );
#ifdef __SW_BW
fprintf( stderr, "Tests completed (%s).\n", strlwr( argv[0] ) );
_dwShutDown();
#endif
/****************/
/* End of tests */
/****************/
return( 0 );
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?