imaxdiv_t rc;
rc.quot = numer / denom;
rc.rem = numer % denom;
- /* TODO: pre-C99 compilers might require modulus corrections */
return rc;
}
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 )
{
#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 );
}
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 */
/* 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.
*/
#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 )
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;
}
/* 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 );
{
char filename1[] = "touch testfile1";
char filename2[] = "testfile2";
+ FILE * file;
remove( filename1 + 6 );
remove( filename2 );
/* check that neither file exists */
/* 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.
*/
#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 )