2 * Workaround for limitations on win9x where a file contents are
3 * not zero'd out if you seek past the end and then write.
4 * Copied from ming local-patch to binutils/bfd/libbfd.c written by
5 * Mumit Khan <khan@xraylith.wisc.edu>
13 #define ZEROBLOCKSIZE 512
14 static int __mingw_fseek_called;
16 /* The fseek in Win9x runtime does not zero out the file if seeking past
17 the end; if you don't want random stuff from your disk included in your
18 output DLL/executable, use this version instead. On WinNT/Win2k, it
19 just calls runtime fseek().
21 CHECK/FIXME: Does this work for both text and binary modes?? */
25 __mingw_fseek (FILE *fp, long offset, int whence)
28 __mingw_fseek_called = 1;
29 return fseek (fp, offset, whence);
33 __mingw_fseeko64 (FILE *fp, off64_t offset, int whence)
36 __mingw_fseek_called = 1;
37 return fseeko64 (fp, offset, whence);
41 __mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp)
44 if ((_osver & 0x8000) && __mingw_fseek_called)
46 ULARGE_INTEGER actual_length;
47 LARGE_INTEGER current_position = {{0LL}};
48 __mingw_fseek_called = 0;
50 actual_length.u.LowPart = GetFileSize ((HANDLE) _get_osfhandle (fileno (fp)),
51 &actual_length.u.HighPart);
52 if (actual_length.u.LowPart == 0xFFFFFFFF
53 && GetLastError() != NO_ERROR )
55 current_position.u.LowPart = SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
56 current_position.u.LowPart,
57 ¤t_position.u.HighPart,
59 if (current_position.u.LowPart == 0xFFFFFFFF
60 && GetLastError() != NO_ERROR )
64 printf ("__mingw_fwrite: current %I64u, actual %I64u\n",
65 current_position.QuadPart, actual_length.QuadPart);
67 if (current_position.QuadPart > actual_length.QuadPart)
69 static char __mingw_zeros[ZEROBLOCKSIZE];
72 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
74 numleft = current_position.QuadPart - actual_length.QuadPart;
77 printf ("__mingw_fwrite: Seeking %I64d bytes past end\n", numleft);
81 DWORD nzeros = (numleft > ZEROBLOCKSIZE)
82 ? ZEROBLOCKSIZE : numleft;
84 if (! WriteFile ((HANDLE) _get_osfhandle (fileno (fp)),
85 __mingw_zeros, nzeros, &written, NULL))
87 /* Best we can hope for, or at least DJ says so. */
88 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
95 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
102 FlushFileBuffers ((HANDLE) _get_osfhandle (fileno (fp)));
105 return (fwrite) (buffer, size, count, fp);