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