fix red.
[kugel-rb/myfork.git] / apps / plugins / lua / loslib.c
blob19fef84a9ceb8ae903353f71a236c75e1488443a
1 /*
2 ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
3 ** Standard Operating System library
4 ** See Copyright Notice in lua.h
5 */
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
13 #define loslib_c
14 #define LUA_LIB
16 #include "lua.h"
18 #include "lauxlib.h"
19 #include "lualib.h"
22 static int os_pushresult (lua_State *L, int i, const char *filename) {
23 int en = errno; /* calls to Lua API may change this value */
24 if (i) {
25 lua_pushboolean(L, 1);
26 return 1;
28 else {
29 lua_pushnil(L);
30 lua_pushfstring(L, "%s: %s", filename, strerror(en));
31 lua_pushinteger(L, en);
32 return 3;
37 static int os_remove (lua_State *L) {
38 const char *filename = luaL_checkstring(L, 1);
39 return os_pushresult(L, rb->remove(filename) == 0, filename);
43 static int os_rename (lua_State *L) {
44 const char *fromname = luaL_checkstring(L, 1);
45 const char *toname = luaL_checkstring(L, 2);
46 return os_pushresult(L, rb->rename(fromname, toname) == 0, fromname);
51 ** {======================================================
52 ** Time/Date operations
53 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
54 ** wday=%w+1, yday=%j, isdst=? }
55 ** =======================================================
58 static void setfield (lua_State *L, const char *key, int value) {
59 lua_pushinteger(L, value);
60 lua_setfield(L, -2, key);
63 static void setboolfield (lua_State *L, const char *key, int value) {
64 if (value < 0) /* undefined? */
65 return; /* does not set field */
66 lua_pushboolean(L, value);
67 lua_setfield(L, -2, key);
70 #if CONFIG_RTC
71 static int getboolfield (lua_State *L, const char *key) {
72 int res;
73 lua_getfield(L, -1, key);
74 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
75 lua_pop(L, 1);
76 return res;
80 static int getfield (lua_State *L, const char *key, int d) {
81 int res;
82 lua_getfield(L, -1, key);
83 if (lua_isnumber(L, -1))
84 res = (int)lua_tointeger(L, -1);
85 else {
86 if (d < 0)
87 return luaL_error(L, "field " LUA_QS " missing in date table", key);
88 res = d;
90 lua_pop(L, 1);
91 return res;
93 #endif
96 static int os_date (lua_State *L) {
97 const char *s = luaL_optstring(L, 1, "%c");
98 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2,
99 #if CONFIG_RTC
100 rb->mktime(rb->get_time())
101 #else
103 #endif
105 struct tm *stm;
106 if (*s == '!') /* UTC? */ /* Rockbox doesn't support timezones */
107 s++; /* skip `!' */
108 stm = gmtime(&t);
109 if (stm == NULL) /* invalid date? */
110 lua_pushnil(L);
111 else if (strcmp(s, "*t") == 0) {
112 lua_createtable(L, 0, 9); /* 9 = number of fields */
113 setfield(L, "sec", stm->tm_sec);
114 setfield(L, "min", stm->tm_min);
115 setfield(L, "hour", stm->tm_hour);
116 setfield(L, "day", stm->tm_mday);
117 setfield(L, "month", stm->tm_mon+1);
118 setfield(L, "year", stm->tm_year+1900);
119 setfield(L, "wday", stm->tm_wday+1);
120 setfield(L, "yday", stm->tm_yday+1);
121 setboolfield(L, "isdst", stm->tm_isdst);
123 else {
124 char cc[3];
125 luaL_Buffer b;
126 cc[0] = '%'; cc[2] = '\0';
127 luaL_buffinit(L, &b);
128 for (; *s; s++) {
129 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
130 luaL_addchar(&b, *s);
131 else {
132 size_t reslen;
133 char buff[200]; /* should be big enough for any conversion result */
134 cc[1] = *(++s);
135 reslen = strftime(buff, sizeof(buff), cc, stm);
136 luaL_addlstring(&b, buff, reslen);
139 luaL_pushresult(&b);
141 return 1;
144 static int os_time (lua_State *L) {
145 time_t t = -1;
146 #if CONFIG_RTC
147 if (lua_isnoneornil(L, 1)) /* called without args? */
148 t = rb->mktime(rb->get_time()); /* get current time */
149 else {
150 struct tm ts;
151 luaL_checktype(L, 1, LUA_TTABLE);
152 lua_settop(L, 1); /* make sure table is at the top */
153 ts.tm_sec = getfield(L, "sec", 0);
154 ts.tm_min = getfield(L, "min", 0);
155 ts.tm_hour = getfield(L, "hour", 12);
156 ts.tm_mday = getfield(L, "day", -1);
157 ts.tm_mon = getfield(L, "month", -1) - 1;
158 ts.tm_year = getfield(L, "year", -1) - 1900;
159 ts.tm_isdst = getboolfield(L, "isdst");
160 t = rb->mktime(&ts);
162 #endif
163 if (t == (time_t)(-1))
164 lua_pushnil(L);
165 else
166 lua_pushnumber(L, (lua_Number)t);
167 return 1;
171 /* }====================================================== */
174 static int os_exit (lua_State *L) {
175 exit(luaL_optint(L, 1, EXIT_SUCCESS));
178 static const luaL_Reg syslib[] = {
179 //{"clock", os_clock},
180 {"date", os_date},
181 //{"difftime", os_difftime},
182 //{"execute", os_execute},
183 {"exit", os_exit},
184 //{"getenv", os_getenv},
185 {"remove", os_remove},
186 {"rename", os_rename},
187 //{"setlocale", os_setlocale},
188 {"time", os_time},
189 //{"tmpname", os_tmpname},
190 {NULL, NULL}
193 /* }====================================================== */
197 LUALIB_API int luaopen_os (lua_State *L) {
198 luaL_register(L, LUA_OSLIBNAME, syslib);
199 return 1;