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