]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/loslib.c
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
26 ** {==================================================================
27 ** list of valid conversion specifiers for the 'strftime' function
28 ** ===================================================================
30 #if !defined(LUA_STRFTIMEOPTIONS) /* { */
32 #if defined(LUA_USE_C89)
33 #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
34 #else /* C99 specification */
35 #define LUA_STRFTIMEOPTIONS \
36 { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
38 "O", "deHImMSuUVwWy" }
42 /* }================================================================== */
46 ** {==================================================================
47 ** Configuration for time-related stuff
48 ** ===================================================================
51 #if !defined(l_time_t) /* { */
53 ** type to represent time_t in Lua
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))
62 #if !defined(l_gmtime) /* { */
64 ** By default, Lua uses gmtime/localtime, except when POSIX is available,
65 ** where it uses gmtime_r/localtime_r
68 #if defined(LUA_USE_POSIX) /* { */
70 #define l_gmtime(t,r) gmtime_r(t,r)
71 #define l_localtime(t,r) localtime_r(t,r)
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))
83 /* }================================================================== */
87 ** {==================================================================
88 ** Configuration for 'tmpnam':
89 ** By default, Lua uses tmpnam except when POSIX is available, where
91 ** ===================================================================
93 #if !defined(lua_tmpnam) /* { */
95 #if defined(LUA_USE_POSIX) /* { */
99 #define LUA_TMPNAMBUFSIZE 32
101 #if !defined(LUA_TMPNAMTEMPLATE)
102 #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
105 #define lua_tmpnam(b,e) { \
106 strcpy(b, LUA_TMPNAMTEMPLATE); \
108 if (e != -1) close(e); \
113 /* ISO C definitions */
114 #define LUA_TMPNAMBUFSIZE L_tmpnam
115 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
120 /* }================================================================== */
125 static int os_execute (lua_State
*L
) {
126 const char *cmd
= luaL_optstring(L
, 1, NULL
);
127 int stat
= system(cmd
);
129 return luaL_execresult(L
, stat
);
131 lua_pushboolean(L
, stat
); /* true if there is a shell */
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
);
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
);
150 static int os_tmpname (lua_State
*L
) {
151 char buff
[LUA_TMPNAMBUFSIZE
];
153 lua_tmpnam(buff
, err
);
155 return luaL_error(L
, "unable to generate a unique filename");
156 lua_pushstring(L
, buff
);
161 static int os_getenv (lua_State
*L
) {
162 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
167 static int os_clock (lua_State
*L
) {
168 lua_pushnumber(L
, ((lua_Number
)clock())/(lua_Number
)CLOCKS_PER_SEC
);
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 ** =======================================================
181 static void setfield (lua_State
*L
, const char *key
, int value
) {
182 lua_pushinteger(L
, value
);
183 lua_setfield(L
, -2, key
);
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
);
193 static int getboolfield (lua_State
*L
, const char *key
) {
195 res
= (lua_getfield(L
, -1, key
) == LUA_TNIL
) ? -1 : lua_toboolean(L
, -1);
201 static int getfield (lua_State
*L
, const char *key
, int d
) {
203 lua_getfield(L
, -1, key
);
204 res
= (int)lua_tointegerx(L
, -1, &isnum
);
207 return luaL_error(L
, "field '%s' missing in date table", key
);
215 static const char *checkoption (lua_State
*L
, const char *conv
, char *buff
) {
216 static const char *const options
[] = LUA_STRFTIMEOPTIONS
;
218 for (i
= 0; i
< sizeof(options
)/sizeof(options
[0]); i
+= 2) {
219 if (*conv
!= '\0' && strchr(options
[i
], *conv
) != NULL
) {
221 if (*options
[i
+ 1] == '\0') { /* one-char conversion specifier? */
222 buff
[2] = '\0'; /* end buffer */
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 */
234 lua_pushfstring(L
, "invalid conversion specifier '%%%s'", conv
));
235 return conv
; /* to avoid warnings */
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
));
243 if (*s
== '!') { /* UTC? */
244 stm
= l_gmtime(&t
, &tmr
);
248 stm
= l_localtime(&t
, &tmr
);
249 if (stm
== NULL
) /* invalid date? */
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
);
267 luaL_buffinit(L
, &b
);
269 if (*s
!= '%') /* no conversion specifier? */
270 luaL_addchar(&b
, *s
++);
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
);
285 static int os_time (lua_State
*L
) {
287 if (lua_isnoneornil(L
, 1)) /* called without args? */
288 t
= time(NULL
); /* get current time */
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");
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))
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
));
319 /* }====================================================== */
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
));
334 static int os_exit (lua_State
*L
) {
336 if (lua_isboolean(L
, 1))
337 status
= (lua_toboolean(L
, 1) ? EXIT_SUCCESS
: EXIT_FAILURE
);
339 status
= (int)luaL_optinteger(L
, 1, EXIT_SUCCESS
);
340 if (lua_toboolean(L
, 2))
342 if (L
) exit(status
); /* 'if' to avoid warnings for unreachable 'return' */
347 static const luaL_Reg syslib
[] = {
350 {"difftime", os_difftime
},
351 {"execute", os_execute
},
353 {"getenv", os_getenv
},
354 {"remove", os_remove
},
355 {"rename", os_rename
},
356 {"setlocale", os_setlocale
},
358 {"tmpname", os_tmpname
},
362 /* }====================================================== */
366 LUAMOD_API
int luaopen_os (lua_State
*L
) {
367 luaL_newlib(L
, syslib
);