88
99#ifdef _WIN32
1010 #include <windows.h>
11+ #include <io.h>
1112#endif
1213
1314#ifndef _WIN32
@@ -66,7 +67,12 @@ static void open_call(HkState *state, HkValue *args)
6667 hk_return_if_not_ok (state );
6768 HkString * filename = hk_as_string (args [1 ]);
6869 HkString * mode = hk_as_string (args [2 ]);
69- FILE * stream = fopen (filename -> chars , mode -> chars );
70+ FILE * stream = NULL ;
71+ #ifdef _WIN32
72+ (void ) fopen_s (& stream , filename -> chars , mode -> chars );
73+ #else
74+ stream = fopen (filename -> chars , mode -> chars );
75+ #endif
7076 if (!stream )
7177 {
7278 hk_state_push_nil (state );
@@ -133,11 +139,13 @@ static void sync_call(HkState *state, HkValue *args)
133139 hk_state_check_argument_userdata (state , args , 1 );
134140 hk_return_if_not_ok (state );
135141 FILE * stream = ((File * ) hk_as_userdata (args [1 ]))-> stream ;
136- int fd = fileno (stream );
137142 bool result ;
138143#ifdef _WIN32
139- result = FlushFileBuffers (fd );
144+ int fd = _fileno (stream );
145+ HANDLE handle = (HANDLE ) _get_osfhandle (fd );
146+ result = FlushFileBuffers (handle );
140147#else
148+ int fd = fileno (stream );
141149 result = !fsync (fd );
142150#endif
143151 hk_state_push_bool (state , result );
@@ -169,7 +177,7 @@ static void seek_call(HkState *state, HkValue *args)
169177 hk_state_check_argument_int (state , args , 3 );
170178 hk_return_if_not_ok (state );
171179 FILE * stream = ((File * ) hk_as_userdata (args [1 ]))-> stream ;
172- int64_t offset = (int64_t ) hk_as_number (args [2 ]);
180+ long offset = (long ) hk_as_number (args [2 ]);
173181 int whence = (int ) hk_as_number (args [3 ]);
174182 hk_state_push_number (state , fseek (stream , offset , whence ));
175183}
@@ -181,7 +189,7 @@ static void read_call(HkState *state, HkValue *args)
181189 hk_state_check_argument_int (state , args , 2 );
182190 hk_return_if_not_ok (state );
183191 FILE * stream = ((File * ) hk_as_userdata (args [1 ]))-> stream ;
184- int64_t size = (int64_t ) hk_as_number (args [2 ]);
192+ int size = (int ) hk_as_number (args [2 ]);
185193 HkString * str = hk_string_new_with_capacity (size );
186194 int length = (int ) fread (str -> chars , 1 , size , stream );
187195 if (length < size && !feof (stream ))
@@ -205,8 +213,8 @@ static void write_call(HkState *state, HkValue *args)
205213 hk_return_if_not_ok (state );
206214 FILE * stream = ((File * ) hk_as_userdata (args [1 ]))-> stream ;
207215 HkString * str = hk_as_string (args [2 ]);
208- size_t size = str -> length ;
209- if (fwrite (str -> chars , 1 , size , stream ) < size )
216+ int size = str -> length ;
217+ if (( int ) fwrite (str -> chars , 1 , size , stream ) < size )
210218 {
211219 hk_state_push_nil (state );
212220 return ;
@@ -230,8 +238,9 @@ static void writeln_call(HkState *state, HkValue *args)
230238 hk_return_if_not_ok (state );
231239 FILE * stream = ((File * ) hk_as_userdata (args [1 ]))-> stream ;
232240 HkString * str = hk_as_string (args [2 ]);
233- size_t size = str -> length ;
234- if (fwrite (str -> chars , 1 , size , stream ) < size || fwrite ("\n" , 1 , 1 , stream ) < 1 )
241+ int size = str -> length ;
242+ if ((int ) fwrite (str -> chars , 1 , size , stream ) < size
243+ || fwrite ("\n" , 1 , 1 , stream ) < 1 )
235244 {
236245 hk_state_push_nil (state );
237246 return ;
0 commit comments