+++ /dev/null
-/*\r
-** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $\r
-** Standard Operating System library\r
-** See Copyright Notice in lua.h\r
-*/\r
-\r
-\r
-#include <errno.h>\r
-#include <locale.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <time.h>\r
-\r
-#define loslib_c\r
-#define LUA_LIB\r
-\r
-#include "lua.h"\r
-\r
-#include "lauxlib.h"\r
-#include "lualib.h"\r
-\r
-\r
-/*\r
-** list of valid conversion specifiers for the 'strftime' function\r
-*/\r
-#if !defined(LUA_STRFTIMEOPTIONS)\r
-\r
-#if !defined(LUA_USE_POSIX)\r
-#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }\r
-#else\r
-#define LUA_STRFTIMEOPTIONS \\r
- { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \\r
- "", "E", "cCxXyY", \\r
- "O", "deHImMSuUVwWy" }\r
-#endif\r
-\r
-#endif\r
-\r
-\r
-\r
-/*\r
-** By default, Lua uses tmpnam except when POSIX is available, where it\r
-** uses mkstemp.\r
-*/\r
-#if defined(LUA_USE_MKSTEMP)\r
-#include <unistd.h>\r
-#define LUA_TMPNAMBUFSIZE 32\r
-#define lua_tmpnam(b,e) { \\r
- strcpy(b, "/tmp/lua_XXXXXX"); \\r
- e = mkstemp(b); \\r
- if (e != -1) close(e); \\r
- e = (e == -1); }\r
-\r
-#elif !defined(lua_tmpnam)\r
-\r
-#define LUA_TMPNAMBUFSIZE L_tmpnam\r
-#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }\r
-\r
-#endif\r
-\r
-\r
-/*\r
-** By default, Lua uses gmtime/localtime, except when POSIX is available,\r
-** where it uses gmtime_r/localtime_r\r
-*/\r
-#if defined(LUA_USE_GMTIME_R)\r
-\r
-#define l_gmtime(t,r) gmtime_r(t,r)\r
-#define l_localtime(t,r) localtime_r(t,r)\r
-\r
-#elif !defined(l_gmtime)\r
-\r
-#define l_gmtime(t,r) ((void)r, gmtime(t))\r
-#define l_localtime(t,r) ((void)r, localtime(t))\r
-\r
-#endif\r
-\r
-\r
-\r
-static int os_execute (lua_State *L) {\r
- const char *cmd = luaL_optstring(L, 1, NULL);\r
- int stat = system(cmd);\r
- if (cmd != NULL)\r
- return luaL_execresult(L, stat);\r
- else {\r
- lua_pushboolean(L, stat); /* true if there is a shell */\r
- return 1;\r
- }\r
-}\r
-\r
-\r
-static int os_remove (lua_State *L) {\r
- const char *filename = luaL_checkstring(L, 1);\r
- return luaL_fileresult(L, remove(filename) == 0, filename);\r
-}\r
-\r
-\r
-static int os_rename (lua_State *L) {\r
- const char *fromname = luaL_checkstring(L, 1);\r
- const char *toname = luaL_checkstring(L, 2);\r
- return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);\r
-}\r
-\r
-\r
-static int os_tmpname (lua_State *L) {\r
- char buff[LUA_TMPNAMBUFSIZE];\r
- int err;\r
- lua_tmpnam(buff, err);\r
- if (err)\r
- return luaL_error(L, "unable to generate a unique filename");\r
- lua_pushstring(L, buff);\r
- return 1;\r
-}\r
-\r
-\r
-static int os_getenv (lua_State *L) {\r
- lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */\r
- return 1;\r
-}\r
-\r
-\r
-static int os_clock (lua_State *L) {\r
- lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);\r
- return 1;\r
-}\r
-\r
-\r
-/*\r
-** {======================================================\r
-** Time/Date operations\r
-** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,\r
-** wday=%w+1, yday=%j, isdst=? }\r
-** =======================================================\r
-*/\r
-\r
-static void setfield (lua_State *L, const char *key, int value) {\r
- lua_pushinteger(L, value);\r
- lua_setfield(L, -2, key);\r
-}\r
-\r
-static void setboolfield (lua_State *L, const char *key, int value) {\r
- if (value < 0) /* undefined? */\r
- return; /* does not set field */\r
- lua_pushboolean(L, value);\r
- lua_setfield(L, -2, key);\r
-}\r
-\r
-static int getboolfield (lua_State *L, const char *key) {\r
- int res;\r
- lua_getfield(L, -1, key);\r
- res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);\r
- lua_pop(L, 1);\r
- return res;\r
-}\r
-\r
-\r
-static int getfield (lua_State *L, const char *key, int d) {\r
- int res, isnum;\r
- lua_getfield(L, -1, key);\r
- res = (int)lua_tointegerx(L, -1, &isnum);\r
- if (!isnum) {\r
- if (d < 0)\r
- return luaL_error(L, "field " LUA_QS " missing in date table", key);\r
- res = d;\r
- }\r
- lua_pop(L, 1);\r
- return res;\r
-}\r
-\r
-\r
-static const char *checkoption (lua_State *L, const char *conv, char *buff) {\r
- static const char *const options[] = LUA_STRFTIMEOPTIONS;\r
- unsigned int i;\r
- for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {\r
- if (*conv != '\0' && strchr(options[i], *conv) != NULL) {\r
- buff[1] = *conv;\r
- if (*options[i + 1] == '\0') { /* one-char conversion specifier? */\r
- buff[2] = '\0'; /* end buffer */\r
- return conv + 1;\r
- }\r
- else if (*(conv + 1) != '\0' &&\r
- strchr(options[i + 1], *(conv + 1)) != NULL) {\r
- buff[2] = *(conv + 1); /* valid two-char conversion specifier */\r
- buff[3] = '\0'; /* end buffer */\r
- return conv + 2;\r
- }\r
- }\r
- }\r
- luaL_argerror(L, 1,\r
- lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));\r
- return conv; /* to avoid warnings */\r
-}\r
-\r
-\r
-static int os_date (lua_State *L) {\r
- const char *s = luaL_optstring(L, 1, "%c");\r
- time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));\r
- struct tm tmr, *stm;\r
- if (*s == '!') { /* UTC? */\r
- stm = l_gmtime(&t, &tmr);\r
- s++; /* skip `!' */\r
- }\r
- else\r
- stm = l_localtime(&t, &tmr);\r
- if (stm == NULL) /* invalid date? */\r
- lua_pushnil(L);\r
- else if (strcmp(s, "*t") == 0) {\r
- lua_createtable(L, 0, 9); /* 9 = number of fields */\r
- setfield(L, "sec", stm->tm_sec);\r
- setfield(L, "min", stm->tm_min);\r
- setfield(L, "hour", stm->tm_hour);\r
- setfield(L, "day", stm->tm_mday);\r
- setfield(L, "month", stm->tm_mon+1);\r
- setfield(L, "year", stm->tm_year+1900);\r
- setfield(L, "wday", stm->tm_wday+1);\r
- setfield(L, "yday", stm->tm_yday+1);\r
- setboolfield(L, "isdst", stm->tm_isdst);\r
- }\r
- else {\r
- char cc[4];\r
- luaL_Buffer b;\r
- cc[0] = '%';\r
- luaL_buffinit(L, &b);\r
- while (*s) {\r
- if (*s != '%') /* no conversion specifier? */\r
- luaL_addchar(&b, *s++);\r
- else {\r
- size_t reslen;\r
- char buff[200]; /* should be big enough for any conversion result */\r
- s = checkoption(L, s + 1, cc);\r
- reslen = strftime(buff, sizeof(buff), cc, stm);\r
- luaL_addlstring(&b, buff, reslen);\r
- }\r
- }\r
- luaL_pushresult(&b);\r
- }\r
- return 1;\r
-}\r
-\r
-\r
-static int os_time (lua_State *L) {\r
- time_t t;\r
- if (lua_isnoneornil(L, 1)) /* called without args? */\r
- t = time(NULL); /* get current time */\r
- else {\r
- struct tm ts;\r
- luaL_checktype(L, 1, LUA_TTABLE);\r
- lua_settop(L, 1); /* make sure table is at the top */\r
- ts.tm_sec = getfield(L, "sec", 0);\r
- ts.tm_min = getfield(L, "min", 0);\r
- ts.tm_hour = getfield(L, "hour", 12);\r
- ts.tm_mday = getfield(L, "day", -1);\r
- ts.tm_mon = getfield(L, "month", -1) - 1;\r
- ts.tm_year = getfield(L, "year", -1) - 1900;\r
- ts.tm_isdst = getboolfield(L, "isdst");\r
- t = mktime(&ts);\r
- }\r
- if (t == (time_t)(-1))\r
- lua_pushnil(L);\r
- else\r
- lua_pushnumber(L, (lua_Number)t);\r
- return 1;\r
-}\r
-\r
-\r
-static int os_difftime (lua_State *L) {\r
- lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),\r
- (time_t)(luaL_optnumber(L, 2, 0))));\r
- return 1;\r
-}\r
-\r
-/* }====================================================== */\r
-\r
-\r
-static int os_setlocale (lua_State *L) {\r
- static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,\r
- LC_NUMERIC, LC_TIME};\r
- static const char *const catnames[] = {"all", "collate", "ctype", "monetary",\r
- "numeric", "time", NULL};\r
- const char *l = luaL_optstring(L, 1, NULL);\r
- int op = luaL_checkoption(L, 2, "all", catnames);\r
- lua_pushstring(L, setlocale(cat[op], l));\r
- return 1;\r
-}\r
-\r
-\r
-static int os_exit (lua_State *L) {\r
- int status;\r
- if (lua_isboolean(L, 1))\r
- status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);\r
- else\r
- status = luaL_optint(L, 1, EXIT_SUCCESS);\r
- if (lua_toboolean(L, 2))\r
- lua_close(L);\r
- if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */\r
- return 0;\r
-}\r
-\r
-\r
-static const luaL_Reg syslib[] = {\r
- {"clock", os_clock},\r
- {"date", os_date},\r
- {"difftime", os_difftime},\r
- {"execute", os_execute},\r
- {"exit", os_exit},\r
- {"getenv", os_getenv},\r
- {"remove", os_remove},\r
- {"rename", os_rename},\r
- {"setlocale", os_setlocale},\r
- {"time", os_time},\r
- {"tmpname", os_tmpname},\r
- {NULL, NULL}\r
-};\r
-\r
-/* }====================================================== */\r
-\r
-\r
-\r
-LUAMOD_API int luaopen_os (lua_State *L) {\r
- luaL_newlib(L, syslib);\r
- return 1;\r
-}\r
-\r