31#if !defined(LUA_STRFTIMEOPTIONS)
33#if defined(LUA_USE_WINDOWS)
34#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
35 "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y"
36#elif defined(LUA_USE_C89)
37#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
39#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
40 "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy"
56#if !defined(LUA_NUMTIME)
58#define l_timet lua_Integer
59#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
60#define l_gettime(L,arg) luaL_checkinteger(L, arg)
64#define l_timet lua_Number
65#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
66#define l_gettime(L,arg) luaL_checknumber(L, arg)
77#if defined(LUA_USE_POSIX)
79#define l_gmtime(t,r) gmtime_r(t,r)
80#define l_localtime(t,r) localtime_r(t,r)
85#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
86#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
102#if !defined(lua_tmpnam)
104#if defined(LUA_USE_POSIX)
108#define LUA_TMPNAMBUFSIZE 32
110#if !defined(LUA_TMPNAMTEMPLATE)
111#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
114#define lua_tmpnam(b,e) { \
115 strcpy(b, LUA_TMPNAMTEMPLATE); \
117 if (e != -1) close(e); \
123#define LUA_TMPNAMBUFSIZE L_tmpnam
124#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
132#if !defined(l_system)
133#if defined(LUA_USE_IOS)
135#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
137#define l_system(cmd) system(cmd)
143 const char *cmd = luaL_optstring(L, 1, NULL);
146 stat = l_system(cmd);
148 return luaL_execresult(L, stat);
150 lua_pushboolean(L, stat);
157 const char *filename = luaL_checkstring(L, 1);
158 return luaL_fileresult(L, remove(filename) == 0, filename);
163 const char *fromname = luaL_checkstring(L, 1);
164 const char *toname = luaL_checkstring(L, 2);
165 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
170 char buff[LUA_TMPNAMBUFSIZE];
172 lua_tmpnam(buff, err);
174 return luaL_error(L,
"unable to generate a unique filename");
175 lua_pushstring(L, buff);
181 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
187 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
209static void setfield (
lua_State *L,
const char *key,
int value,
int delta) {
210 #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
211 if (l_unlikely(value > LUA_MAXINTEGER - delta))
212 luaL_error(L,
"field '%s' is out-of-bound", key);
214 lua_pushinteger(L, (lua_Integer)value + delta);
215 lua_setfield(L, -2, key);
219static void setboolfield (
lua_State *L,
const char *key,
int value) {
222 lua_pushboolean(L, value);
223 lua_setfield(L, -2, key);
230static void setallfields (
lua_State *L,
struct tm *stm) {
231 setfield(L,
"year", stm->tm_year, 1900);
232 setfield(L,
"month", stm->tm_mon, 1);
233 setfield(L,
"day", stm->tm_mday, 0);
234 setfield(L,
"hour", stm->tm_hour, 0);
235 setfield(L,
"min", stm->tm_min, 0);
236 setfield(L,
"sec", stm->tm_sec, 0);
237 setfield(L,
"yday", stm->tm_yday, 1);
238 setfield(L,
"wday", stm->tm_wday, 1);
239 setboolfield(L,
"isdst", stm->tm_isdst);
243static int getboolfield (
lua_State *L,
const char *key) {
245 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
251static int getfield (
lua_State *L,
const char *key,
int d,
int delta) {
253 int t = lua_getfield(L, -1, key);
254 lua_Integer res = lua_tointegerx(L, -1, &isnum);
256 if (l_unlikely(t != LUA_TNIL))
257 return luaL_error(L,
"field '%s' is not an integer", key);
258 else if (l_unlikely(d < 0))
259 return luaL_error(L,
"field '%s' missing in date table", key);
263 if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
264 return luaL_error(L,
"field '%s' is out-of-bound", key);
272static const char *checkoption (
lua_State *L,
const char *conv,
273 ptrdiff_t convlen,
char *buff) {
274 const char *option = LUA_STRFTIMEOPTIONS;
276 for (; *option !=
'\0' && oplen <= convlen; option += oplen) {
279 else if (memcmp(conv, option, oplen) == 0) {
280 memcpy(buff, conv, oplen);
286 lua_pushfstring(L,
"invalid conversion specifier '%%%s'", conv));
291static time_t l_checktime (
lua_State *L,
int arg) {
292 l_timet t = l_gettime(L, arg);
293 luaL_argcheck(L, (time_t)t == t, arg,
"time out-of-bounds");
299#define SIZETIMEFMT 250
304 const char *s = luaL_optlstring(L, 1,
"%c", &slen);
305 time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
306 const char *se = s + slen;
309 stm = l_gmtime(&t, &tmr);
313 stm = l_localtime(&t, &tmr);
316 "date result cannot be represented in this installation");
317 if (strcmp(s,
"*t") == 0) {
318 lua_createtable(L, 0, 9);
319 setallfields(L, stm);
325 luaL_buffinit(L, &b);
328 luaL_addchar(&b, *s++);
331 char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
333 s = checkoption(L, s, se - s, cc + 1);
334 reslen = strftime(buff, SIZETIMEFMT, cc, stm);
335 luaL_addsize(&b, reslen);
346 if (lua_isnoneornil(L, 1))
350 luaL_checktype(L, 1, LUA_TTABLE);
352 ts.tm_year = getfield(L,
"year", -1, 1900);
353 ts.tm_mon = getfield(L,
"month", -1, 1);
354 ts.tm_mday = getfield(L,
"day", -1, 0);
355 ts.tm_hour = getfield(L,
"hour", 12, 0);
356 ts.tm_min = getfield(L,
"min", 0, 0);
357 ts.tm_sec = getfield(L,
"sec", 0, 0);
358 ts.tm_isdst = getboolfield(L,
"isdst");
360 setallfields(L, &ts);
362 if (t != (time_t)(l_timet)t || t == (time_t)(-1))
364 "time result cannot be represented in this installation");
371 time_t t1 = l_checktime(L, 1);
372 time_t t2 = l_checktime(L, 2);
373 lua_pushnumber(L, (lua_Number)difftime(t1, t2));
381 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
382 LC_NUMERIC, LC_TIME};
383 static const char *
const catnames[] = {
"all",
"collate",
"ctype",
"monetary",
384 "numeric",
"time", NULL};
385 const char *l = luaL_optstring(L, 1, NULL);
386 int op = luaL_checkoption(L, 2,
"all", catnames);
387 lua_pushstring(L, setlocale(cat[op], l));
394 if (lua_isboolean(L, 1))
395 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
397 status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
398 if (lua_toboolean(L, 2))
408 {
"difftime", os_difftime},
409 {
"execute", os_execute},
411 {
"getenv", os_getenv},
412 {
"remove", os_remove},
413 {
"rename", os_rename},
414 {
"setlocale", os_setlocale},
416 {
"tmpname", os_tmpname},
424LUAMOD_API
int luaopen_os (
lua_State *L) {
425 luaL_newlib(L, syslib);