diff options
Diffstat (limited to '3rdparty/lua/src/loslib.c')
-rw-r--r-- | 3rdparty/lua/src/loslib.c | 93 |
1 files changed, 63 insertions, 30 deletions
diff --git a/3rdparty/lua/src/loslib.c b/3rdparty/lua/src/loslib.c index 41aeeef81d1..20359b24747 100644 --- a/3rdparty/lua/src/loslib.c +++ b/3rdparty/lua/src/loslib.c @@ -1,9 +1,14 @@ /* -** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ +** $Id: loslib.c,v 1.54 2014/12/26 14:46:07 roberto Exp $ ** Standard Operating System library ** See Copyright Notice in lua.h */ +#define loslib_c +#define LUA_LIB + +#include "lprefix.h" + #include <errno.h> #include <locale.h> @@ -11,69 +16,96 @@ #include <string.h> #include <time.h> -#define loslib_c -#define LUA_LIB - #include "lua.h" #include "lauxlib.h" #include "lualib.h" +#if !defined(LUA_STRFTIMEOPTIONS) /* { */ /* ** list of valid conversion specifiers for the 'strftime' function */ -#if !defined(LUA_STRFTIMEOPTIONS) -#if !defined(LUA_USE_POSIX) +#if defined(LUA_USE_C89) #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" } -#else +#else /* C99 specification */ #define LUA_STRFTIMEOPTIONS \ - { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \ - "", "E", "cCxXyY", \ + { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \ + "E", "cCxXyY", \ "O", "deHImMSuUVwWy" } #endif -#endif +#endif /* } */ + + + +#if !defined(l_time_t) /* { */ +/* +** type to represent time_t in Lua +*/ +#define l_timet lua_Integer +#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) +#define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a)) + +#endif /* } */ +#if !defined(lua_tmpnam) /* { */ /* ** By default, Lua uses tmpnam except when POSIX is available, where it ** uses mkstemp. */ -#if defined(LUA_USE_MKSTEMP) + +#if defined(LUA_USE_POSIX) /* { */ + #include <unistd.h> + #define LUA_TMPNAMBUFSIZE 32 + +#if !defined(LUA_TMPNAMTEMPLATE) +#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" +#endif + #define lua_tmpnam(b,e) { \ - strcpy(b, "/tmp/lua_XXXXXX"); \ + strcpy(b, LUA_TMPNAMTEMPLATE); \ e = mkstemp(b); \ if (e != -1) close(e); \ e = (e == -1); } -#elif !defined(lua_tmpnam) +#else /* }{ */ +/* ISO C definitions */ #define LUA_TMPNAMBUFSIZE L_tmpnam #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } -#endif +#endif /* } */ +#endif /* } */ + + +#if !defined(l_gmtime) /* { */ /* ** By default, Lua uses gmtime/localtime, except when POSIX is available, ** where it uses gmtime_r/localtime_r */ -#if defined(LUA_USE_GMTIME_R) + +#if defined(LUA_USE_POSIX) /* { */ #define l_gmtime(t,r) gmtime_r(t,r) #define l_localtime(t,r) localtime_r(t,r) -#elif !defined(l_gmtime) +#else /* }{ */ -#define l_gmtime(t,r) ((void)r, gmtime(t)) -#define l_localtime(t,r) ((void)r, localtime(t)) +/* ISO C definitions */ +#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) -#endif +#endif /* } */ + +#endif /* } */ @@ -120,7 +152,7 @@ static int os_getenv (lua_State *L) { static int os_clock (lua_State *L) { - lua_pushnumber(L, ((lua_Number)(1.0*clock()))/(lua_Number)CLOCKS_PER_SEC); + lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); return 1; } @@ -147,8 +179,7 @@ static void setboolfield (lua_State *L, const char *key, int value) { static int getboolfield (lua_State *L, const char *key) { int res; - lua_getfield(L, -1, key); - res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); lua_pop(L, 1); return res; } @@ -160,7 +191,7 @@ static int getfield (lua_State *L, const char *key, int d) { res = (int)lua_tointegerx(L, -1, &isnum); if (!isnum) { if (d < 0) - return luaL_error(L, "field " LUA_QS " missing in date table", key); + return luaL_error(L, "field '%s' missing in date table", key); res = d; } lua_pop(L, 1); @@ -194,11 +225,11 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); - time_t t = luaL_opt(L, luaL_checknumber, 2, time(NULL)); + time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); - s++; /* skip `!' */ + s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); @@ -255,17 +286,19 @@ static int os_time (lua_State *L) { ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); } - if (t == (time_t)(-1)) + if (t != (time_t)(l_timet)t) + luaL_error(L, "time result cannot be represented in this Lua instalation"); + else if (t == (time_t)(-1)) lua_pushnil(L); else - lua_pushnumber(L, (lua_Number)t); + l_pushtime(L, t); return 1; } static int os_difftime (lua_State *L) { - lua_pushnumber(L, difftime((luaL_checknumber(L, 1)), - (luaL_optnumber(L, 2, 0)))); + double res = difftime((l_checktime(L, 1)), (l_checktime(L, 2))); + lua_pushnumber(L, (lua_Number)res); return 1; } @@ -289,7 +322,7 @@ static int os_exit (lua_State *L) { if (lua_isboolean(L, 1)) status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); else - status = luaL_optint(L, 1, EXIT_SUCCESS); + status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); if (lua_toboolean(L, 2)) lua_close(L); if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ |