rlm@1: /* rlm@1: ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ rlm@1: ** Standard Operating System library rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define loslib_c rlm@1: #define LUA_LIB rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lauxlib.h" rlm@1: #include "lualib.h" rlm@1: rlm@1: rlm@1: static int os_pushresult (lua_State *L, int i, const char *filename) { rlm@1: int en = errno; /* calls to Lua API may change this value */ rlm@1: if (i) { rlm@1: lua_pushboolean(L, 1); rlm@1: return 1; rlm@1: } rlm@1: else { rlm@1: lua_pushnil(L); rlm@1: lua_pushfstring(L, "%s: %s", filename, strerror(en)); rlm@1: lua_pushinteger(L, en); rlm@1: return 3; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static int os_execute (lua_State *L) { rlm@1: lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_remove (lua_State *L) { rlm@1: const char *filename = luaL_checkstring(L, 1); rlm@1: return os_pushresult(L, remove(filename) == 0, filename); rlm@1: } rlm@1: rlm@1: rlm@1: static int os_rename (lua_State *L) { rlm@1: const char *fromname = luaL_checkstring(L, 1); rlm@1: const char *toname = luaL_checkstring(L, 2); rlm@1: return os_pushresult(L, rename(fromname, toname) == 0, fromname); rlm@1: } rlm@1: rlm@1: rlm@1: static int os_tmpname (lua_State *L) { rlm@1: char buff[LUA_TMPNAMBUFSIZE]; rlm@1: int err; rlm@1: lua_tmpnam(buff, err); rlm@1: if (err) rlm@1: return luaL_error(L, "unable to generate a unique filename"); rlm@1: lua_pushstring(L, buff); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_getenv (lua_State *L) { rlm@1: lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_clock (lua_State *L) { rlm@1: lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** {====================================================== rlm@1: ** Time/Date operations rlm@1: ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, rlm@1: ** wday=%w+1, yday=%j, isdst=? } rlm@1: ** ======================================================= rlm@1: */ rlm@1: rlm@1: static void setfield (lua_State *L, const char *key, int value) { rlm@1: lua_pushinteger(L, value); rlm@1: lua_setfield(L, -2, key); rlm@1: } rlm@1: rlm@1: static void setboolfield (lua_State *L, const char *key, int value) { rlm@1: if (value < 0) /* undefined? */ rlm@1: return; /* does not set field */ rlm@1: lua_pushboolean(L, value); rlm@1: lua_setfield(L, -2, key); rlm@1: } rlm@1: rlm@1: static int getboolfield (lua_State *L, const char *key) { rlm@1: int res; rlm@1: lua_getfield(L, -1, key); rlm@1: res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); rlm@1: lua_pop(L, 1); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: static int getfield (lua_State *L, const char *key, int d) { rlm@1: int res; rlm@1: lua_getfield(L, -1, key); rlm@1: if (lua_isnumber(L, -1)) rlm@1: res = (int)lua_tointeger(L, -1); rlm@1: else { rlm@1: if (d < 0) rlm@1: return luaL_error(L, "field " LUA_QS " missing in date table", key); rlm@1: res = d; rlm@1: } rlm@1: lua_pop(L, 1); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_date (lua_State *L) { rlm@1: const char *s = luaL_optstring(L, 1, "%c"); rlm@1: time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); rlm@1: struct tm *stm; rlm@1: if (*s == '!') { /* UTC? */ rlm@1: stm = gmtime(&t); rlm@1: s++; /* skip `!' */ rlm@1: } rlm@1: else rlm@1: stm = localtime(&t); rlm@1: if (stm == NULL) /* invalid date? */ rlm@1: lua_pushnil(L); rlm@1: else if (strcmp(s, "*t") == 0) { rlm@1: lua_createtable(L, 0, 9); /* 9 = number of fields */ rlm@1: setfield(L, "sec", stm->tm_sec); rlm@1: setfield(L, "min", stm->tm_min); rlm@1: setfield(L, "hour", stm->tm_hour); rlm@1: setfield(L, "day", stm->tm_mday); rlm@1: setfield(L, "month", stm->tm_mon+1); rlm@1: setfield(L, "year", stm->tm_year+1900); rlm@1: setfield(L, "wday", stm->tm_wday+1); rlm@1: setfield(L, "yday", stm->tm_yday+1); rlm@1: setboolfield(L, "isdst", stm->tm_isdst); rlm@1: } rlm@1: else { rlm@1: char cc[3]; rlm@1: luaL_Buffer b; rlm@1: cc[0] = '%'; cc[2] = '\0'; rlm@1: luaL_buffinit(L, &b); rlm@1: for (; *s; s++) { rlm@1: if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ rlm@1: luaL_addchar(&b, *s); rlm@1: else { rlm@1: size_t reslen; rlm@1: char buff[200]; /* should be big enough for any conversion result */ rlm@1: cc[1] = *(++s); rlm@1: reslen = strftime(buff, sizeof(buff), cc, stm); rlm@1: luaL_addlstring(&b, buff, reslen); rlm@1: } rlm@1: } rlm@1: luaL_pushresult(&b); rlm@1: } rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_time (lua_State *L) { rlm@1: time_t t; rlm@1: if (lua_isnoneornil(L, 1)) /* called without args? */ rlm@1: t = time(NULL); /* get current time */ rlm@1: else { rlm@1: struct tm ts; rlm@1: luaL_checktype(L, 1, LUA_TTABLE); rlm@1: lua_settop(L, 1); /* make sure table is at the top */ rlm@1: ts.tm_sec = getfield(L, "sec", 0); rlm@1: ts.tm_min = getfield(L, "min", 0); rlm@1: ts.tm_hour = getfield(L, "hour", 12); rlm@1: ts.tm_mday = getfield(L, "day", -1); rlm@1: ts.tm_mon = getfield(L, "month", -1) - 1; rlm@1: ts.tm_year = getfield(L, "year", -1) - 1900; rlm@1: ts.tm_isdst = getboolfield(L, "isdst"); rlm@1: t = mktime(&ts); rlm@1: } rlm@1: if (t == (time_t)(-1)) rlm@1: lua_pushnil(L); rlm@1: else rlm@1: lua_pushnumber(L, (lua_Number)t); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_difftime (lua_State *L) { rlm@1: lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), rlm@1: (time_t)(luaL_optnumber(L, 2, 0)))); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: static int os_setlocale (lua_State *L) { rlm@1: static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, rlm@1: LC_NUMERIC, LC_TIME}; rlm@1: static const char *const catnames[] = {"all", "collate", "ctype", "monetary", rlm@1: "numeric", "time", NULL}; rlm@1: const char *l = luaL_optstring(L, 1, NULL); rlm@1: int op = luaL_checkoption(L, 2, "all", catnames); rlm@1: lua_pushstring(L, setlocale(cat[op], l)); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: static int os_exit (lua_State *L) { rlm@1: exit(luaL_optint(L, 1, EXIT_SUCCESS)); rlm@1: } rlm@1: rlm@1: static const luaL_Reg syslib[] = { rlm@1: {"clock", os_clock}, rlm@1: {"date", os_date}, rlm@1: {"difftime", os_difftime}, rlm@1: {"execute", os_execute}, rlm@1: {"exit", os_exit}, rlm@1: {"getenv", os_getenv}, rlm@1: {"remove", os_remove}, rlm@1: {"rename", os_rename}, rlm@1: {"setlocale", os_setlocale}, rlm@1: {"time", os_time}, rlm@1: {"tmpname", os_tmpname}, rlm@1: {NULL, NULL} rlm@1: }; rlm@1: rlm@1: /* }====================================================== */ rlm@1: rlm@1: rlm@1: rlm@1: LUALIB_API int luaopen_os (lua_State *L) { rlm@1: luaL_register(L, LUA_OSLIBNAME, syslib); rlm@1: return 1; rlm@1: } rlm@1: