From 04ff9a4a124eaa87d5d26d90077fb4ed15f3277f Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Wed, 15 Aug 2012 14:08:07 +0100 Subject: [PATCH] _PDCLIB_flushbuffer for win32. correct seeking behaviour. --- functions/_PDCLIB/filemode.c | 2 ++ functions/stdio/fgetpos.c | 1 + functions/stdio/fopen.c | 4 ++++ functions/stdio/ftell.c | 3 ++- .../win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c | 1 + platform/win32/functions/_PDCLIB/_PDCLIB_seek.c | 13 +++++++++++-- platform/win32/internals/_PDCLIB_config.h | 2 ++ 7 files changed, 23 insertions(+), 3 deletions(-) diff --git a/functions/_PDCLIB/filemode.c b/functions/_PDCLIB/filemode.c index ded5408..4548106 100644 --- a/functions/_PDCLIB/filemode.c +++ b/functions/_PDCLIB/filemode.c @@ -15,6 +15,8 @@ */ unsigned int _PDCLIB_filemode( char const * const mode ) { + if(!mode) return 0; + unsigned rc = 0; switch ( mode[0] ) { diff --git a/functions/stdio/fgetpos.c b/functions/stdio/fgetpos.c index 248fb35..3e41c40 100644 --- a/functions/stdio/fgetpos.c +++ b/functions/stdio/fgetpos.c @@ -31,6 +31,7 @@ int main( void ) TESTCASE( ( fh = tmpfile() ) != NULL ); TESTCASE( fgetpos( fh, &pos1 ) == 0 ); TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) ); + TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) ); TESTCASE( fgetpos( fh, &pos2 ) == 0 ); TESTCASE( fsetpos( fh, &pos1 ) == 0 ); TESTCASE( ftell( fh ) == 0 ); diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 18b7b3a..ff8e8e8 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -20,6 +20,10 @@ FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) { int imode = _PDCLIB_filemode( mode ); + + if( imode == 0 || filename == NULL ) + return NULL; + _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode ); if(fd == _PDCLIB_NOHANDLE) { return NULL; diff --git a/functions/stdio/ftell.c b/functions/stdio/ftell.c index 27e25ee..e82ca48 100644 --- a/functions/stdio/ftell.c +++ b/functions/stdio/ftell.c @@ -38,7 +38,8 @@ long int ftell( struct _PDCLIB_file_t * stream ) _PDCLIB_errno = _PDCLIB_ERANGE; return -1; } - return (long int)( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) ); + long int res = ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) ); + return res; } #endif diff --git a/platform/win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c b/platform/win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c index 82e36a0..d2cb60c 100644 --- a/platform/win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c +++ b/platform/win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c @@ -33,6 +33,7 @@ int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream ) BOOL res = WriteFile( stream->handle, stream->buffer + written, toWrite, &justWrote, NULL); written += justWrote; + stream->pos.offset += justWrote; if(!res) { stream->status |=_PDCLIB_ERRORFLAG; diff --git a/platform/win32/functions/_PDCLIB/_PDCLIB_seek.c b/platform/win32/functions/_PDCLIB/_PDCLIB_seek.c index c4c25f8..3add659 100644 --- a/platform/win32/functions/_PDCLIB/_PDCLIB_seek.c +++ b/platform/win32/functions/_PDCLIB/_PDCLIB_seek.c @@ -10,11 +10,20 @@ #include #ifndef REGTEST #include <_PDCLIB_glue.h> +#include +extern void _PDCLIB_w32errno( void ); _PDCLIB_int64_t _PDCLIB_seek( struct _PDCLIB_file_t * stream, _PDCLIB_int64_t offset, int whence ) { - errno = ENOTSUP; - return EOF; + LARGE_INTEGER liOffset; + liOffset.QuadPart = offset; + BOOL rv = SetFilePointerEx( stream->handle, liOffset, &liOffset, whence ); + if(!rv) { + _PDCLIB_w32errno(); + return EOF; + } + stream->pos.offset = liOffset.QuadPart; + return liOffset.QuadPart; } #endif diff --git a/platform/win32/internals/_PDCLIB_config.h b/platform/win32/internals/_PDCLIB_config.h index 793065d..c828c7a 100644 --- a/platform/win32/internals/_PDCLIB_config.h +++ b/platform/win32/internals/_PDCLIB_config.h @@ -336,6 +336,8 @@ typedef void * _PDCLIB_fd_t; /* The values of SEEK_SET, SEEK_CUR and SEEK_END, used by fseek(). Since at least one platform (POSIX) uses the same symbols for its own "seek" function, we use whatever the host defines (if it does define them). + + Win32 note: Must match Win32 API values (FILE_BEGIN/FILE_CURRENT/FILE_END) */ #define _PDCLIB_SEEK_SET 0 #define _PDCLIB_SEEK_CUR 1 -- 2.40.0