2002-06-13 18:20:48 +08:00
|
|
|
/*
|
|
|
|
* Workaround for limitations on win9x where a file contents are
|
|
|
|
* not zero'd out if you seek past the end and then write.
|
|
|
|
* Copied from ming local-patch to binutils/bfd/libbfd.c written by
|
|
|
|
* Mumit Khan <khan@xraylith.wisc.edu>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <io.h>
|
2003-01-26 14:38:43 +08:00
|
|
|
#include <stdlib.h>
|
2002-06-13 18:20:48 +08:00
|
|
|
|
|
|
|
#define ZEROBLOCKSIZE 512
|
|
|
|
static int __mingw_fseek_called;
|
|
|
|
|
|
|
|
/* The fseek in Win9x runtime does not zero out the file if seeking past
|
|
|
|
the end; if you don't want random stuff from your disk included in your
|
|
|
|
output DLL/executable, use this version instead. On WinNT/Win2k, it
|
|
|
|
just calls runtime fseek().
|
|
|
|
|
|
|
|
CHECK/FIXME: Does this work for both text and binary modes?? */
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
__mingw_fseek (FILE *fp, long offset, int whence)
|
|
|
|
{
|
|
|
|
# undef fseek
|
|
|
|
__mingw_fseek_called = 1;
|
|
|
|
return fseek (fp, offset, whence);
|
|
|
|
}
|
|
|
|
|
2004-06-30 12:51:18 +08:00
|
|
|
int
|
|
|
|
__mingw_fseeko64 (FILE *fp, off64_t offset, int whence)
|
|
|
|
{
|
|
|
|
# undef fseeko64
|
|
|
|
__mingw_fseek_called = 1;
|
|
|
|
return fseeko64 (fp, offset, whence);
|
|
|
|
}
|
|
|
|
|
2005-03-24 13:58:52 +08:00
|
|
|
size_t
|
2002-06-13 18:20:48 +08:00
|
|
|
__mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp)
|
|
|
|
{
|
|
|
|
# undef fwrite
|
2003-01-26 14:38:43 +08:00
|
|
|
if ((_osver & 0x8000) && __mingw_fseek_called)
|
2002-06-13 18:20:48 +08:00
|
|
|
{
|
2004-06-30 12:51:18 +08:00
|
|
|
ULARGE_INTEGER actual_length;
|
|
|
|
LARGE_INTEGER current_position = {{0LL}};
|
2002-06-13 18:20:48 +08:00
|
|
|
__mingw_fseek_called = 0;
|
|
|
|
fflush (fp);
|
2004-06-30 12:51:18 +08:00
|
|
|
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;
|
|
|
|
|
2002-06-13 18:20:48 +08:00
|
|
|
#ifdef DEBUG
|
2004-06-30 12:51:18 +08:00
|
|
|
printf ("__mingw_fwrite: current %I64u, actual %I64u\n",
|
|
|
|
current_position.QuadPart, actual_length.QuadPart);
|
2002-06-13 18:20:48 +08:00
|
|
|
#endif /* DEBUG */
|
2004-06-30 12:51:18 +08:00
|
|
|
if (current_position.QuadPart > actual_length.QuadPart)
|
2002-06-13 18:20:48 +08:00
|
|
|
{
|
|
|
|
static char __mingw_zeros[ZEROBLOCKSIZE];
|
2004-06-30 12:51:18 +08:00
|
|
|
long long numleft;
|
2002-06-13 18:20:48 +08:00
|
|
|
|
|
|
|
SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
|
|
|
|
0, 0, FILE_END);
|
2004-06-30 12:51:18 +08:00
|
|
|
numleft = current_position.QuadPart - actual_length.QuadPart;
|
2002-06-13 18:20:48 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2004-06-30 12:51:18 +08:00
|
|
|
printf ("__mingw_fwrite: Seeking %I64d bytes past end\n", numleft);
|
2002-06-13 18:20:48 +08:00
|
|
|
#endif /* DEBUG */
|
2004-06-30 12:51:18 +08:00
|
|
|
while (numleft > 0LL)
|
2002-06-13 18:20:48 +08:00
|
|
|
{
|
|
|
|
DWORD nzeros = (numleft > ZEROBLOCKSIZE)
|
|
|
|
? ZEROBLOCKSIZE : numleft;
|
|
|
|
DWORD written;
|
|
|
|
if (! WriteFile ((HANDLE) _get_osfhandle (fileno (fp)),
|
|
|
|
__mingw_zeros, nzeros, &written, NULL))
|
|
|
|
{
|
|
|
|
/* Best we can hope for, or at least DJ says so. */
|
|
|
|
SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
|
|
|
|
0, 0, FILE_BEGIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (written < nzeros)
|
|
|
|
{
|
|
|
|
/* Likewise. */
|
|
|
|
SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
|
|
|
|
0, 0, FILE_BEGIN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
numleft -= written;
|
|
|
|
}
|
|
|
|
FlushFileBuffers ((HANDLE) _get_osfhandle (fileno (fp)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fwrite (buffer, size, count, fp);
|
|
|
|
}
|