]>
Commit | Line | Data |
---|---|---|
16a5fed6 | 1 | /*\r |
2 | ** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $\r | |
3 | ** Standard Operating System library\r | |
4 | ** See Copyright Notice in lua.h\r | |
5 | */\r | |
6 | \r | |
7 | \r | |
8 | #include <errno.h>\r | |
9 | #include <locale.h>\r | |
10 | #include <stdlib.h>\r | |
11 | #include <string.h>\r | |
12 | #include <time.h>\r | |
13 | \r | |
14 | #define loslib_c\r | |
15 | #define LUA_LIB\r | |
16 | \r | |
17 | #include "lua.h"\r | |
18 | \r | |
19 | #include "lauxlib.h"\r | |
20 | #include "lualib.h"\r | |
21 | \r | |
22 | \r | |
23 | /*\r | |
24 | ** list of valid conversion specifiers for the 'strftime' function\r | |
25 | */\r | |
26 | #if !defined(LUA_STRFTIMEOPTIONS)\r | |
27 | \r | |
28 | #if !defined(LUA_USE_POSIX)\r | |
29 | #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }\r | |
30 | #else\r | |
31 | #define LUA_STRFTIMEOPTIONS \\r | |
32 | { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \\r | |
33 | "", "E", "cCxXyY", \\r | |
34 | "O", "deHImMSuUVwWy" }\r | |
35 | #endif\r | |
36 | \r | |
37 | #endif\r | |
38 | \r | |
39 | \r | |
40 | \r | |
41 | /*\r | |
42 | ** By default, Lua uses tmpnam except when POSIX is available, where it\r | |
43 | ** uses mkstemp.\r | |
44 | */\r | |
45 | #if defined(LUA_USE_MKSTEMP)\r | |
46 | #include <unistd.h>\r | |
47 | #define LUA_TMPNAMBUFSIZE 32\r | |
48 | #define lua_tmpnam(b,e) { \\r | |
49 | strcpy(b, "/tmp/lua_XXXXXX"); \\r | |
50 | e = mkstemp(b); \\r | |
51 | if (e != -1) close(e); \\r | |
52 | e = (e == -1); }\r | |
53 | \r | |
54 | #elif !defined(lua_tmpnam)\r | |
55 | \r | |
56 | #define LUA_TMPNAMBUFSIZE L_tmpnam\r | |
57 | #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }\r | |
58 | \r | |
59 | #endif\r | |
60 | \r | |
61 | \r | |
62 | /*\r | |
63 | ** By default, Lua uses gmtime/localtime, except when POSIX is available,\r | |
64 | ** where it uses gmtime_r/localtime_r\r | |
65 | */\r | |
66 | #if defined(LUA_USE_GMTIME_R)\r | |
67 | \r | |
68 | #define l_gmtime(t,r) gmtime_r(t,r)\r | |
69 | #define l_localtime(t,r) localtime_r(t,r)\r | |
70 | \r | |
71 | #elif !defined(l_gmtime)\r | |
72 | \r | |
73 | #define l_gmtime(t,r) ((void)r, gmtime(t))\r | |
74 | #define l_localtime(t,r) ((void)r, localtime(t))\r | |
75 | \r | |
76 | #endif\r | |
77 | \r | |
78 | \r | |
79 | \r | |
80 | static int os_execute (lua_State *L) {\r | |
81 | const char *cmd = luaL_optstring(L, 1, NULL);\r | |
82 | int stat = system(cmd);\r | |
83 | if (cmd != NULL)\r | |
84 | return luaL_execresult(L, stat);\r | |
85 | else {\r | |
86 | lua_pushboolean(L, stat); /* true if there is a shell */\r | |
87 | return 1;\r | |
88 | }\r | |
89 | }\r | |
90 | \r | |
91 | \r | |
92 | static int os_remove (lua_State *L) {\r | |
93 | const char *filename = luaL_checkstring(L, 1);\r | |
94 | return luaL_fileresult(L, remove(filename) == 0, filename);\r | |
95 | }\r | |
96 | \r | |
97 | \r | |
98 | static int os_rename (lua_State *L) {\r | |
99 | const char *fromname = luaL_checkstring(L, 1);\r | |
100 | const char *toname = luaL_checkstring(L, 2);\r | |
101 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);\r | |
102 | }\r | |
103 | \r | |
104 | \r | |
105 | static int os_tmpname (lua_State *L) {\r | |
106 | char buff[LUA_TMPNAMBUFSIZE];\r | |
107 | int err;\r | |
108 | lua_tmpnam(buff, err);\r | |
109 | if (err)\r | |
110 | return luaL_error(L, "unable to generate a unique filename");\r | |
111 | lua_pushstring(L, buff);\r | |
112 | return 1;\r | |
113 | }\r | |
114 | \r | |
115 | \r | |
116 | static int os_getenv (lua_State *L) {\r | |
117 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */\r | |
118 | return 1;\r | |
119 | }\r | |
120 | \r | |
121 | \r | |
122 | static int os_clock (lua_State *L) {\r | |
123 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);\r | |
124 | return 1;\r | |
125 | }\r | |
126 | \r | |
127 | \r | |
128 | /*\r | |
129 | ** {======================================================\r | |
130 | ** Time/Date operations\r | |
131 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,\r | |
132 | ** wday=%w+1, yday=%j, isdst=? }\r | |
133 | ** =======================================================\r | |
134 | */\r | |
135 | \r | |
136 | static void setfield (lua_State *L, const char *key, int value) {\r | |
137 | lua_pushinteger(L, value);\r | |
138 | lua_setfield(L, -2, key);\r | |
139 | }\r | |
140 | \r | |
141 | static void setboolfield (lua_State *L, const char *key, int value) {\r | |
142 | if (value < 0) /* undefined? */\r | |
143 | return; /* does not set field */\r | |
144 | lua_pushboolean(L, value);\r | |
145 | lua_setfield(L, -2, key);\r | |
146 | }\r | |
147 | \r | |
148 | static int getboolfield (lua_State *L, const char *key) {\r | |
149 | int res;\r | |
150 | lua_getfield(L, -1, key);\r | |
151 | res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);\r | |
152 | lua_pop(L, 1);\r | |
153 | return res;\r | |
154 | }\r | |
155 | \r | |
156 | \r | |
157 | static int getfield (lua_State *L, const char *key, int d) {\r | |
158 | int res, isnum;\r | |
159 | lua_getfield(L, -1, key);\r | |
160 | res = (int)lua_tointegerx(L, -1, &isnum);\r | |
161 | if (!isnum) {\r | |
162 | if (d < 0)\r | |
163 | return luaL_error(L, "field " LUA_QS " missing in date table", key);\r | |
164 | res = d;\r | |
165 | }\r | |
166 | lua_pop(L, 1);\r | |
167 | return res;\r | |
168 | }\r | |
169 | \r | |
170 | \r | |
171 | static const char *checkoption (lua_State *L, const char *conv, char *buff) {\r | |
172 | static const char *const options[] = LUA_STRFTIMEOPTIONS;\r | |
173 | unsigned int i;\r | |
174 | for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {\r | |
175 | if (*conv != '\0' && strchr(options[i], *conv) != NULL) {\r | |
176 | buff[1] = *conv;\r | |
177 | if (*options[i + 1] == '\0') { /* one-char conversion specifier? */\r | |
178 | buff[2] = '\0'; /* end buffer */\r | |
179 | return conv + 1;\r | |
180 | }\r | |
181 | else if (*(conv + 1) != '\0' &&\r | |
182 | strchr(options[i + 1], *(conv + 1)) != NULL) {\r | |
183 | buff[2] = *(conv + 1); /* valid two-char conversion specifier */\r | |
184 | buff[3] = '\0'; /* end buffer */\r | |
185 | return conv + 2;\r | |
186 | }\r | |
187 | }\r | |
188 | }\r | |
189 | luaL_argerror(L, 1,\r | |
190 | lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));\r | |
191 | return conv; /* to avoid warnings */\r | |
192 | }\r | |
193 | \r | |
194 | \r | |
195 | static int os_date (lua_State *L) {\r | |
196 | const char *s = luaL_optstring(L, 1, "%c");\r | |
197 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));\r | |
198 | struct tm tmr, *stm;\r | |
199 | if (*s == '!') { /* UTC? */\r | |
200 | stm = l_gmtime(&t, &tmr);\r | |
201 | s++; /* skip `!' */\r | |
202 | }\r | |
203 | else\r | |
204 | stm = l_localtime(&t, &tmr);\r | |
205 | if (stm == NULL) /* invalid date? */\r | |
206 | lua_pushnil(L);\r | |
207 | else if (strcmp(s, "*t") == 0) {\r | |
208 | lua_createtable(L, 0, 9); /* 9 = number of fields */\r | |
209 | setfield(L, "sec", stm->tm_sec);\r | |
210 | setfield(L, "min", stm->tm_min);\r | |
211 | setfield(L, "hour", stm->tm_hour);\r | |
212 | setfield(L, "day", stm->tm_mday);\r | |
213 | setfield(L, "month", stm->tm_mon+1);\r | |
214 | setfield(L, "year", stm->tm_year+1900);\r | |
215 | setfield(L, "wday", stm->tm_wday+1);\r | |
216 | setfield(L, "yday", stm->tm_yday+1);\r | |
217 | setboolfield(L, "isdst", stm->tm_isdst);\r | |
218 | }\r | |
219 | else {\r | |
220 | char cc[4];\r | |
221 | luaL_Buffer b;\r | |
222 | cc[0] = '%';\r | |
223 | luaL_buffinit(L, &b);\r | |
224 | while (*s) {\r | |
225 | if (*s != '%') /* no conversion specifier? */\r | |
226 | luaL_addchar(&b, *s++);\r | |
227 | else {\r | |
228 | size_t reslen;\r | |
229 | char buff[200]; /* should be big enough for any conversion result */\r | |
230 | s = checkoption(L, s + 1, cc);\r | |
231 | reslen = strftime(buff, sizeof(buff), cc, stm);\r | |
232 | luaL_addlstring(&b, buff, reslen);\r | |
233 | }\r | |
234 | }\r | |
235 | luaL_pushresult(&b);\r | |
236 | }\r | |
237 | return 1;\r | |
238 | }\r | |
239 | \r | |
240 | \r | |
241 | static int os_time (lua_State *L) {\r | |
242 | time_t t;\r | |
243 | if (lua_isnoneornil(L, 1)) /* called without args? */\r | |
244 | t = time(NULL); /* get current time */\r | |
245 | else {\r | |
246 | struct tm ts;\r | |
247 | luaL_checktype(L, 1, LUA_TTABLE);\r | |
248 | lua_settop(L, 1); /* make sure table is at the top */\r | |
249 | ts.tm_sec = getfield(L, "sec", 0);\r | |
250 | ts.tm_min = getfield(L, "min", 0);\r | |
251 | ts.tm_hour = getfield(L, "hour", 12);\r | |
252 | ts.tm_mday = getfield(L, "day", -1);\r | |
253 | ts.tm_mon = getfield(L, "month", -1) - 1;\r | |
254 | ts.tm_year = getfield(L, "year", -1) - 1900;\r | |
255 | ts.tm_isdst = getboolfield(L, "isdst");\r | |
256 | t = mktime(&ts);\r | |
257 | }\r | |
258 | if (t == (time_t)(-1))\r | |
259 | lua_pushnil(L);\r | |
260 | else\r | |
261 | lua_pushnumber(L, (lua_Number)t);\r | |
262 | return 1;\r | |
263 | }\r | |
264 | \r | |
265 | \r | |
266 | static int os_difftime (lua_State *L) {\r | |
267 | lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),\r | |
268 | (time_t)(luaL_optnumber(L, 2, 0))));\r | |
269 | return 1;\r | |
270 | }\r | |
271 | \r | |
272 | /* }====================================================== */\r | |
273 | \r | |
274 | \r | |
275 | static int os_setlocale (lua_State *L) {\r | |
276 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,\r | |
277 | LC_NUMERIC, LC_TIME};\r | |
278 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary",\r | |
279 | "numeric", "time", NULL};\r | |
280 | const char *l = luaL_optstring(L, 1, NULL);\r | |
281 | int op = luaL_checkoption(L, 2, "all", catnames);\r | |
282 | lua_pushstring(L, setlocale(cat[op], l));\r | |
283 | return 1;\r | |
284 | }\r | |
285 | \r | |
286 | \r | |
287 | static int os_exit (lua_State *L) {\r | |
288 | int status;\r | |
289 | if (lua_isboolean(L, 1))\r | |
290 | status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);\r | |
291 | else\r | |
292 | status = luaL_optint(L, 1, EXIT_SUCCESS);\r | |
293 | if (lua_toboolean(L, 2))\r | |
294 | lua_close(L);\r | |
295 | if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */\r | |
296 | return 0;\r | |
297 | }\r | |
298 | \r | |
299 | \r | |
300 | static const luaL_Reg syslib[] = {\r | |
301 | {"clock", os_clock},\r | |
302 | {"date", os_date},\r | |
303 | {"difftime", os_difftime},\r | |
304 | {"execute", os_execute},\r | |
305 | {"exit", os_exit},\r | |
306 | {"getenv", os_getenv},\r | |
307 | {"remove", os_remove},\r | |
308 | {"rename", os_rename},\r | |
309 | {"setlocale", os_setlocale},\r | |
310 | {"time", os_time},\r | |
311 | {"tmpname", os_tmpname},\r | |
312 | {NULL, NULL}\r | |
313 | };\r | |
314 | \r | |
315 | /* }====================================================== */\r | |
316 | \r | |
317 | \r | |
318 | \r | |
319 | LUAMOD_API int luaopen_os (lua_State *L) {\r | |
320 | luaL_newlib(L, syslib);\r | |
321 | return 1;\r | |
322 | }\r | |
323 | \r |