diff --git a/winsup/mingw/ChangeLog b/winsup/mingw/ChangeLog index 51d637362..64cad4ee4 100644 --- a/winsup/mingw/ChangeLog +++ b/winsup/mingw/ChangeLog @@ -1,3 +1,26 @@ +2004-06-30 Danny Smith + + * include/stdio.h (__mingw_fseeko64): Add prototype. + * mingwex/mingw-fseek.c (__mingw_fseeko64): Add definition. + (__mingw_fwrite): Handle huge files. + +2004-06-30 Kees Zeelenberg + Danny Smith + + * include/stdio.h (fopen64): Add inline function. + (fseeko64): Add prototype. + (ftello64): Add inline function. + * include/io.h (lseek64): Add inline function. + * include/sys/types (off64_t): Add typedef. + (fpos64_t): Add typedef. + * mingwex/fopen64.c: New file. + * mingwex/fseeko64.c: New file. + * mingwex/ftello64.c: New file. + * mingwex/lseek64.c: New file. + * mingwex/Makefile.in (STDIO_DISTFILES): Add fopen64.c, + fseeko.64.c, ftello64.c, lseek64.c. + (STDIO_OBJS): Add fopen64.o, fseeko.64.o, ftello64.o, lseek64.o. + 2004-04-24 Luke Dunstan * include/limits.h (_I64_MIN, _I64_MAX, _UI64_MAX): Add defines. diff --git a/winsup/mingw/include/io.h b/winsup/mingw/include/io.h index 265f42bc5..66d59b5a8 100644 --- a/winsup/mingw/include/io.h +++ b/winsup/mingw/include/io.h @@ -144,6 +144,14 @@ _CRTIMP __int64 __cdecl _telli64(int); _CRTIMP intptr_t __cdecl _findfirst64(const char*, struct __finddata64_t*); _CRTIMP intptr_t __cdecl _findnext64(intptr_t, struct __finddata64_t*); #endif /* __MSVCRT_VERSION__ >= 0x0601 */ + +#ifndef __NO_MINGW_LFS +__CRT_INLINE off64_t lseek64 (int fd, off64_t offset, int whence) +{ + return _lseeki64(fd, (__int64) offset, whence); +} +#endif + #endif /* __MSVCRT__ */ #ifndef _NO_OLDNAMES diff --git a/winsup/mingw/include/stdio.h b/winsup/mingw/include/stdio.h index 63ff1d6ca..9b53fbfb1 100644 --- a/winsup/mingw/include/stdio.h +++ b/winsup/mingw/include/stdio.h @@ -392,6 +392,30 @@ _CRTIMP int __cdecl fileno (FILE*); #define fileno(__F) ((__F)->_file) #endif +#if defined (__MSVCRT__) && !defined (__NO_MINGW_LFS) +#include +__CRT_INLINE FILE* __cdecl fopen64 (const char* filename, const char* mode) +{ + return fopen (filename, mode); +} + +int __cdecl fseeko64 (FILE*, off64_t, int); + +#ifdef __USE_MINGW_FSEEK +int __cdecl __mingw_fseeko64 (FILE *, off64_t, int); +#define fseeko64(fp, offset, whence) __mingw_fseeko64(fp, offset, whence) +#endif + +__CRT_INLINE off64_t __cdecl ftello64 (FILE * stream) +{ + fpos_t pos; + if (fgetpos(stream, &pos)) + return -1LL; + else + return ((off64_t) pos); +} +#endif /* __NO_MINGW_LFS */ + #endif /* Not __STRICT_ANSI__ */ /* Wide versions */ diff --git a/winsup/mingw/include/sys/types.h b/winsup/mingw/include/sys/types.h index 333013399..2bc04e755 100644 --- a/winsup/mingw/include/sys/types.h +++ b/winsup/mingw/include/sys/types.h @@ -105,6 +105,15 @@ typedef _ssize_t ssize_t; #endif #endif /* Not _SSIZE_T_ */ +#ifndef _FPOS64_T_ +#define _FPOS64_T_ +typedef long long fpos64_t; +#endif + +#ifndef _OFF64_T_ +#define _OFF64_T_ +typedef long long off64_t; +#endif #endif /* Not RC_INVOKED */ diff --git a/winsup/mingw/mingwex/Makefile.in b/winsup/mingw/mingwex/Makefile.in index cc9e44040..db26626c2 100644 --- a/winsup/mingw/mingwex/Makefile.in +++ b/winsup/mingw/mingwex/Makefile.in @@ -62,6 +62,7 @@ MATH_DISTFILES = \ tgammaf.c tgammal.c trunc.c truncf.c truncl.c STDIO_DISTFILES = \ + fopen64.c fseeko64.c ftello64.c lseek64.c \ snprintf.c snwprintf.c vsnprintf.c vsnwprintf.c \ vfscanf.c vfwscanf.c vscanf.c vsscanf.c vswscanf.c vwscanf.c @@ -113,6 +114,7 @@ STDLIB_STUB_OBJS = \ strtof.o wcstof.o \ _Exit.o STDIO_OBJS = \ + fopen64.o fseeko64.o ftello64.o lseek64.o \ snprintf.o vsnprintf.o snwprintf.o vsnwprintf.o \ vfscanf.o vfwscanf.o vscanf.o vsscanf.o vswscanf.o vwscanf.o MATH_OBJS = \ diff --git a/winsup/mingw/mingwex/fopen64.c b/winsup/mingw/mingwex/fopen64.c new file mode 100644 index 000000000..d1dca885b --- /dev/null +++ b/winsup/mingw/mingwex/fopen64.c @@ -0,0 +1,7 @@ +#include + +FILE* __cdecl +fopen64 (const char* filename, const char* mode) +{ + return fopen (filename, mode); +} diff --git a/winsup/mingw/mingwex/fseeko64.c b/winsup/mingw/mingwex/fseeko64.c new file mode 100644 index 000000000..94e17e929 --- /dev/null +++ b/winsup/mingw/mingwex/fseeko64.c @@ -0,0 +1,27 @@ +#include +#include +#include + +int __cdecl +fseeko64 (FILE* stream, off64_t offset, int whence) +{ + fpos_t pos; + if (whence == SEEK_CUR) + { + /* If stream is invalid, fgetpos sets errno. */ + if (fgetpos (stream, &pos)) + return (-1); + pos += (fpos_t) offset; + } + else if (whence == SEEK_END) + pos = (fpos_t) (_filelengthi64 (_fileno (stream)) + offset); + else if (whence == SEEK_SET) + pos = (fpos_t) offset; + else + { + errno = EINVAL; + return (-1); + } + return fsetpos (stream, &pos); +} + diff --git a/winsup/mingw/mingwex/ftello64.c b/winsup/mingw/mingwex/ftello64.c new file mode 100644 index 000000000..97a388cd6 --- /dev/null +++ b/winsup/mingw/mingwex/ftello64.c @@ -0,0 +1,11 @@ +#include + +off64_t __cdecl +ftello64 (FILE * stream) +{ + fpos_t pos; + if (fgetpos(stream, &pos)) + return -1LL; + else + return ((off64_t) pos); +} diff --git a/winsup/mingw/mingwex/lseek64.c b/winsup/mingw/mingwex/lseek64.c new file mode 100644 index 000000000..8f8bfa5e4 --- /dev/null +++ b/winsup/mingw/mingwex/lseek64.c @@ -0,0 +1,8 @@ +#include + +off64_t lseek64 +(int fd, off64_t offset, int whence) +{ + return _lseeki64(fd, (__int64) offset, whence); +} + diff --git a/winsup/mingw/mingwex/mingw-fseek.c b/winsup/mingw/mingwex/mingw-fseek.c index 1ea011fa6..ebec94295 100644 --- a/winsup/mingw/mingwex/mingw-fseek.c +++ b/winsup/mingw/mingwex/mingw-fseek.c @@ -29,36 +29,54 @@ __mingw_fseek (FILE *fp, long offset, int whence) return fseek (fp, offset, whence); } +int +__mingw_fseeko64 (FILE *fp, off64_t offset, int whence) +{ +# undef fseeko64 + __mingw_fseek_called = 1; + return fseeko64 (fp, offset, whence); +} + int __mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp) { # undef fwrite if ((_osver & 0x8000) && __mingw_fseek_called) { - DWORD actual_length, current_position; + ULARGE_INTEGER actual_length; + LARGE_INTEGER current_position = {{0LL}}; __mingw_fseek_called = 0; fflush (fp); - actual_length = GetFileSize ((HANDLE) _get_osfhandle (fileno (fp)), - NULL); - current_position = SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)), - 0, 0, FILE_CURRENT); + actual_length.LowPart = GetFileSize ((HANDLE) _get_osfhandle (fileno (fp)), + &actual_length.HighPart); + if (actual_length.LowPart == 0xFFFFFFFF + && GetLastError() != NO_ERROR ) + return -1; + current_position.LowPart = SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)), + current_position.LowPart, + ¤t_position.HighPart, + FILE_CURRENT); + if (current_position.LowPart == 0xFFFFFFFF + && GetLastError() != NO_ERROR ) + return -1; + #ifdef DEBUG - printf ("__mingw_fwrite: current %ld, actual %ld\n", - current_position, actual_length); + printf ("__mingw_fwrite: current %I64u, actual %I64u\n", + current_position.QuadPart, actual_length.QuadPart); #endif /* DEBUG */ - if (current_position > actual_length) + if (current_position.QuadPart > actual_length.QuadPart) { static char __mingw_zeros[ZEROBLOCKSIZE]; - long numleft; + long long numleft; SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)), 0, 0, FILE_END); - numleft = current_position - actual_length; + numleft = current_position.QuadPart - actual_length.QuadPart; #ifdef DEBUG - printf ("__mingw_fwrite: Seeking %ld bytes past end\n", numleft); + printf ("__mingw_fwrite: Seeking %I64d bytes past end\n", numleft); #endif /* DEBUG */ - while (numleft > 0) + while (numleft > 0LL) { DWORD nzeros = (numleft > ZEROBLOCKSIZE) ? ZEROBLOCKSIZE : numleft;