lua-hooks/ext/perf/luaoslib.c in immunio-1.1.2 vs lua-hooks/ext/perf/luaoslib.c in immunio-1.1.5
- old
+ new
@@ -1,20 +1,73 @@
#include <time.h>
+#include <string.h>
#define lib_os_c
#define LUA_LIB
+/*
+** list of valid conversion specifiers for the 'strftime' function
+*/
+#if !defined(LUA_STRFTIMEOPTIONS)
+
+#if !defined(LUA_USE_POSIX)
+#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
+#else
+#define LUA_STRFTIMEOPTIONS \
+ { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
+ "", "E", "cCxXyY", \
+ "O", "deHImMSuUVwWy" }
+#endif
+
+#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
+
#include "lua.h"
#include "lauxlib.h"
#include "lj_err.h"
static int os_clock(lua_State *L) {
lua_Number clk = ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC);
lua_pushnumber(L, clk);
return 1;
}
+/*
+** {======================================================
+** Time/Date operations
+** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
+** wday=%w+1, yday=%j, isdst=? }
+** =======================================================
+*/
+
+static void setfield (lua_State *L, const char *key, int value) {
+ lua_pushinteger(L, value);
+ lua_setfield(L, -2, key);
+}
+
+static void setboolfield (lua_State *L, const char *key, int value) {
+ if (value < 0) /* undefined? */
+ return; /* does not set field */
+ lua_pushboolean(L, value);
+ lua_setfield(L, -2, key);
+}
+
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);
lua_pop(L, 1);
@@ -33,10 +86,78 @@
}
lua_pop(L, 1);
return res;
}
+static const char *checkoption (lua_State *L, const char *conv, char *buff) {
+ static const char *const options[] = LUA_STRFTIMEOPTIONS;
+ unsigned int i;
+ for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
+ if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
+ buff[1] = *conv;
+ if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
+ buff[2] = '\0'; /* end buffer */
+ return conv + 1;
+ }
+ else if (*(conv + 1) != '\0' &&
+ strchr(options[i + 1], *(conv + 1)) != NULL) {
+ buff[2] = *(conv + 1); /* valid two-char conversion specifier */
+ buff[3] = '\0'; /* end buffer */
+ return conv + 2;
+ }
+ }
+ }
+ luaL_argerror(L, 1,
+ lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
+ return conv; /* to avoid warnings */
+}
+
+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 tmr, *stm;
+ if (*s == '!') { /* UTC? */
+ stm = l_gmtime(&t, &tmr);
+ s++; /* skip `!' */
+ }
+ else
+ stm = l_localtime(&t, &tmr);
+ if (stm == NULL) /* invalid date? */
+ lua_pushnil(L);
+ else if (strcmp(s, "*t") == 0) {
+ lua_createtable(L, 0, 9); /* 9 = number of fields */
+ setfield(L, "sec", stm->tm_sec);
+ setfield(L, "min", stm->tm_min);
+ setfield(L, "hour", stm->tm_hour);
+ setfield(L, "day", stm->tm_mday);
+ setfield(L, "month", stm->tm_mon+1);
+ setfield(L, "year", stm->tm_year+1900);
+ setfield(L, "wday", stm->tm_wday+1);
+ setfield(L, "yday", stm->tm_yday+1);
+ setboolfield(L, "isdst", stm->tm_isdst);
+ }
+ else {
+ char cc[4];
+ luaL_Buffer b;
+ cc[0] = '%';
+ luaL_buffinit(L, &b);
+ while (*s) {
+ if (*s != '%') /* no conversion specifier? */
+ luaL_addchar(&b, *s++);
+ else {
+ size_t reslen;
+ char buff[200]; /* should be big enough for any conversion result */
+ s = checkoption(L, s + 1, cc);
+ reslen = strftime(buff, sizeof(buff), cc, stm);
+ luaL_addlstring(&b, buff, reslen);
+ }
+ }
+ luaL_pushresult(&b);
+ }
+ return 1;
+}
+
static int os_time(lua_State *L) {
time_t t;
if (lua_isnoneornil(L, 1)) { /* called without args? */
t = time(NULL); /* get current time */
} else {
@@ -59,23 +180,24 @@
return 1;
}
static int os_difftime(lua_State *L) {
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
- (time_t)(luaL_optnumber(L, 2, (lua_Number)0))));
+ (time_t)(luaL_optnumber(L, 2, (lua_Number)0))));
return 1;
}
static const luaL_Reg libluaos[] = {
{"clock", os_clock},
{"time", os_time},
{"difftime", os_difftime},
+ {"date", os_date},
{NULL, NULL}
};
int
luaopen_luaos(lua_State *L) {
- luaL_checkversion(L);
+// luaL_checkversion(L);
luaL_register(L, "perf", libluaos);
return 1;
}