2004-07-09 19:47:48 +04:00
|
|
|
/*
|
2006-04-26 22:19:49 +04:00
|
|
|
** $Id: loslib.c,v 1.18 2006/03/09 18:08:22 roberto Exp roberto $
|
2004-07-09 19:47:48 +04:00
|
|
|
** Standard Operating System library
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define loslib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
2004-10-08 22:57:16 +04:00
|
|
|
static int os_pushresult (lua_State *L, int i, const char *filename) {
|
2006-01-27 16:54:39 +03:00
|
|
|
int en = errno; /* calls to Lua API may change this value */
|
2004-07-09 19:47:48 +04:00
|
|
|
if (i) {
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_pushnil(L);
|
2006-03-09 21:08:22 +03:00
|
|
|
lua_pushfstring(L, "%s: %s", filename, strerror(en));
|
2006-01-27 16:54:39 +03:00
|
|
|
lua_pushinteger(L, en);
|
2004-07-09 19:47:48 +04:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_execute (lua_State *L) {
|
2004-08-05 23:30:37 +04:00
|
|
|
lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
|
2004-07-09 19:47:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2004-10-08 22:57:16 +04:00
|
|
|
return os_pushresult(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);
|
2004-10-08 22:57:16 +04:00
|
|
|
return os_pushresult(L, rename(fromname, toname) == 0, fromname);
|
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);
|
|
|
|
if (err)
|
|
|
|
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=? }
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void setfield (lua_State *L, const char *key, int value) {
|
|
|
|
lua_pushinteger(L, value);
|
2005-01-10 22:16:29 +03:00
|
|
|
lua_setfield(L, -2, key);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int getfield (lua_State *L, const char *key, int d) {
|
|
|
|
int res;
|
|
|
|
lua_getfield(L, -1, key);
|
|
|
|
if (lua_isnumber(L, -1))
|
|
|
|
res = (int)lua_tointeger(L, -1);
|
|
|
|
else {
|
|
|
|
if (d < 0)
|
2005-05-17 23:49:15 +04:00
|
|
|
return luaL_error(L, "field " LUA_QS " missing in date table", key);
|
2004-07-09 19:47:48 +04:00
|
|
|
res = d;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-15 21:17:49 +03:00
|
|
|
static int os_date (lua_State *L) {
|
2004-07-09 19:47:48 +04:00
|
|
|
const char *s = luaL_optstring(L, 1, "%c");
|
2006-04-26 22:19:49 +04:00
|
|
|
time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
|
2004-07-09 19:47:48 +04:00
|
|
|
struct tm *stm;
|
|
|
|
if (*s == '!') { /* UTC? */
|
|
|
|
stm = gmtime(&t);
|
|
|
|
s++; /* skip `!' */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stm = localtime(&t);
|
|
|
|
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 b[256];
|
|
|
|
if (strftime(b, sizeof(b), s, stm))
|
|
|
|
lua_pushstring(L, b);
|
|
|
|
else
|
2005-05-17 23:49:15 +04:00
|
|
|
return luaL_error(L, LUA_QL("date") " format too long");
|
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 */
|
|
|
|
ts.tm_sec = getfield(L, "sec", 0);
|
|
|
|
ts.tm_min = getfield(L, "min", 0);
|
|
|
|
ts.tm_hour = getfield(L, "hour", 12);
|
|
|
|
ts.tm_mday = getfield(L, "day", -1);
|
|
|
|
ts.tm_mon = getfield(L, "month", -1) - 1;
|
|
|
|
ts.tm_year = getfield(L, "year", -1) - 1900;
|
|
|
|
ts.tm_isdst = getboolfield(L, "isdst");
|
|
|
|
t = mktime(&ts);
|
|
|
|
}
|
2004-08-05 23:30:37 +04:00
|
|
|
if (t == (time_t)(-1))
|
|
|
|
lua_pushnil(L);
|
|
|
|
else
|
2005-12-22 19:19:56 +03:00
|
|
|
lua_pushnumber(L, (lua_Number)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) {
|
2004-07-09 19:47:48 +04:00
|
|
|
lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
|
|
|
|
(time_t)(luaL_optnumber(L, 2, 0))));
|
|
|
|
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) {
|
2004-07-09 19:47:48 +04:00
|
|
|
exit(luaL_optint(L, 1, EXIT_SUCCESS));
|
|
|
|
return 0; /* to avoid warnings */
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LUALIB_API int luaopen_os (lua_State *L) {
|
2005-08-15 18:12:32 +04:00
|
|
|
luaL_register(L, LUA_OSLIBNAME, syslib);
|
2004-07-09 19:47:48 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|