1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6 #include <windows.h>
7 #include <stdio.h>
8 #include <io.h>
9 #include <stdlib.h>
10
11 #define ZEROBLOCKSIZE 512
12 static int __mingw_fseek_called;
13
14 int __mingw_fseek (FILE *fp, int offset, int whence);
15
16 int
__mingw_fseek(FILE * fp,int offset,int whence)17 __mingw_fseek (FILE *fp, int offset, int whence)
18 {
19 # undef fseek
20 __mingw_fseek_called = 1;
21 return fseek (fp, offset, whence);
22 }
23
24 int __mingw_fseeko64 (FILE *fp, long offset, int whence);
25
26 int
__mingw_fseeko64(FILE * fp,long offset,int whence)27 __mingw_fseeko64 (FILE *fp, long offset, int whence)
28 {
29 # undef fseeko64
30 __mingw_fseek_called = 1;
31 return fseeko64 (fp, offset, whence);
32 }
33
34 size_t __mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp);
35
36 size_t
__mingw_fwrite(const void * buffer,size_t size,size_t count,FILE * fp)37 __mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp)
38 {
39 # undef fwrite
40 if ((_osver & 0x8000) && __mingw_fseek_called)
41 {
42 ULARGE_INTEGER actual_length;
43 LARGE_INTEGER current_position;
44
45 memset (¤t_position, 0, sizeof (LARGE_INTEGER));
46 __mingw_fseek_called = 0;
47 fflush (fp);
48 actual_length.LowPart = GetFileSize ((HANDLE) _get_osfhandle (fileno (fp)),
49 &actual_length.HighPart);
50 if (actual_length.LowPart == 0xFFFFFFFF
51 && GetLastError() != NO_ERROR )
52 return -1;
53 current_position.LowPart = SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
54 current_position.LowPart,
55 ¤t_position.HighPart,
56 FILE_CURRENT);
57 if (current_position.LowPart == 0xFFFFFFFF
58 && GetLastError() != NO_ERROR )
59 return -1;
60
61 #ifdef DEBUG
62 printf ("__mingw_fwrite: current %I64u, actual %I64u\n",
63 current_position.QuadPart, actual_length.QuadPart);
64 #endif /* DEBUG */
65 if ((size_t)current_position.QuadPart > (size_t)actual_length.QuadPart)
66 {
67 static char __mingw_zeros[ZEROBLOCKSIZE];
68 long long numleft;
69
70 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
71 0, 0, FILE_END);
72 numleft = current_position.QuadPart - actual_length.QuadPart;
73
74 #ifdef DEBUG
75 printf ("__mingw_fwrite: Seeking %I64d bytes past end\n", numleft);
76 #endif /* DEBUG */
77 while (numleft > 0LL)
78 {
79 DWORD nzeros = (numleft > ZEROBLOCKSIZE)
80 ? ZEROBLOCKSIZE : numleft;
81 DWORD written;
82 if (! WriteFile ((HANDLE) _get_osfhandle (fileno (fp)),
83 __mingw_zeros, nzeros, &written, NULL))
84 {
85 /* Best we can hope for, or at least DJ says so. */
86 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
87 0, 0, FILE_BEGIN);
88 return -1;
89 }
90 if (written < nzeros)
91 {
92 /* Likewise. */
93 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
94 0, 0, FILE_BEGIN);
95 return -1;
96 }
97
98 numleft -= written;
99 }
100 FlushFileBuffers ((HANDLE) _get_osfhandle (fileno (fp)));
101 }
102 }
103 return fwrite (buffer, size, count, fp);
104 }
105