Something went wrong. Try again.
A modern, network-enabled music player platform built on Rockbox technology. rockboxd.tsiry-sandratraina.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214/*** Standard Operating System library** See Copyright Notice in lua.h*/
#include <errno.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"#include "rocklibc.h" /* ROCKLUA ADDED */
static int os_pushresult (lua_State *L, int i, const char *filename) { int en = errno; /* calls to Lua API may change this value */ if (i) { lua_pushboolean(L, 1); return 1; } else { lua_pushnil(L); lua_pushfstring(L, "%s: %s", filename, strerror(en)); lua_pushinteger(L, en); return 3; }}
static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); return os_pushresult(L, remove(filename) == 0, filename);}
static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); return os_pushresult(L, rename(fromname, toname) == 0, fromname);}
/*** {======================================================** Time/Date operations** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,** wday=%w+1, yday=%j, isdst=? }** =======================================================*/#if 0 /* supplied by strfrtime.lua*/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);}#endif
#if CONFIG_RTCstatic 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) return luaL_error(L, "field " LUA_QS " missing in date table", key); res = d; } lua_pop(L, 1); return res;}#endif
static int os_date (lua_State *L) {#if 0 const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2,#if CONFIG_RTC mktime(get_time())#else 0#endif ); struct tm *stm; if (*s == '!') /* UTC? */ /* Rockbox doesn't support timezones */ s++; /* skip `!' */ stm = gmtime(&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 cc[3]; luaL_Buffer b; cc[0] = '%'; cc[2] = '\0'; luaL_buffinit(L, &b); for (; *s; s++) { if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ luaL_addchar(&b, *s); else { size_t reslen; char buff[200]; /* should be big enough for any conversion result */ cc[1] = *(++s); reslen = strftime(buff, sizeof(buff), cc, stm); luaL_addlstring(&b, buff, reslen); } } luaL_pushresult(&b); } return 1;#else /* supplied by strfrtime.lua*/ lua_pushnil(L); return 1;#endif
}
static int os_time (lua_State *L) { time_t t = -1;#if CONFIG_RTC if (lua_isnoneornil(L, 1)) /* called without args? */ t = mktime(get_time()); /* get current time */ 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); }#endif if (t == (time_t)(-1)) lua_pushnil(L); else lua_pushnumber(L, (lua_Number)t); return 1;}
/* }====================================================== */
static int os_exit (lua_State *L) { lua_settop(L, 2); int status = luaL_optint(L, 1, EXIT_SUCCESS); if (status != EXIT_SUCCESS && lua_type (L, 2) != LUA_TSTRING) lua_pushfstring(L, "exit (%d)", status); lua_pushvalue(L, 1); /* put exit status on top of stack */ exit(status); return EXIT_SUCCESS; /* never reached, surpress warning */}
static const luaL_Reg syslib[] = { //{"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}, {NULL, NULL}};
/* }====================================================== */
LUALIB_API int luaopen_os (lua_State *L) { luaL_register(L, LUA_OSLIBNAME, syslib); return 1;}