From b189506b1b895940933bdfc5d6d6ae5d0ba65721 Mon Sep 17 00:00:00 2001 From: solar Date: Sun, 20 Jun 2010 11:06:51 +0000 Subject: [PATCH] Some cleanups. --- functions/inttypes/imaxdiv.c | 1 - functions/stdio/fputs.c | 6 ++--- functions/stdio/rename.c | 26 +++++++++++++++++---- functions/string/strncpy.c | 17 ++++---------- includes/stdio.h | 11 +++++---- platform/example/functions/_PDCLIB/rename.c | 10 +++++--- platform/example/functions/stdio/remove.c | 13 +++++++++++ 7 files changed, 55 insertions(+), 29 deletions(-) diff --git a/functions/inttypes/imaxdiv.c b/functions/inttypes/imaxdiv.c index be93b99..94e69e2 100755 --- a/functions/inttypes/imaxdiv.c +++ b/functions/inttypes/imaxdiv.c @@ -15,7 +15,6 @@ imaxdiv_t imaxdiv( intmax_t numer, intmax_t denom ) imaxdiv_t rc; rc.quot = numer / denom; rc.rem = numer % denom; - /* TODO: pre-C99 compilers might require modulus corrections */ return rc; } diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 3dfe2d3..9a98089 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -24,9 +24,9 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest buffer runs full. */ stream->buffer[ stream->bufidx++ ] = *s; - /* TODO: Should IOLBF flush on \n, or the correct EOL sequence of the system? */ - if ( ( stream->bufidx == stream->bufsize ) - || ( ( stream->status & _IOLBF ) && *s == '\n' ) ) + if ( ( stream->bufidx == stream->bufsize ) || + ( ( stream->status & _IOLBF ) && *s == '\n' ) + ) { if ( _PDCLIB_flushbuffer( stream ) == EOF ) { diff --git a/functions/stdio/rename.c b/functions/stdio/rename.c index 3f9152d..2a34fe6 100644 --- a/functions/stdio/rename.c +++ b/functions/stdio/rename.c @@ -11,9 +11,22 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include + +extern struct _PDCLIB_file_t * _PDCLIB_filelist; + int rename( const char * old, const char * new ) { - /* TODO: Search open file list, flush and close file */ + struct _PDCLIB_file_t * current = _PDCLIB_filelist; + while ( current != NULL ) + { + if ( ( current->filename != NULL ) && ( strcmp( current->filename, old ) == 0 ) ) + { + /* File of that name currently open. Do not rename. */ + return EOF; + } + current = current->next; + } return _PDCLIB_rename( old, new ); } @@ -26,9 +39,9 @@ int rename( const char * old, const char * new ) int main( void ) { - /* TODO: Extend to internal testing (buffer etc.) */ char filename1[] = "touch testfile1"; char filename2[] = "testfile2"; + FILE * file; remove( filename1 + 6 ); remove( filename2 ); /* make sure that neither file exists */ @@ -39,16 +52,19 @@ int main( void ) /* create file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 */ TESTCASE( rename( filename1 + 6, filename2 ) == 0 ); /* check that file 2 exists, file 1 does not */ TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* create another file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 - expected to fail, see comment in _PDCLIB_rename() itself. */ diff --git a/functions/string/strncpy.c b/functions/string/strncpy.c index 2fb3f00..efb6480 100644 --- a/functions/string/strncpy.c +++ b/functions/string/strncpy.c @@ -8,9 +8,6 @@ #include -/* TODO: Debuggung only */ -#include - #ifndef REGTEST char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n ) @@ -19,19 +16,15 @@ char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, si while ( ( n > 0 ) && ( *s1++ = *s2++ ) ) { /* Cannot do "n--" in the conditional as size_t is unsigned and we have - to check it again for >0 in the next loop. + to check it again for >0 in the next loop below, so we must not risk + underflow. */ --n; } - /* TODO: This works correctly, but somehow the handling of n is ugly as - hell. - */ - if ( n > 0 ) + /* Checking against 1 as we missed the last --n in the loop above. */ + while ( n-- > 1 ) { - while ( --n ) - { - *s1++ = '\0'; - } + *s1++ = '\0'; } return rc; } diff --git a/includes/stdio.h b/includes/stdio.h index 4a029a0..014052b 100644 --- a/includes/stdio.h +++ b/includes/stdio.h @@ -52,17 +52,18 @@ extern FILE * stderr; /* Remove the given file. Returns zero if successful, non-zero otherwise. - This implementation does detect if the filename corresponds to an open file, - and closes it before attempting the rename. + This implementation does detect if a file of that name is currently open, + and fails the remove in this case. This does not detect two distinct names + that merely result in the same file (e.g. "/home/user/foo" vs. "~/foo"). */ int remove( const char * filename ); /* Rename the given old file to the given new name. Returns zero if successful, non-zero otherwise. This implementation does detect if the old filename corresponds to an open - file, and closes it before attempting the rename. - If the already is a file with the new filename, behaviour is defined by the - OS. + file, and fails the rename in this case. + If there already is a file with the new filename, behaviour is defined by + the glue code (see functions/_PDCLIB/rename.c). */ int rename( const char * old, const char * new ); diff --git a/platform/example/functions/_PDCLIB/rename.c b/platform/example/functions/_PDCLIB/rename.c index 3f770c9..53cf84c 100644 --- a/platform/example/functions/_PDCLIB/rename.c +++ b/platform/example/functions/_PDCLIB/rename.c @@ -92,6 +92,7 @@ int main( void ) { char filename1[] = "touch testfile1"; char filename2[] = "testfile2"; + FILE * file; remove( filename1 + 6 ); remove( filename2 ); /* check that neither file exists */ @@ -102,16 +103,19 @@ int main( void ) /* create file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 */ TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 ); /* check that file 2 exists, file 1 does not */ TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* create another file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 - expected to fail, see comment in _PDCLIB_rename() itself. */ diff --git a/platform/example/functions/stdio/remove.c b/platform/example/functions/stdio/remove.c index 0e66f4c..8bf3040 100644 --- a/platform/example/functions/stdio/remove.c +++ b/platform/example/functions/stdio/remove.c @@ -13,13 +13,26 @@ #ifndef REGTEST +#include + #include "/usr/include/errno.h" +extern struct _PDCLIB_file_t * _PDCLIB_filelist; + extern int unlink( const char * pathname ); int remove( const char * pathname ) { int rc; + struct _PDCLIB_file_t * current = _PDCLIB_filelist; + while ( current != NULL ) + { + if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) + { + return EOF; + } + current = current->next; + } if ( ( rc = unlink( pathname ) ) == -1 ) { switch ( errno ) -- 2.40.0