Add memtest support.
[syslinux-debian/hramrach.git] / com32 / lua / src / loslib.c
blob2d9b260cbdf883baa275e76af16d67e0029263c9
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 <locale.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
14 #define loslib_c
15 #define LUA_LIB
17 #include "lua.h"
19 #include "lauxlib.h"
20 #include "lualib.h"
23 static int os_pushresult (lua_State *L, int i, const char *filename) {
24 int en = errno; /* calls to Lua API may change this value */
25 if (i) {
26 lua_pushboolean(L, 1);
27 return 1;
29 else {
30 lua_pushnil(L);
31 lua_pushfstring(L, "%s: %s", filename, strerror(en));
32 lua_pushinteger(L, en);
33 return 3;
38 static int os_execute (lua_State *L) {
39 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
40 return 1;
44 static int os_remove (lua_State *L) {
45 const char *filename = luaL_checkstring(L, 1);
46 return os_pushresult(L, remove(filename) == 0, filename);
50 static int os_rename (lua_State *L) {
51 const char *fromname = luaL_checkstring(L, 1);
52 const char *toname = luaL_checkstring(L, 2);
53 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
57 static int os_tmpname (lua_State *L) {
58 char buff[LUA_TMPNAMBUFSIZE];
59 int err;
60 lua_tmpnam(buff, err);
61 if (err)
62 return luaL_error(L, "unable to generate a unique filename");
63 lua_pushstring(L, buff);
64 return 1;
68 static int os_getenv (lua_State *L) {
69 #ifdef SYSLINUX
70 lua_pushstring(L, NULL); /* if NULL push nil */
71 #else
72 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
73 #endif
74 return 1;
78 static int os_clock (lua_State *L) {
79 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
80 return 1;
85 ** {======================================================
86 ** Time/Date operations
87 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
88 ** wday=%w+1, yday=%j, isdst=? }
89 ** =======================================================
92 static void setfield (lua_State *L, const char *key, int value) {
93 lua_pushinteger(L, value);
94 lua_setfield(L, -2, key);
97 static void setboolfield (lua_State *L, const char *key, int value) {
98 if (value < 0) /* undefined? */
99 return; /* does not set field */
100 lua_pushboolean(L, value);
101 lua_setfield(L, -2, key);
104 static int getboolfield (lua_State *L, const char *key) {
105 int res;
106 lua_getfield(L, -1, key);
107 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
108 lua_pop(L, 1);
109 return res;
113 static int getfield (lua_State *L, const char *key, int d) {
114 int res;
115 lua_getfield(L, -1, key);
116 if (lua_isnumber(L, -1))
117 res = (int)lua_tointeger(L, -1);
118 else {
119 if (d < 0)
120 return luaL_error(L, "field " LUA_QS " missing in date table", key);
121 res = d;
123 lua_pop(L, 1);
124 return res;
128 static int os_date (lua_State *L) {
129 const char *s = luaL_optstring(L, 1, "%c");
130 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
131 struct tm *stm;
132 if (*s == '!') { /* UTC? */
133 stm = gmtime(&t);
134 s++; /* skip `!' */
136 else
137 stm = localtime(&t);
138 if (stm == NULL) /* invalid date? */
139 lua_pushnil(L);
140 else if (strcmp(s, "*t") == 0) {
141 lua_createtable(L, 0, 9); /* 9 = number of fields */
142 setfield(L, "sec", stm->tm_sec);
143 setfield(L, "min", stm->tm_min);
144 setfield(L, "hour", stm->tm_hour);
145 setfield(L, "day", stm->tm_mday);
146 setfield(L, "month", stm->tm_mon+1);
147 setfield(L, "year", stm->tm_year+1900);
148 setfield(L, "wday", stm->tm_wday+1);
149 setfield(L, "yday", stm->tm_yday+1);
150 setboolfield(L, "isdst", stm->tm_isdst);
152 else {
153 char cc[3];
154 luaL_Buffer b;
155 cc[0] = '%'; cc[2] = '\0';
156 luaL_buffinit(L, &b);
157 for (; *s; s++) {
158 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
159 luaL_addchar(&b, *s);
160 else {
161 size_t reslen;
162 char buff[200]; /* should be big enough for any conversion result */
163 cc[1] = *(++s);
164 reslen = strftime(buff, sizeof(buff), cc, stm);
165 luaL_addlstring(&b, buff, reslen);
168 luaL_pushresult(&b);
170 return 1;
174 static int os_time (lua_State *L) {
175 time_t t;
176 if (lua_isnoneornil(L, 1)) /* called without args? */
177 t = time(NULL); /* get current time */
178 else {
179 struct tm ts;
180 luaL_checktype(L, 1, LUA_TTABLE);
181 lua_settop(L, 1); /* make sure table is at the top */
182 ts.tm_sec = getfield(L, "sec", 0);
183 ts.tm_min = getfield(L, "min", 0);
184 ts.tm_hour = getfield(L, "hour", 12);
185 ts.tm_mday = getfield(L, "day", -1);
186 ts.tm_mon = getfield(L, "month", -1) - 1;
187 ts.tm_year = getfield(L, "year", -1) - 1900;
188 ts.tm_isdst = getboolfield(L, "isdst");
189 t = mktime(&ts);
191 if (t == (time_t)(-1))
192 lua_pushnil(L);
193 else
194 lua_pushnumber(L, (lua_Number)t);
195 return 1;
199 #if !defined LUA_NUMBER_INTEGRAL
200 static int os_difftime (lua_State *L) {
201 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
202 (time_t)(luaL_optnumber(L, 2, 0))));
203 return 1;
205 #endif
207 /* }====================================================== */
210 static int os_setlocale (lua_State *L) {
211 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
212 LC_NUMERIC, LC_TIME};
213 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
214 "numeric", "time", NULL};
215 const char *l = luaL_optstring(L, 1, NULL);
216 int op = luaL_checkoption(L, 2, "all", catnames);
217 lua_pushstring(L, setlocale(cat[op], l));
218 return 1;
222 static int os_exit (lua_State *L) {
223 exit(luaL_optint(L, 1, EXIT_SUCCESS));
226 static const luaL_Reg syslib[] = {
227 {"clock", os_clock},
228 {"date", os_date},
229 #if !defined LUA_NUMBER_INTEGRAL
230 {"difftime", os_difftime},
231 #endif
232 {"execute", os_execute},
233 {"exit", os_exit},
234 {"getenv", os_getenv},
235 {"remove", os_remove},
236 {"rename", os_rename},
237 {"setlocale", os_setlocale},
238 {"time", os_time},
239 {"tmpname", os_tmpname},
240 {NULL, NULL}
243 /* }====================================================== */
247 LUALIB_API int luaopen_os (lua_State *L) {
248 luaL_register(L, LUA_OSLIBNAME, syslib);
249 return 1;