summaryrefslogtreecommitdiff
path: root/src/lib_io.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib_io.c')
-rw-r--r--src/lib_io.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/src/lib_io.c b/src/lib_io.c
index 307d005c..66b174f8 100644
--- a/src/lib_io.c
+++ b/src/lib_io.c
@@ -195,7 +195,7 @@ static int io_file_readchars(lua_State *L, FILE *fp, size_t n)
195 195
196static int io_file_read(lua_State *L, FILE *fp, int start) 196static int io_file_read(lua_State *L, FILE *fp, int start)
197{ 197{
198 int ok, n, nargs = cast_int(L->top - L->base) - start; 198 int ok, n, nargs = (int)(L->top - L->base) - start;
199 clearerr(fp); 199 clearerr(fp);
200 if (nargs == 0) { 200 if (nargs == 0) {
201 ok = io_file_readline(L, fp); 201 ok = io_file_readline(L, fp);
@@ -240,10 +240,15 @@ static int io_file_write(lua_State *L, FILE *fp, int start)
240 if (tvisstr(tv)) { 240 if (tvisstr(tv)) {
241 MSize len = strV(tv)->len; 241 MSize len = strV(tv)->len;
242 status = status && (fwrite(strVdata(tv), 1, len, fp) == len); 242 status = status && (fwrite(strVdata(tv), 1, len, fp) == len);
243 } else if (tvisint(tv)) {
244 char buf[LJ_STR_INTBUF];
245 char *p = lj_str_bufint(buf, intV(tv));
246 size_t len = (size_t)(buf+LJ_STR_INTBUF-p);
247 status = status && (fwrite(p, 1, len, fp) == len);
243 } else if (tvisnum(tv)) { 248 } else if (tvisnum(tv)) {
244 status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0); 249 status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0);
245 } else { 250 } else {
246 lj_err_argt(L, cast_int(tv - L->base) + 1, LUA_TSTRING); 251 lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING);
247 } 252 }
248 } 253 }
249 return io_pushresult(L, status, NULL); 254 return io_pushresult(L, status, NULL);
@@ -279,37 +284,42 @@ LJLIB_CF(io_method_seek)
279{ 284{
280 FILE *fp = io_tofile(L)->fp; 285 FILE *fp = io_tofile(L)->fp;
281 int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end"); 286 int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end");
282 lua_Number ofs; 287 int64_t ofs = 0;
288 cTValue *o;
283 int res; 289 int res;
284 if (opt == 0) opt = SEEK_SET; 290 if (opt == 0) opt = SEEK_SET;
285 else if (opt == 1) opt = SEEK_CUR; 291 else if (opt == 1) opt = SEEK_CUR;
286 else if (opt == 2) opt = SEEK_END; 292 else if (opt == 2) opt = SEEK_END;
287 lj_lib_opt(L, 3, 293 o = L->base+2;
288 ofs = lj_lib_checknum(L, 3); 294 if (o < L->top) {
289 , 295 if (tvisint(o))
290 ofs = 0; 296 ofs = (int64_t)intV(o);
291 ) 297 else if (tvisnum(o))
298 ofs = (int64_t)numV(o);
299 else if (!tvisnil(o))
300 lj_err_argt(L, 3, LUA_TNUMBER);
301 }
292#if LJ_TARGET_POSIX 302#if LJ_TARGET_POSIX
293 res = fseeko(fp, (int64_t)ofs, opt); 303 res = fseeko(fp, ofs, opt);
294#elif _MSC_VER >= 1400 304#elif _MSC_VER >= 1400
295 res = _fseeki64(fp, (int64_t)ofs, opt); 305 res = _fseeki64(fp, ofs, opt);
296#elif defined(__MINGW32__) 306#elif defined(__MINGW32__)
297 res = fseeko64(fp, (int64_t)ofs, opt); 307 res = fseeko64(fp, ofs, opt);
298#else 308#else
299 res = fseek(fp, (long)ofs, opt); 309 res = fseek(fp, (long)ofs, opt);
300#endif 310#endif
301 if (res) 311 if (res)
302 return io_pushresult(L, 0, NULL); 312 return io_pushresult(L, 0, NULL);
303#if LJ_TARGET_POSIX 313#if LJ_TARGET_POSIX
304 ofs = cast_num(ftello(fp)); 314 ofs = ftello(fp);
305#elif _MSC_VER >= 1400 315#elif _MSC_VER >= 1400
306 ofs = cast_num(_ftelli64(fp)); 316 ofs = _ftelli64(fp);
307#elif defined(__MINGW32__) 317#elif defined(__MINGW32__)
308 ofs = cast_num(ftello64(fp)); 318 ofs = ftello64(fp);
309#else 319#else
310 ofs = cast_num(ftell(fp)); 320 ofs = (int64_t)ftell(fp);
311#endif 321#endif
312 setnumV(L->top-1, ofs); 322 setint64V(L->top-1, ofs);
313 return 1; 323 return 1;
314} 324}
315 325