2004-07-09 23:47:48 +08:00
|
|
|
/*
|
2014-03-13 04:57:40 +08:00
|
|
|
** $Id: loslib.c,v 1.43 2014/02/26 15:55:58 roberto Exp roberto $
|
2004-07-09 23:47:48 +08: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"
|
|
|
|
|
|
|
|
|
2009-12-17 21:08:51 +08:00
|
|
|
/*
|
2011-06-21 00:50:59 +08:00
|
|
|
** list of valid conversion specifiers for the 'strftime' function
|
2009-12-17 21:08:51 +08:00
|
|
|
*/
|
|
|
|
#if !defined(LUA_STRFTIMEOPTIONS)
|
|
|
|
|
|
|
|
#if !defined(LUA_USE_POSIX)
|
2012-05-23 23:37:09 +08:00
|
|
|
#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
|
2009-12-17 21:08:51 +08:00
|
|
|
#else
|
2012-05-23 23:37:09 +08:00
|
|
|
#define LUA_STRFTIMEOPTIONS \
|
2014-02-26 23:55:58 +08:00
|
|
|
{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
|
|
|
|
"E", "cCxXyY", \
|
2012-05-23 23:37:09 +08:00
|
|
|
"O", "deHImMSuUVwWy" }
|
2009-12-17 21:08:51 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-17 20:26:09 +08:00
|
|
|
/*
|
|
|
|
** By default, Lua uses tmpnam except when POSIX is available, where it
|
|
|
|
** uses mkstemp.
|
|
|
|
*/
|
2014-02-26 23:27:56 +08:00
|
|
|
#if !defined(lua_tmpnam) /* { */
|
|
|
|
|
|
|
|
#if defined(LUA_USE_POSIX) /* { */
|
|
|
|
|
2009-12-17 20:26:09 +08:00
|
|
|
#include <unistd.h>
|
2012-05-23 23:37:09 +08:00
|
|
|
#define LUA_TMPNAMBUFSIZE 32
|
2009-12-17 20:26:09 +08:00
|
|
|
#define lua_tmpnam(b,e) { \
|
|
|
|
strcpy(b, "/tmp/lua_XXXXXX"); \
|
|
|
|
e = mkstemp(b); \
|
|
|
|
if (e != -1) close(e); \
|
|
|
|
e = (e == -1); }
|
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
#else /* }{ */
|
2009-12-17 20:26:09 +08:00
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
/* ANSI definitions */
|
2012-05-23 23:37:09 +08:00
|
|
|
#define LUA_TMPNAMBUFSIZE L_tmpnam
|
|
|
|
#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
|
2009-12-17 20:26:09 +08:00
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
#endif /* } */
|
2009-12-17 20:26:09 +08:00
|
|
|
|
|
|
|
|
2011-11-30 01:15:42 +08:00
|
|
|
/*
|
|
|
|
** By default, Lua uses gmtime/localtime, except when POSIX is available,
|
|
|
|
** where it uses gmtime_r/localtime_r
|
|
|
|
*/
|
2014-02-26 23:27:56 +08:00
|
|
|
#if !defined(l_gmtime) /* { */
|
|
|
|
|
|
|
|
#if defined(LUA_USE_POSIX) /* { */
|
2011-11-30 01:15:42 +08:00
|
|
|
|
|
|
|
#define l_gmtime(t,r) gmtime_r(t,r)
|
|
|
|
#define l_localtime(t,r) localtime_r(t,r)
|
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
#else /* }{ */
|
2011-11-30 01:15:42 +08:00
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
/* ANSI definitions */
|
2011-11-30 01:15:42 +08:00
|
|
|
#define l_gmtime(t,r) ((void)r, gmtime(t))
|
|
|
|
#define l_localtime(t,r) ((void)r, localtime(t))
|
|
|
|
|
2014-02-26 23:27:56 +08:00
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
#endif /* } */
|
2011-11-30 01:15:42 +08:00
|
|
|
|
|
|
|
|
2009-12-17 20:26:09 +08:00
|
|
|
|
2011-03-04 00:34:46 +08:00
|
|
|
static int os_execute (lua_State *L) {
|
|
|
|
const char *cmd = luaL_optstring(L, 1, NULL);
|
|
|
|
int stat = system(cmd);
|
|
|
|
if (cmd != NULL)
|
|
|
|
return luaL_execresult(L, stat);
|
2004-07-09 23:47:48 +08:00
|
|
|
else {
|
2011-03-04 00:34:46 +08:00
|
|
|
lua_pushboolean(L, stat); /* true if there is a shell */
|
|
|
|
return 1;
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_remove (lua_State *L) {
|
2004-07-09 23:47:48 +08:00
|
|
|
const char *filename = luaL_checkstring(L, 1);
|
2011-03-04 00:34:46 +08:00
|
|
|
return luaL_fileresult(L, remove(filename) == 0, filename);
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_rename (lua_State *L) {
|
2004-07-09 23:47:48 +08:00
|
|
|
const char *fromname = luaL_checkstring(L, 1);
|
|
|
|
const char *toname = luaL_checkstring(L, 2);
|
2012-10-19 23:54:02 +08:00
|
|
|
return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_tmpname (lua_State *L) {
|
2005-03-19 02:02:04 +08: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 23:47:48 +08:00
|
|
|
lua_pushstring(L, buff);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_getenv (lua_State *L) {
|
2004-07-09 23:47:48 +08:00
|
|
|
lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_clock (lua_State *L) {
|
2004-07-09 23:47:48 +08: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-11 03:16:29 +08:00
|
|
|
lua_setfield(L, -2, key);
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setboolfield (lua_State *L, const char *key, int value) {
|
2005-09-10 02:22:46 +08:00
|
|
|
if (value < 0) /* undefined? */
|
|
|
|
return; /* does not set field */
|
2004-07-09 23:47:48 +08:00
|
|
|
lua_pushboolean(L, value);
|
2005-01-11 03:16:29 +08:00
|
|
|
lua_setfield(L, -2, key);
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int getboolfield (lua_State *L, const char *key) {
|
|
|
|
int res;
|
2014-03-13 04:57:40 +08:00
|
|
|
res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
|
2004-07-09 23:47:48 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int getfield (lua_State *L, const char *key, int d) {
|
2010-07-03 01:35:06 +08:00
|
|
|
int res, isnum;
|
2004-07-09 23:47:48 +08:00
|
|
|
lua_getfield(L, -1, key);
|
2010-07-03 01:35:06 +08:00
|
|
|
res = (int)lua_tointegerx(L, -1, &isnum);
|
|
|
|
if (!isnum) {
|
2004-07-09 23:47:48 +08:00
|
|
|
if (d < 0)
|
2005-05-18 03:49:15 +08:00
|
|
|
return luaL_error(L, "field " LUA_QS " missing in date table", key);
|
2004-07-09 23:47:48 +08:00
|
|
|
res = d;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-26 03:58:08 +08:00
|
|
|
static const char *checkoption (lua_State *L, const char *conv, char *buff) {
|
2009-12-17 21:08:51 +08:00
|
|
|
static const char *const options[] = LUA_STRFTIMEOPTIONS;
|
2009-08-26 03:58:08 +08:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2011-01-27 00:30:02 +08:00
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_date (lua_State *L) {
|
2004-07-09 23:47:48 +08:00
|
|
|
const char *s = luaL_optstring(L, 1, "%c");
|
2013-05-14 23:57:11 +08:00
|
|
|
time_t t = luaL_opt(L, (time_t)luaL_checkinteger, 2, time(NULL));
|
2011-11-30 01:15:42 +08:00
|
|
|
struct tm tmr, *stm;
|
2004-07-09 23:47:48 +08:00
|
|
|
if (*s == '!') { /* UTC? */
|
2011-11-30 01:15:42 +08:00
|
|
|
stm = l_gmtime(&t, &tmr);
|
2004-07-09 23:47:48 +08:00
|
|
|
s++; /* skip `!' */
|
|
|
|
}
|
|
|
|
else
|
2011-11-30 01:15:42 +08:00
|
|
|
stm = l_localtime(&t, &tmr);
|
2004-07-09 23:47:48 +08:00
|
|
|
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 {
|
2009-08-26 03:58:08 +08:00
|
|
|
char cc[4];
|
2006-09-19 21:57:08 +08:00
|
|
|
luaL_Buffer b;
|
2007-05-04 04:49:29 +08:00
|
|
|
cc[0] = '%';
|
2006-09-19 21:57:08 +08:00
|
|
|
luaL_buffinit(L, &b);
|
2009-08-26 03:58:08 +08:00
|
|
|
while (*s) {
|
2007-05-04 04:49:29 +08:00
|
|
|
if (*s != '%') /* no conversion specifier? */
|
2009-08-26 03:58:08 +08:00
|
|
|
luaL_addchar(&b, *s++);
|
2006-09-19 21:57:08 +08:00
|
|
|
else {
|
|
|
|
size_t reslen;
|
|
|
|
char buff[200]; /* should be big enough for any conversion result */
|
2009-08-26 03:58:08 +08:00
|
|
|
s = checkoption(L, s + 1, cc);
|
2006-09-19 21:57:08 +08:00
|
|
|
reslen = strftime(buff, sizeof(buff), cc, stm);
|
|
|
|
luaL_addlstring(&b, buff, reslen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_time (lua_State *L) {
|
2004-08-06 03:30:37 +08:00
|
|
|
time_t t;
|
2004-07-09 23:47:48 +08:00
|
|
|
if (lua_isnoneornil(L, 1)) /* called without args? */
|
2004-08-06 03:30:37 +08:00
|
|
|
t = time(NULL); /* get current time */
|
2004-07-09 23:47:48 +08: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-06 03:30:37 +08:00
|
|
|
if (t == (time_t)(-1))
|
|
|
|
lua_pushnil(L);
|
|
|
|
else
|
2013-05-14 23:57:11 +08:00
|
|
|
lua_pushinteger(L, t);
|
2004-07-09 23:47:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_difftime (lua_State *L) {
|
2013-05-14 23:57:11 +08:00
|
|
|
lua_pushnumber(L, difftime((time_t)(luaL_checkinteger(L, 1)),
|
|
|
|
(time_t)(luaL_optinteger(L, 2, 0))));
|
2004-07-09 23:47:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_setlocale (lua_State *L) {
|
2004-07-09 23:47:48 +08: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-27 02:19:49 +08:00
|
|
|
const char *l = luaL_optstring(L, 1, NULL);
|
2005-05-25 21:21:26 +08:00
|
|
|
int op = luaL_checkoption(L, 2, "all", catnames);
|
2004-07-09 23:47:48 +08:00
|
|
|
lua_pushstring(L, setlocale(cat[op], l));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-16 02:17:49 +08:00
|
|
|
static int os_exit (lua_State *L) {
|
2010-10-05 20:18:03 +08:00
|
|
|
int status;
|
|
|
|
if (lua_isboolean(L, 1))
|
|
|
|
status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
else
|
|
|
|
status = luaL_optint(L, 1, EXIT_SUCCESS);
|
2009-11-24 02:20:38 +08:00
|
|
|
if (lua_toboolean(L, 2))
|
2008-06-14 00:59:00 +08:00
|
|
|
lua_close(L);
|
2011-11-29 23:55:51 +08:00
|
|
|
if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
|
|
|
|
return 0;
|
2004-07-09 23:47:48 +08:00
|
|
|
}
|
|
|
|
|
2008-06-14 00:59:00 +08:00
|
|
|
|
2005-08-27 01:36:32 +08:00
|
|
|
static const luaL_Reg syslib[] = {
|
2005-12-16 02:17:49 +08: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 23:47:48 +08:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-24 20:05:44 +08:00
|
|
|
LUAMOD_API int luaopen_os (lua_State *L) {
|
2010-07-02 19:38:13 +08:00
|
|
|
luaL_newlib(L, syslib);
|
2004-07-09 23:47:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|