diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-09-13 18:09:04 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-09-13 18:09:04 -0300 |
commit | 5ea8f108e123317b27b7b4b22209aab19d781605 (patch) | |
tree | 2a819c1d36baa38f7fb22ab2ca601e8b7f479c5d /liolib.c | |
parent | 2275030b86f1f4ec1bfbc458b922b08aa259e7da (diff) | |
download | lua-5ea8f108e123317b27b7b4b22209aab19d781605.tar.gz lua-5ea8f108e123317b27b7b4b22209aab19d781605.tar.bz2 lua-5ea8f108e123317b27b7b4b22209aab19d781605.zip |
using 'long' versions of 'fseek'/'ftell' when available
Diffstat (limited to 'liolib.c')
-rw-r--r-- | liolib.c | 63 |
1 files changed, 57 insertions, 6 deletions
@@ -1,10 +1,20 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.102 2011/07/28 18:41:15 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.103 2011/08/02 18:00:01 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | /* | ||
9 | ** POSIX idiosyncrasy! | ||
10 | ** This definition must come before the inclusion of 'stdio.h'; it | ||
11 | ** should not affect non-POSIX systems | ||
12 | */ | ||
13 | #if !defined(_FILE_OFFSET_BITS) | ||
14 | #define _FILE_OFFSET_BITS 64 | ||
15 | #endif | ||
16 | |||
17 | |||
8 | #include <errno.h> | 18 | #include <errno.h> |
9 | #include <stdio.h> | 19 | #include <stdio.h> |
10 | #include <stdlib.h> | 20 | #include <stdlib.h> |
@@ -21,9 +31,12 @@ | |||
21 | 31 | ||
22 | 32 | ||
23 | /* | 33 | /* |
24 | ** lua_popen spawns a new process connected to the current one through | 34 | ** {====================================================== |
25 | ** the file streams. | 35 | ** lua_popen spawns a new process connected to the current |
36 | ** one through the file streams. | ||
37 | ** ======================================================= | ||
26 | */ | 38 | */ |
39 | |||
27 | #if !defined(lua_popen) /* { */ | 40 | #if !defined(lua_popen) /* { */ |
28 | 41 | ||
29 | #if defined(LUA_USE_POPEN) /* { */ | 42 | #if defined(LUA_USE_POPEN) /* { */ |
@@ -48,6 +61,41 @@ | |||
48 | 61 | ||
49 | #endif /* } */ | 62 | #endif /* } */ |
50 | 63 | ||
64 | /* }====================================================== */ | ||
65 | |||
66 | |||
67 | /* | ||
68 | ** {====================================================== | ||
69 | ** lua_fseek/lua_ftell: configuration for longer offsets | ||
70 | ** ======================================================= | ||
71 | */ | ||
72 | |||
73 | #if !defined(lua_fseek) /* { */ | ||
74 | |||
75 | #if defined(LUA_USE_POSIX) | ||
76 | |||
77 | #define l_fseek(f,o,w) fseeko(f,o,w) | ||
78 | #define l_ftell(f) ftello(f) | ||
79 | #define l_seeknum off_t | ||
80 | |||
81 | #elif defined(LUA_WIN) | ||
82 | |||
83 | #define l_fseek(f,o,w) _fseeki64(f,o,w) | ||
84 | #define l_ftell(f) _ftelli64(f) | ||
85 | #define l_seeknum __int64 | ||
86 | |||
87 | #else | ||
88 | |||
89 | #define l_fseek(f,o,w) fseek(f,o,w) | ||
90 | #define l_ftell(f) ftell(f) | ||
91 | #define l_seeknum long | ||
92 | |||
93 | #endif | ||
94 | |||
95 | #endif /* } */ | ||
96 | |||
97 | /* }====================================================== */ | ||
98 | |||
51 | 99 | ||
52 | #define IO_PREFIX "_IO_" | 100 | #define IO_PREFIX "_IO_" |
53 | #define IO_INPUT (IO_PREFIX "input") | 101 | #define IO_INPUT (IO_PREFIX "input") |
@@ -492,12 +540,15 @@ static int f_seek (lua_State *L) { | |||
492 | static const char *const modenames[] = {"set", "cur", "end", NULL}; | 540 | static const char *const modenames[] = {"set", "cur", "end", NULL}; |
493 | FILE *f = tofile(L); | 541 | FILE *f = tofile(L); |
494 | int op = luaL_checkoption(L, 2, "cur", modenames); | 542 | int op = luaL_checkoption(L, 2, "cur", modenames); |
495 | long offset = luaL_optlong(L, 3, 0); | 543 | lua_Number p3 = luaL_optnumber(L, 3, 0); |
496 | op = fseek(f, offset, mode[op]); | 544 | l_seeknum offset = (l_seeknum)p3; |
545 | luaL_argcheck(L, (lua_Number)offset == p3, 3, | ||
546 | "not an integer in proper range"); | ||
547 | op = l_fseek(f, offset, mode[op]); | ||
497 | if (op) | 548 | if (op) |
498 | return luaL_fileresult(L, 0, NULL); /* error */ | 549 | return luaL_fileresult(L, 0, NULL); /* error */ |
499 | else { | 550 | else { |
500 | lua_pushinteger(L, ftell(f)); | 551 | lua_pushnumber(L, (lua_Number)l_ftell(f)); |
501 | return 1; | 552 | return 1; |
502 | } | 553 | } |
503 | } | 554 | } |