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
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 */
25 lua_pushboolean(L
, 1);
30 lua_pushfstring(L
, "%s: %s", filename
, strerror(en
));
31 lua_pushinteger(L
, en
);
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
);
71 static int getboolfield (lua_State
*L
, const char *key
) {
73 lua_getfield(L
, -1, key
);
74 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
80 static int getfield (lua_State
*L
, const char *key
, int d
) {
82 lua_getfield(L
, -1, key
);
83 if (lua_isnumber(L
, -1))
84 res
= (int)lua_tointeger(L
, -1);
87 return luaL_error(L
, "field " LUA_QS
" missing in date table", key
);
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,
100 rb
->mktime(rb
->get_time())
106 if (*s
== '!') /* UTC? */ /* Rockbox doesn't support timezones */
109 if (stm
== NULL
) /* invalid date? */
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
);
126 cc
[0] = '%'; cc
[2] = '\0';
127 luaL_buffinit(L
, &b
);
129 if (*s
!= '%' || *(s
+ 1) == '\0') /* no conversion specifier? */
130 luaL_addchar(&b
, *s
);
133 char buff
[200]; /* should be big enough for any conversion result */
135 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
136 luaL_addlstring(&b
, buff
, reslen
);
144 static int os_time (lua_State
*L
) {
147 if (lua_isnoneornil(L
, 1)) /* called without args? */
148 t
= rb
->mktime(rb
->get_time()); /* get current time */
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");
163 if (t
== (time_t)(-1))
166 lua_pushnumber(L
, (lua_Number
)t
);
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},
181 //{"difftime", os_difftime},
182 //{"execute", os_execute},
184 //{"getenv", os_getenv},
185 {"remove", os_remove
},
186 {"rename", os_rename
},
187 //{"setlocale", os_setlocale},
189 //{"tmpname", os_tmpname},
193 /* }====================================================== */
197 LUALIB_API
int luaopen_os (lua_State
*L
) {
198 luaL_register(L
, LUA_OSLIBNAME
, syslib
);