From 7948b8568e226f609871918a1d614450a1a565a7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Nov 2011 15:15:42 -0200 Subject: [PATCH] when available, use '*_r' versions of 'gmtime' and 'localtime' --- loslib.c | 25 +++++++++++++++++++++---- luaconf.h | 3 ++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/loslib.c b/loslib.c index c7ec4215..b9f234f1 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.35 2011/06/20 16:50:59 roberto Exp roberto $ +** $Id: loslib.c,v 1.36 2011/11/29 15:55:51 roberto Exp roberto $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -58,6 +58,23 @@ #endif +/* +** By default, Lua uses gmtime/localtime, except when POSIX is available, +** where it uses gmtime_r/localtime_r +*/ +#if defined(lUA_USE_GMTIME_R) + +#define l_gmtime(t,r) gmtime_r(t,r) +#define l_localtime(t,r) localtime_r(t,r) + +#elif !defined(l_gmtime) + +#define l_gmtime(t,r) ((void)r, gmtime(t)) +#define l_localtime(t,r) ((void)r, localtime(t)) + +#endif + + static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); @@ -177,13 +194,13 @@ 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, (time_t)luaL_checknumber, 2, time(NULL)); - struct tm *stm; + struct tm tmr, *stm; if (*s == '!') { /* UTC? */ - stm = gmtime(&t); + stm = l_gmtime(&t, &tmr); s++; /* skip `!' */ } else - stm = localtime(&t); + stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ lua_pushnil(L); else if (strcmp(s, "*t") == 0) { diff --git a/luaconf.h b/luaconf.h index b04a704d..166b714e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.166 2011/11/09 14:47:14 roberto Exp roberto $ +** $Id: luaconf.h,v 1.167 2011/11/25 12:52:27 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -70,6 +70,7 @@ #define LUA_USE_ISATTY #define LUA_USE_POPEN #define LUA_USE_ULONGJMP +#define lUA_USE_GMTIME_R #endif