2004-07-09 19:47:48 +04:00
|
|
|
/*
|
2018-07-25 17:44:46 +03:00
|
|
|
** $Id: loslib.c $
|
2004-07-09 19:47:48 +04:00
|
|
|
** Standard Operating System library
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define loslib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
2004-07-09 19:47:48 +04:00
|
|
|
|
2020-05-22 21:39:29 +03:00
|
|
|
#include <errno.h>
|
2004-07-09 19:47:48 +04:00
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
2009-12-17 16:08:51 +03:00
|
|
|
/*
|
2015-04-10 20:41:04 +03:00
|
|
|
** {==================================================================
|
2016-02-08 17:42:46 +03:00
|
|
|
** List of valid conversion specifiers for the 'strftime' function;
|
2016-02-09 15:16:11 +03:00
|
|
|
** options are grouped by length; group of length 2 start with '||'.
|
2015-04-10 20:41:04 +03:00
|
|
|
** ===================================================================
|
2009-12-17 16:08:51 +03:00
|
|
|
*/
|
2015-04-10 20:41:04 +03:00
|
|
|
#if !defined(LUA_STRFTIMEOPTIONS) /* { */
|
2009-12-17 16:08:51 +03:00
|
|
|
|
2016-02-08 17:42:46 +03:00
|
|
|
#if defined(LUA_USE_WINDOWS)
|
2023-02-07 16:48:39 +03:00
|
|
|
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \
|
|
|
|
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
|
|
|
|
#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */
|
|
|
|
#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%"
|
2014-12-10 18:42:42 +03:00
|
|
|
#else /* C99 specification */
|
2023-02-07 16:48:39 +03:00
|
|
|
#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
|
|
|
|
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
|
2009-12-17 16:08:51 +03:00
|
|
|
#endif
|
|
|
|
|
2014-03-20 23:18:54 +04:00
|
|
|
#endif /* } */
|
2015-04-10 20:41:04 +03:00
|
|
|
/* }================================================================== */
|
2009-12-17 16:08:51 +03:00
|
|
|
|
|
|
|
|
2015-04-10 20:41:04 +03:00
|
|
|
/*
|
|
|
|
** {==================================================================
|
|
|
|
** Configuration for time-related stuff
|
|
|
|
** ===================================================================
|
|
|
|
*/
|
2009-12-17 16:08:51 +03:00
|
|
|
|
2014-03-20 23:18:54 +04:00
|
|
|
/*
|
|
|
|
** type to represent time_t in Lua
|
|
|
|
*/
|
2019-07-24 21:01:59 +03:00
|
|
|
#if !defined(LUA_NUMTIME) /* { */
|
|
|
|
|
2014-03-20 23:18:54 +04:00
|
|
|
#define l_timet lua_Integer
|
|
|
|
#define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
|
2019-07-24 21:01:59 +03:00
|
|
|
#define l_gettime(L,arg) luaL_checkinteger(L, arg)
|
2015-07-04 19:35:14 +03:00
|
|
|
|
2019-07-24 21:01:59 +03:00
|
|
|
#else /* }{ */
|
|
|
|
|
|
|
|
#define l_timet lua_Number
|
|
|
|
#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t))
|
|
|
|
#define l_gettime(L,arg) luaL_checknumber(L, arg)
|
2014-03-20 23:18:54 +04:00
|
|
|
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
|
2015-04-10 20:41:04 +03:00
|
|
|
#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_POSIX) /* { */
|
|
|
|
|
|
|
|
#define l_gmtime(t,r) gmtime_r(t,r)
|
|
|
|
#define l_localtime(t,r) localtime_r(t,r)
|
|
|
|
|
|
|
|
#else /* }{ */
|
|
|
|
|
|
|
|
/* ISO C definitions */
|
|
|
|
#define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
|
2020-04-23 20:48:15 +03:00
|
|
|
#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
|
2015-04-10 20:41:04 +03:00
|
|
|
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
/* }================================================================== */
|
|
|
|
|
2014-03-20 23:18:54 +04:00
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
/*
|
2015-04-10 20:41:04 +03:00
|
|
|
** {==================================================================
|
|
|
|
** Configuration for 'tmpnam':
|
|
|
|
** By default, Lua uses tmpnam except when POSIX is available, where
|
|
|
|
** it uses mkstemp.
|
|
|
|
** ===================================================================
|
2009-12-17 15:26:09 +03:00
|
|
|
*/
|
2015-04-10 20:41:04 +03:00
|
|
|
#if !defined(lua_tmpnam) /* { */
|
2014-02-26 19:27:56 +04:00
|
|
|
|
|
|
|
#if defined(LUA_USE_POSIX) /* { */
|
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
#include <unistd.h>
|
2014-04-29 21:05:13 +04:00
|
|
|
|
2012-05-23 19:37:09 +04:00
|
|
|
#define LUA_TMPNAMBUFSIZE 32
|
2014-04-29 21:05:13 +04:00
|
|
|
|
|
|
|
#if !defined(LUA_TMPNAMTEMPLATE)
|
|
|
|
#define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
|
|
|
|
#endif
|
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
#define lua_tmpnam(b,e) { \
|
2014-04-29 21:05:13 +04:00
|
|
|
strcpy(b, LUA_TMPNAMTEMPLATE); \
|
2009-12-17 15:26:09 +03:00
|
|
|
e = mkstemp(b); \
|
|
|
|
if (e != -1) close(e); \
|
|
|
|
e = (e == -1); }
|
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
#else /* }{ */
|
2009-12-17 15:26:09 +03:00
|
|
|
|
2014-11-02 22:33:33 +03:00
|
|
|
/* ISO C definitions */
|
2012-05-23 19:37:09 +04:00
|
|
|
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
|
|
|
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
2009-12-17 15:26:09 +03:00
|
|
|
|
2014-02-26 19:27:56 +04:00
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
#endif /* } */
|
2015-04-10 20:41:04 +03:00
|
|
|
/* }================================================================== */
|
2009-12-17 15:26:09 +03:00
|
|
|
|
|
|
|
|
2023-01-24 21:04:17 +03:00
|
|
|
#if !defined(l_system)
|
2023-02-02 19:43:41 +03:00
|
|
|
#if defined(LUA_USE_IOS)
|
|
|
|
/* Despite claiming to be ISO C, iOS does not implement 'system'. */
|
|
|
|
#define l_system(cmd) ((cmd) == NULL ? 0 : -1)
|
|
|
|
#else
|
2023-01-24 21:04:17 +03:00
|
|
|
#define l_system(cmd) system(cmd) /* default definition */
|
|
|
|
#endif
|
2023-02-02 19:43:41 +03:00
|
|
|
#endif
|
2023-01-24 21:04:17 +03:00
|
|
|
|
2014-03-20 23:18:54 +04:00
|
|
|
|
2011-03-03 19:34:46 +03:00
|
|
|
static int os_execute (lua_State *L) {
|
|
|
|
const char *cmd = luaL_optstring(L, 1, NULL);
|
2020-05-22 21:39:29 +03:00
|
|
|
int stat;
|
|
|
|
errno = 0;
|
2023-01-24 21:04:17 +03:00
|
|
|
stat = l_system(cmd);
|
2011-03-03 19:34:46 +03:00
|
|
|
if (cmd != NULL)
|
|
|
|
return luaL_execresult(L, stat);
|
2004-07-09 19:47:48 +04:00
|
|
|
else {
|
2011-03-03 19:34:46 +03:00
|
|
|
lua_pushboolean(L, stat); /* true if there is a shell */
|
|
|
|
return 1;
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_remove (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
2011-03-03 19:34:46 +03:00
|
|
|
return luaL_fileresult(L, remove(filename) == 0, filename);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_rename (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
const char *fromname = luaL_checkstring(L, 1);
|
|
|
|
const char *toname = luaL_checkstring(L, 2);
|
2012-10-19 19:54:02 +04:00
|
|
|
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_tmpname (lua_State *L) {
|
2005-03-18 21:02:04 +03:00
|
|
|
char buff[LUA_TMPNAMBUFSIZE];
|
|
|
|
int err;
|
|
|
|
lua_tmpnam(buff, err);
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(err))
|
2005-03-18 21:02:04 +03:00
|
|
|
return luaL_error(L, "unable to generate a unique filename");
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushstring(L, buff);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_getenv (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_clock (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Time/Date operations
|
|
|
|
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
|
|
|
|
** wday=%w+1, yday=%j, isdst=? }
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2019-07-24 21:01:59 +03:00
|
|
|
/*
|
2019-12-30 17:45:08 +03:00
|
|
|
** About the overflow check: an overflow cannot occur when time
|
2019-07-24 21:01:59 +03:00
|
|
|
** is represented by a lua_Integer, because either lua_Integer is
|
|
|
|
** large enough to represent all int fields or it is not large enough
|
|
|
|
** to represent a time that cause a field to overflow. However, if
|
|
|
|
** times are represented as doubles and lua_Integer is int, then the
|
|
|
|
** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900
|
|
|
|
** to compute the year.
|
|
|
|
*/
|
|
|
|
static void setfield (lua_State *L, const char *key, int value, int delta) {
|
|
|
|
#if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(value > LUA_MAXINTEGER - delta))
|
2019-07-24 21:01:59 +03:00
|
|
|
luaL_error(L, "field '%s' is out-of-bound", key);
|
|
|
|
#endif
|
|
|
|
lua_pushinteger(L, (lua_Integer)value + delta);
|
2005-01-10 22:16:29 +03:00
|
|
|
lua_setfield(L, -2, key);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:01:59 +03:00
|
|
|
|
2004-07-09 19:47:48 +04:00
|
|
|
static void setboolfield (lua_State *L, const char *key, int value) {
|
2005-09-09 22:22:46 +04:00
|
|
|
if (value < 0) /* undefined? */
|
|
|
|
return; /* does not set field */
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushboolean(L, value);
|
2005-01-10 22:16:29 +03:00
|
|
|
lua_setfield(L, -2, key);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
2016-04-18 16:06:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Set all fields from structure 'tm' in the table on top of the stack
|
|
|
|
*/
|
|
|
|
static void setallfields (lua_State *L, struct tm *stm) {
|
2019-07-24 21:01:59 +03:00
|
|
|
setfield(L, "year", stm->tm_year, 1900);
|
|
|
|
setfield(L, "month", stm->tm_mon, 1);
|
|
|
|
setfield(L, "day", stm->tm_mday, 0);
|
|
|
|
setfield(L, "hour", stm->tm_hour, 0);
|
|
|
|
setfield(L, "min", stm->tm_min, 0);
|
|
|
|
setfield(L, "sec", stm->tm_sec, 0);
|
|
|
|
setfield(L, "yday", stm->tm_yday, 1);
|
|
|
|
setfield(L, "wday", stm->tm_wday, 1);
|
2016-04-18 16:06:55 +03:00
|
|
|
setboolfield(L, "isdst", stm->tm_isdst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-09 19:47:48 +04:00
|
|
|
static int getboolfield (lua_State *L, const char *key) {
|
|
|
|
int res;
|
2014-03-13 00:57:40 +04:00
|
|
|
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-04 19:35:14 +03:00
|
|
|
static int getfield (lua_State *L, const char *key, int d, int delta) {
|
|
|
|
int isnum;
|
2016-01-06 16:43:05 +03:00
|
|
|
int t = lua_getfield(L, -1, key); /* get field and its type */
|
2015-07-04 19:35:14 +03:00
|
|
|
lua_Integer res = lua_tointegerx(L, -1, &isnum);
|
2016-01-06 16:43:05 +03:00
|
|
|
if (!isnum) { /* field is not an integer? */
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(t != LUA_TNIL)) /* some other value? */
|
2016-01-06 16:43:05 +03:00
|
|
|
return luaL_error(L, "field '%s' is not an integer", key);
|
2021-02-24 17:14:44 +03:00
|
|
|
else if (l_unlikely(d < 0)) /* absent field; no default? */
|
2014-10-17 20:28:21 +04:00
|
|
|
return luaL_error(L, "field '%s' missing in date table", key);
|
2004-07-09 19:47:48 +04:00
|
|
|
res = d;
|
|
|
|
}
|
2015-07-04 19:35:14 +03:00
|
|
|
else {
|
2022-09-23 16:57:35 +03:00
|
|
|
if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
|
2016-01-06 16:43:05 +03:00
|
|
|
return luaL_error(L, "field '%s' is out-of-bound", key);
|
2015-07-04 19:35:14 +03:00
|
|
|
res -= delta;
|
|
|
|
}
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pop(L, 1);
|
2015-07-04 19:35:14 +03:00
|
|
|
return (int)res;
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-18 20:58:58 +03:00
|
|
|
static const char *checkoption (lua_State *L, const char *conv,
|
|
|
|
ptrdiff_t convlen, char *buff) {
|
|
|
|
const char *option = LUA_STRFTIMEOPTIONS;
|
|
|
|
int oplen = 1; /* length of options being checked */
|
|
|
|
for (; *option != '\0' && oplen <= convlen; option += oplen) {
|
2016-02-09 15:16:11 +03:00
|
|
|
if (*option == '|') /* next block? */
|
2016-07-18 20:58:58 +03:00
|
|
|
oplen++; /* will check options with next length (+1) */
|
2016-02-09 15:16:11 +03:00
|
|
|
else if (memcmp(conv, option, oplen) == 0) { /* match? */
|
|
|
|
memcpy(buff, conv, oplen); /* copy valid option to buffer */
|
2016-02-08 17:42:46 +03:00
|
|
|
buff[oplen] = '\0';
|
|
|
|
return conv + oplen; /* return next item */
|
2009-08-25 23:58:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
luaL_argerror(L, 1,
|
|
|
|
lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
|
|
|
|
return conv; /* to avoid warnings */
|
|
|
|
}
|
|
|
|
|
2011-01-26 19:30:02 +03:00
|
|
|
|
2019-07-24 21:01:59 +03:00
|
|
|
static time_t l_checktime (lua_State *L, int arg) {
|
|
|
|
l_timet t = l_gettime(L, arg);
|
|
|
|
luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
|
|
|
|
return (time_t)t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-06 18:16:51 +03:00
|
|
|
/* maximum size for an individual 'strftime' item */
|
|
|
|
#define SIZETIMEFMT 250
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_date (lua_State *L) {
|
2016-07-18 20:58:58 +03:00
|
|
|
size_t slen;
|
|
|
|
const char *s = luaL_optlstring(L, 1, "%c", &slen);
|
2014-03-20 23:18:54 +04:00
|
|
|
time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
|
2016-07-18 20:58:58 +03:00
|
|
|
const char *se = s + slen; /* 's' end */
|
2011-11-29 21:15:42 +04:00
|
|
|
struct tm tmr, *stm;
|
2004-07-09 19:47:48 +04:00
|
|
|
if (*s == '!') { /* UTC? */
|
2011-11-29 21:15:42 +04:00
|
|
|
stm = l_gmtime(&t, &tmr);
|
2014-10-25 15:50:46 +04:00
|
|
|
s++; /* skip '!' */
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
else
|
2011-11-29 21:15:42 +04:00
|
|
|
stm = l_localtime(&t, &tmr);
|
2004-07-09 19:47:48 +04:00
|
|
|
if (stm == NULL) /* invalid date? */
|
2017-03-14 15:40:44 +03:00
|
|
|
return luaL_error(L,
|
2019-07-24 21:01:59 +03:00
|
|
|
"date result cannot be represented in this installation");
|
2015-07-04 19:35:14 +03:00
|
|
|
if (strcmp(s, "*t") == 0) {
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_createtable(L, 0, 9); /* 9 = number of fields */
|
2016-04-18 16:06:55 +03:00
|
|
|
setallfields(L, stm);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
else {
|
2016-02-08 17:42:46 +03:00
|
|
|
char cc[4]; /* buffer for individual conversion specifiers */
|
2006-09-19 17:57:08 +04:00
|
|
|
luaL_Buffer b;
|
2007-05-04 00:49:29 +04:00
|
|
|
cc[0] = '%';
|
2006-09-19 17:57:08 +04:00
|
|
|
luaL_buffinit(L, &b);
|
2016-07-18 20:58:58 +03:00
|
|
|
while (s < se) {
|
2015-07-06 18:16:51 +03:00
|
|
|
if (*s != '%') /* not a conversion specifier? */
|
2009-08-25 23:58:08 +04:00
|
|
|
luaL_addchar(&b, *s++);
|
2006-09-19 17:57:08 +04:00
|
|
|
else {
|
|
|
|
size_t reslen;
|
2015-07-06 18:16:51 +03:00
|
|
|
char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
|
2016-07-18 20:58:58 +03:00
|
|
|
s++; /* skip '%' */
|
|
|
|
s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
|
2015-07-06 18:16:51 +03:00
|
|
|
reslen = strftime(buff, SIZETIMEFMT, cc, stm);
|
|
|
|
luaL_addsize(&b, reslen);
|
2006-09-19 17:57:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_time (lua_State *L) {
|
2004-08-05 23:30:37 +04:00
|
|
|
time_t t;
|
2004-07-09 19:47:48 +04:00
|
|
|
if (lua_isnoneornil(L, 1)) /* called without args? */
|
2004-08-05 23:30:37 +04:00
|
|
|
t = time(NULL); /* get current time */
|
2004-07-09 19:47:48 +04:00
|
|
|
else {
|
|
|
|
struct tm ts;
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
lua_settop(L, 1); /* make sure table is at the top */
|
2015-07-04 19:35:14 +03:00
|
|
|
ts.tm_year = getfield(L, "year", -1, 1900);
|
2019-07-24 21:01:59 +03:00
|
|
|
ts.tm_mon = getfield(L, "month", -1, 1);
|
|
|
|
ts.tm_mday = getfield(L, "day", -1, 0);
|
|
|
|
ts.tm_hour = getfield(L, "hour", 12, 0);
|
|
|
|
ts.tm_min = getfield(L, "min", 0, 0);
|
|
|
|
ts.tm_sec = getfield(L, "sec", 0, 0);
|
2004-07-09 19:47:48 +04:00
|
|
|
ts.tm_isdst = getboolfield(L, "isdst");
|
|
|
|
t = mktime(&ts);
|
2016-04-18 16:06:55 +03:00
|
|
|
setallfields(L, &ts); /* update fields with normalized values */
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
2015-07-04 19:35:14 +03:00
|
|
|
if (t != (time_t)(l_timet)t || t == (time_t)(-1))
|
2017-03-14 15:40:44 +03:00
|
|
|
return luaL_error(L,
|
|
|
|
"time result cannot be represented in this installation");
|
2015-07-04 19:35:14 +03:00
|
|
|
l_pushtime(L, t);
|
2004-07-09 19:47:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_difftime (lua_State *L) {
|
2015-02-09 20:41:54 +03:00
|
|
|
time_t t1 = l_checktime(L, 1);
|
|
|
|
time_t t2 = l_checktime(L, 2);
|
|
|
|
lua_pushnumber(L, (lua_Number)difftime(t1, t2));
|
2004-07-09 19:47:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_setlocale (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
|
|
|
|
LC_NUMERIC, LC_TIME};
|
|
|
|
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
|
|
|
|
"numeric", "time", NULL};
|
2006-04-26 22:19:49 +04:00
|
|
|
const char *l = luaL_optstring(L, 1, NULL);
|
2005-05-25 17:21:26 +04:00
|
|
|
int op = luaL_checkoption(L, 2, "all", catnames);
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushstring(L, setlocale(cat[op], l));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_exit (lua_State *L) {
|
2010-10-05 16:18:03 +04:00
|
|
|
int status;
|
|
|
|
if (lua_isboolean(L, 1))
|
|
|
|
status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
else
|
2014-10-01 15:54:56 +04:00
|
|
|
status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
|
2009-11-23 21:20:38 +03:00
|
|
|
if (lua_toboolean(L, 2))
|
2008-06-13 20:59:00 +04:00
|
|
|
lua_close(L);
|
2011-11-29 19:55:51 +04:00
|
|
|
if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
|
|
|
|
return 0;
|
2004-07-09 19:47:48 +04:00
|
|
|
}
|
|
|
|
|
2008-06-13 20:59:00 +04:00
|
|
|
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg syslib[] = {
|
2005-12-15 21:17:49 +03:00
|
|
|
{"clock", os_clock},
|
|
|
|
{"date", os_date},
|
|
|
|
{"difftime", os_difftime},
|
|
|
|
{"execute", os_execute},
|
|
|
|
{"exit", os_exit},
|
|
|
|
{"getenv", os_getenv},
|
|
|
|
{"remove", os_remove},
|
|
|
|
{"rename", os_rename},
|
|
|
|
{"setlocale", os_setlocale},
|
|
|
|
{"time", os_time},
|
|
|
|
{"tmpname", os_tmpname},
|
2004-07-09 19:47:48 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-24 15:05:44 +03:00
|
|
|
LUAMOD_API int luaopen_os (lua_State *L) {
|
2010-07-02 15:38:13 +04:00
|
|
|
luaL_newlib(L, syslib);
|
2004-07-09 19:47:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|