]> pd.if.org Git - pdclib/commitdiff
Some cleanups.
authorsolar <unknown>
Sun, 20 Jun 2010 11:06:51 +0000 (11:06 +0000)
committersolar <unknown>
Sun, 20 Jun 2010 11:06:51 +0000 (11:06 +0000)
functions/inttypes/imaxdiv.c
functions/stdio/fputs.c
functions/stdio/rename.c
functions/string/strncpy.c
includes/stdio.h
platform/example/functions/_PDCLIB/rename.c
platform/example/functions/stdio/remove.c

index be93b99cfc0b453072d344e29cf934240b725221..94e69e23f93c30cd4e1bf19c8ec01c1053a133e4 100755 (executable)
@@ -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;
 }
 
index 3dfe2d3a0781b6dfb52371d453311035c7cbf0bb..9a980892adc3f1b8b39ea1e10397160d10fe211c 100644 (file)
@@ -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 )
             {
index 3f9152dca7a5d2fb07f1eaa0b558910f040ceb2e..2a34fe698f6704f1e9c626cf16bb9161fdc97e0f 100644 (file)
 #ifndef REGTEST
 #include <_PDCLIB_glue.h>
 
+#include <string.h>
+
+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.
     */
index 2fb3f000e947a179ee77e5f279cfd23acb1974dd..efb6480468391a0bf35fae0e8001bee7d0d5c1d9 100644 (file)
@@ -8,9 +8,6 @@
 
 #include <string.h>
 
-/* TODO: Debuggung only */
-#include <stdio.h>
-
 #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;
 }
index 4a029a01f493824aacf820c8b19246e9d037cc18..014052bda5f4451883f586f8c2e530248f67b00b 100644 (file)
@@ -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 );
 
index 3f770c9cc54cc32cc49ac3e47b6f5a586b4837e4..53cf84cb4063a7ff6ef97662314a7dd70e19e0de 100644 (file)
@@ -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.
     */
index 0e66f4c65c6cb3124f51fcbc8105fe793174f405..8bf3040cf03c9f821f68ecff2325a7c63b4e66af 100644 (file)
 
 #ifndef REGTEST
 
+#include <string.h>
+
 #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 )