From 526e9954c39f30819e421cb74e82aa59d5a41013 Mon Sep 17 00:00:00 2001 From: solar Date: Mon, 26 Oct 2009 22:27:59 +0000 Subject: [PATCH] restrict keyword cleanup. --- functions/stdio/fgets.c | 2 +- functions/stdio/fputs.c | 2 +- functions/stdio/freopen.c | 4 +-- functions/stdio/fscanf.c | 56 +++++++++++++++++++++++++++---------- functions/stdio/fseek.c | 2 +- functions/stdio/ungetc.c | 2 +- functions/stdio/vsnprintf.c | 2 +- functions/stdio/vsprintf.c | 4 +-- 8 files changed, 51 insertions(+), 23 deletions(-) diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index a82818a..f763e33 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -13,7 +13,7 @@ #define _PDCLIB_GLUE_H _PDCLIB_GLUE_H #include <_PDCLIB_glue.h> -char * fgets( char * s, int size, struct _PDCLIB_file_t * stream ) +char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( size <= 1 ) { diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 7a62d03..3dfe2d3 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -11,7 +11,7 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> -int fputs( const char * s, struct _PDCLIB_file_t * stream ) +int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( _PDCLIB_prepwrite( stream ) == EOF ) { diff --git a/functions/stdio/freopen.c b/functions/stdio/freopen.c index e8cc72a..906096f 100644 --- a/functions/stdio/freopen.c +++ b/functions/stdio/freopen.c @@ -1,6 +1,6 @@ /* $Id$ */ -/* freopen( const char *, const char * ) +/* freopen( const char *, const char *, FILE * ) This file is part of the Public Domain C Library (PDCLib). Permission is granted to use, modify, and / or redistribute at will. @@ -21,7 +21,7 @@ (Primary use of this function is to redirect stdin, stdout, and stderr.) */ -struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * stream ) +struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { /* FIXME: This is ad-hoc (to make the vprintf() testdriver work), and must be checked. */ /* FIXME: If filename is NULL, change mode. */ diff --git a/functions/stdio/fscanf.c b/functions/stdio/fscanf.c index 6ea4866..17b0dca 100644 --- a/functions/stdio/fscanf.c +++ b/functions/stdio/fscanf.c @@ -26,27 +26,55 @@ int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format #ifdef TEST #include <_PDCLIB_test.h> -#include #include -int main( void ) +char scanstring[] = " 1 23\00045\000\00067 "; + +void scantest( int testnr, FILE * fh, size_t position, char const * format, + int expected_fscan_rc, char const * expected_fscan_output, size_t expected_fscan_length, + int expected_sscan_rc, char const * expected_sscan_output, size_t expected_sscan_length ) { - char teststring1[] = " 1 23\045\0\067 "; char buffer[15]; + printf( "Test %d\n", testnr ); + TESTCASE( memset( buffer, -1, 15 ) == buffer ); + TESTCASE( fseek( fh, position, SEEK_SET ) == 0 ); + TESTCASE( fscanf( fh, format, buffer ) == expected_fscan_rc ); + TESTCASE( memcmp( buffer, expected_fscan_output, expected_fscan_length ) == 0 ); + TESTCASE( memset( buffer, -1, 15 ) == buffer ); + TESTCASE( sscanf( scanstring + position, format, buffer ) == expected_sscan_rc ); + TESTCASE( memcmp( buffer, expected_sscan_output, expected_sscan_length ) == 0 ); +} + +int main( void ) +{ FILE * fh; TESTCASE( ( fh = fopen( "testfile", "w+" ) ) != NULL ); - TESTCASE( fwrite( teststring1, 15, 1, fh ) == 1 ); + TESTCASE( fwrite( scanstring, 14, 1, fh ) == 1 ); rewind( fh ); - /* */ - TESTCASE( memset( buffer, CHAR_MAX, 15 ) == buffer ); \ - TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 ); \ - TESTCASE( fscanf( fh, "%14c", buffer ) == 1 ); \ - TESTCASE( memcmp( buffer, teststring1 + 0, 14 ) == 0 ); \ - TESTCASE( buffer[ 14 ] == CHAR_MAX ); \ - TESTCASE( memset( buffer, CHAR_MAX, 15 ) == buffer ); \ - TESTCASE( sscanf( teststring1 + 14, "%14c", buffer ) ); \ - TESTCASE( memcmp( buffer, teststring1 + 0, 14 ) == 0 ); \ - TESTCASE( buffer[ 14 ] == CHAR_MAX ); + + /* %14c - full scan */ + scantest( 1, fh, 0, "%14c", + 1, " 1 23\00045\000\00067 \377", 15, + 1, " 1 23\377", 7 ); + + /* %c - default to one, reading whitespace */ + scantest( 2, fh, 0, "%c", + 1, " \377", 2, + 1, " \377", 2 ); + + /* %1c - reading zero byte */ + scantest( 3, fh, 9, "%1c", + 1, "\000\377", 2, + -1, "\377", 1 ); + + /* %0c - NOT reading EOF */ + scantest( 4, fh, 13, "%0c", + 0, "\377", 1, + 0, "\377", 1 ); + + TESTCASE( fclose( fh ) == 0 ); + //TESTCASE( remove( "testfile" ) == 0 ); + return TEST_RESULTS; } diff --git a/functions/stdio/fseek.c b/functions/stdio/fseek.c index 43789c8..9a94547 100644 --- a/functions/stdio/fseek.c +++ b/functions/stdio/fseek.c @@ -12,7 +12,7 @@ #include <_PDCLIB_glue.h> -int fseek( struct _PDCLIB_file_t * _PDCLIB_restrict stream, long offset, int whence ) +int fseek( struct _PDCLIB_file_t * stream, long offset, int whence ) { if ( stream->status & _PDCLIB_FWRITE ) { diff --git a/functions/stdio/ungetc.c b/functions/stdio/ungetc.c index f85ca9c..001735b 100644 --- a/functions/stdio/ungetc.c +++ b/functions/stdio/ungetc.c @@ -10,7 +10,7 @@ #ifndef REGTEST -int ungetc( int c, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +int ungetc( int c, struct _PDCLIB_file_t * stream ) { if ( c == EOF || stream->ungetidx == _PDCLIB_UNGETCBUFSIZE ) { diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 9c9ab59..21e76b1 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -11,7 +11,7 @@ #ifndef REGTEST -int vsnprintf( char * s, size_t n, const char * format, _PDCLIB_va_list arg ) +int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) { /* TODO: This function should interpret format as multibyte characters. */ /* Members: base, flags, n, i, this, s, width, prec, stream, arg */ diff --git a/functions/stdio/vsprintf.c b/functions/stdio/vsprintf.c index 308b710..9f6cdd2 100644 --- a/functions/stdio/vsprintf.c +++ b/functions/stdio/vsprintf.c @@ -12,9 +12,9 @@ #ifndef REGTEST -int vsprintf( char * str, const char * format, va_list arg ) +int vsprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, va_list arg ) { - return vsnprintf( str, SIZE_MAX, format, arg ); /* TODO: Replace with a non-checking call */ + return vsnprintf( s, SIZE_MAX, format, arg ); /* TODO: Replace with a non-checking call */ } #endif -- 2.40.0