Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
loslib.c File Reference
#include "lprefix.h"
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define loslib_c
 
#define LUA_LIB
 
#define L_STRFTIMEC89   "aAbBcdHIjmMpSUwWxXyYZ%"
 
#define L_STRFTIMEC99
 
#define L_STRFTIMEWIN
 
#define LUA_STRFTIMEOPTIONS   L_STRFTIMEC99
 
#define l_timet   lua_Integer
 
#define l_pushtime(L, t)   lua_pushinteger(L,(lua_Integer)(t))
 
#define l_gettime(L, arg)   luaL_checkinteger(L, arg)
 
#define l_gmtime(t, r)   ((void)(r)->tm_sec, gmtime(t))
 
#define l_localtime(t, r)   ((void)(r)->tm_sec, localtime(t))
 
#define LUA_TMPNAMBUFSIZE   L_tmpnam
 
#define lua_tmpnam(b, e)   { e = (tmpnam(b) == NULL); }
 
#define SIZETIMEFMT   250
 

Functions

static int os_execute (lua_State *L)
 
static int os_remove (lua_State *L)
 
static int os_rename (lua_State *L)
 
static int os_tmpname (lua_State *L)
 
static int os_getenv (lua_State *L)
 
static int os_clock (lua_State *L)
 
static void setfield (lua_State *L, const char *key, int value, int delta)
 
static void setboolfield (lua_State *L, const char *key, int value)
 
static void setallfields (lua_State *L, struct tm *stm)
 
static int getboolfield (lua_State *L, const char *key)
 
static int getfield (lua_State *L, const char *key, int d, int delta)
 
static const char * checkoption (lua_State *L, const char *conv, ptrdiff_t convlen, char *buff)
 
static time_t l_checktime (lua_State *L, int arg)
 
static int os_date (lua_State *L)
 
static int os_time (lua_State *L)
 
static int os_difftime (lua_State *L)
 
static int os_setlocale (lua_State *L)
 
static int os_exit (lua_State *L)
 
LUAMOD_API int luaopen_os (lua_State *L)
 

Variables

static const luaL_Reg syslib []
 

Macro Definition Documentation

◆ l_gettime

#define l_gettime ( L,
arg )   luaL_checkinteger(L, arg)

Definition at line 69 of file lua-5.4.3/src/loslib.c.

Referenced by l_checktime().

◆ l_gmtime

#define l_gmtime ( t,
r )   ((void)(r)->tm_sec, gmtime(t))

Definition at line 94 of file lua-5.4.3/src/loslib.c.

Referenced by os_date().

◆ l_localtime

#define l_localtime ( t,
r )   ((void)(r)->tm_sec, localtime(t))

Definition at line 95 of file lua-5.4.3/src/loslib.c.

Referenced by os_date().

◆ l_pushtime

#define l_pushtime ( L,
t )   lua_pushinteger(L,(lua_Integer)(t))

Definition at line 68 of file lua-5.4.3/src/loslib.c.

Referenced by os_time().

◆ L_STRFTIMEC89

#define L_STRFTIMEC89   "aAbBcdHIjmMpSUwWxXyYZ%"

Definition at line 34 of file lua-5.4.3/src/loslib.c.

◆ L_STRFTIMEC99

#define L_STRFTIMEC99
Value:
"aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
"||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */

Definition at line 37 of file lua-5.4.3/src/loslib.c.

37#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
38 "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */

◆ L_STRFTIMEWIN

#define L_STRFTIMEWIN
Value:
"aAbBcdHIjmMpSUwWxXyYzZ%" \
"||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */

Definition at line 41 of file lua-5.4.3/src/loslib.c.

41#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
42 "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */

◆ l_timet

#define l_timet   lua_Integer

Definition at line 67 of file lua-5.4.3/src/loslib.c.

Referenced by l_checktime(), and os_time().

◆ loslib_c

#define loslib_c

Definition at line 7 of file lua-5.4.3/src/loslib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.4.3/src/loslib.c.

◆ LUA_STRFTIMEOPTIONS

#define LUA_STRFTIMEOPTIONS   L_STRFTIMEC99

Definition at line 49 of file lua-5.4.3/src/loslib.c.

Referenced by checkoption().

◆ lua_tmpnam

#define lua_tmpnam ( b,
e )   { e = (tmpnam(b) == NULL); }

Definition at line 133 of file lua-5.4.3/src/loslib.c.

Referenced by os_tmpname().

◆ LUA_TMPNAMBUFSIZE

#define LUA_TMPNAMBUFSIZE   L_tmpnam

Definition at line 132 of file lua-5.4.3/src/loslib.c.

Referenced by os_tmpname().

◆ SIZETIMEFMT

#define SIZETIMEFMT   250

Definition at line 301 of file lua-5.4.3/src/loslib.c.

Referenced by os_date().

Function Documentation

◆ checkoption()

static const char * checkoption ( lua_State * L,
const char * conv,
ptrdiff_t convlen,
char * buff )
static

Definition at line 274 of file lua-5.4.3/src/loslib.c.

275 {
276 const char *option = LUA_STRFTIMEOPTIONS;
277 int oplen = 1; /* length of options being checked */
278 for (; *option != '\0' && oplen <= convlen; option += oplen) {
279 if (*option == '|') /* next block? */
280 oplen++; /* will check options with next length (+1) */
281 else if (memcmp(conv, option, oplen) == 0) { /* match? */
282 memcpy(buff, conv, oplen); /* copy valid option to buffer */
283 buff[oplen] = '\0';
284 return conv + oplen; /* return next item */
285 }
286 }
287 luaL_argerror(L, 1,
288 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
289 return conv; /* to avoid warnings */
290}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
#define LUA_STRFTIMEOPTIONS

References lua_pushfstring(), LUA_STRFTIMEOPTIONS, and luaL_argerror().

Referenced by os_date().

◆ getboolfield()

static int getboolfield ( lua_State * L,
const char * key )
static

Definition at line 243 of file lua-5.4.3/src/loslib.c.

243 {
244 int res;
245 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
246 lua_pop(L, 1);
247 return res;
248}
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define lua_pop(L, n)
#define LUA_TNIL

References lua_getfield(), lua_pop, LUA_TNIL, and lua_toboolean().

Referenced by os_time().

◆ getfield()

static int getfield ( lua_State * L,
const char * key,
int d,
int delta )
static

Definition at line 251 of file lua-5.4.3/src/loslib.c.

251 {
252 int isnum;
253 int t = lua_getfield(L, -1, key); /* get field and its type */
254 lua_Integer res = lua_tointegerx(L, -1, &isnum);
255 if (!isnum) { /* field is not an integer? */
256 if (l_unlikely(t != LUA_TNIL)) /* some other value? */
257 return luaL_error(L, "field '%s' is not an integer", key);
258 else if (l_unlikely(d < 0)) /* absent field; no default? */
259 return luaL_error(L, "field '%s' missing in date table", key);
260 res = d;
261 }
262 else {
263 /* unsigned avoids overflow when lua_Integer has 32 bits */
264 if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta
265 : (lua_Integer)INT_MIN + delta <= res))
266 return luaL_error(L, "field '%s' is out-of-bound", key);
267 res -= delta;
268 }
269 lua_pop(L, 1);
270 return (int)res;
271}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUA_INTEGER lua_Integer
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)
LUA_UNSIGNED lua_Unsigned

References lua_getfield(), lua_pop, LUA_TNIL, lua_tointegerx(), and luaL_error().

Referenced by os_time().

◆ l_checktime()

static time_t l_checktime ( lua_State * L,
int arg )
static

Definition at line 293 of file lua-5.4.3/src/loslib.c.

293 {
294 l_timet t = l_gettime(L, arg);
295 luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
296 return (time_t)t;
297}
CURL_EXTERN int void * arg
Definition curl.h:2622
#define luaL_argcheck(L, cond, numarg, extramsg)
#define l_gettime(L, arg)
#define l_timet

References arg, l_gettime, l_timet, and luaL_argcheck.

Referenced by os_date(), and os_difftime().

◆ luaopen_os()

LUAMOD_API int luaopen_os ( lua_State * L)

Definition at line 426 of file lua-5.4.3/src/loslib.c.

426 {
428 return 1;
429}
#define luaL_newlib(L, l)
static const luaL_Reg syslib[]

References luaL_newlib, and syslib.

◆ os_clock()

static int os_clock ( lua_State * L)
static

Definition at line 186 of file lua-5.4.3/src/loslib.c.

186 {
187 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
188 return 1;
189}
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_NUMBER lua_Number

References lua_pushnumber().

◆ os_date()

static int os_date ( lua_State * L)
static

Definition at line 304 of file lua-5.4.3/src/loslib.c.

304 {
305 size_t slen;
306 const char *s = luaL_optlstring(L, 1, "%c", &slen);
307 time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
308 const char *se = s + slen; /* 's' end */
309 struct tm tmr, *stm;
310 if (*s == '!') { /* UTC? */
311 stm = l_gmtime(&t, &tmr);
312 s++; /* skip '!' */
313 }
314 else
315 stm = l_localtime(&t, &tmr);
316 if (stm == NULL) /* invalid date? */
317 return luaL_error(L,
318 "date result cannot be represented in this installation");
319 if (strcmp(s, "*t") == 0) {
320 lua_createtable(L, 0, 9); /* 9 = number of fields */
321 setallfields(L, stm);
322 }
323 else {
324 char cc[4]; /* buffer for individual conversion specifiers */
325 luaL_Buffer b;
326 cc[0] = '%';
327 luaL_buffinit(L, &b);
328 while (s < se) {
329 if (*s != '%') /* not a conversion specifier? */
330 luaL_addchar(&b, *s++);
331 else {
332 size_t reslen;
333 char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
334 s++; /* skip '%' */
335 s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
336 reslen = strftime(buff, SIZETIMEFMT, cc, stm);
337 luaL_addsize(&b, reslen);
338 }
339 }
340 luaL_pushresult(&b);
341 }
342 return 1;
343}
#define NULL
Definition gmacros.h:924
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API const char * luaL_optlstring(lua_State *L, int narg, const char *def, size_t *len)
#define luaL_addsize(B, n)
#define luaL_addchar(B, c)
#define luaL_opt(L, f, n, d)
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
#define SIZETIMEFMT
#define l_localtime(t, r)
#define l_gmtime(t, r)
static const char * checkoption(lua_State *L, const char *conv, ptrdiff_t convlen, char *buff)
static void setallfields(lua_State *L, struct tm *stm)
static time_t l_checktime(lua_State *L, int arg)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References checkoption(), l_checktime(), l_gmtime, l_localtime, lua_createtable(), luaL_addchar, luaL_addsize, luaL_buffinit(), luaL_error(), luaL_opt, luaL_optlstring(), luaL_prepbuffsize(), luaL_pushresult(), NULL, s, setallfields(), and SIZETIMEFMT.

◆ os_difftime()

static int os_difftime ( lua_State * L)
static

Definition at line 372 of file lua-5.4.3/src/loslib.c.

372 {
373 time_t t1 = l_checktime(L, 1);
374 time_t t2 = l_checktime(L, 2);
375 lua_pushnumber(L, (lua_Number)difftime(t1, t2));
376 return 1;
377}

References l_checktime(), and lua_pushnumber().

◆ os_execute()

static int os_execute ( lua_State * L)
static

Definition at line 142 of file lua-5.4.3/src/loslib.c.

142 {
143 const char *cmd = luaL_optstring(L, 1, NULL);
144 int stat;
145 errno = 0;
146 stat = system(cmd);
147 if (cmd != NULL)
148 return luaL_execresult(L, stat);
149 else {
150 lua_pushboolean(L, stat); /* true if there is a shell */
151 return 1;
152 }
153}
LUA_API void lua_pushboolean(lua_State *L, int b)
#define luaL_optstring(L, n, d)
LUALIB_API int luaL_execresult(lua_State *L, int stat)

References lua_pushboolean(), luaL_execresult(), luaL_optstring, and NULL.

◆ os_exit()

static int os_exit ( lua_State * L)
static

Definition at line 394 of file lua-5.4.3/src/loslib.c.

394 {
395 int status;
396 if (lua_isboolean(L, 1))
397 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
398 else
399 status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
400 if (lua_toboolean(L, 2))
401 lua_close(L);
402 if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
403 return 0;
404}
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUA_API void lua_close(lua_State *L)
#define lua_isboolean(L, n)

References lua_close(), lua_isboolean, lua_toboolean(), and luaL_optinteger().

◆ os_getenv()

static int os_getenv ( lua_State * L)
static

Definition at line 180 of file lua-5.4.3/src/loslib.c.

180 {
181 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
182 return 1;
183}
LUA_API void lua_pushstring(lua_State *L, const char *s)
#define luaL_checkstring(L, n)

References lua_pushstring(), and luaL_checkstring.

◆ os_remove()

static int os_remove ( lua_State * L)
static

Definition at line 156 of file lua-5.4.3/src/loslib.c.

156 {
157 const char *filename = luaL_checkstring(L, 1);
158 return luaL_fileresult(L, remove(filename) == 0, filename);
159}
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)

References luaL_checkstring, and luaL_fileresult().

◆ os_rename()

static int os_rename ( lua_State * L)
static

Definition at line 162 of file lua-5.4.3/src/loslib.c.

162 {
163 const char *fromname = luaL_checkstring(L, 1);
164 const char *toname = luaL_checkstring(L, 2);
165 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
166}

References luaL_checkstring, luaL_fileresult(), and NULL.

◆ os_setlocale()

static int os_setlocale ( lua_State * L)
static

Definition at line 382 of file lua-5.4.3/src/loslib.c.

382 {
383 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
384 LC_NUMERIC, LC_TIME};
385 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
386 "numeric", "time", NULL};
387 const char *l = luaL_optstring(L, 1, NULL);
388 int op = luaL_checkoption(L, 2, "all", catnames);
389 lua_pushstring(L, setlocale(cat[op], l));
390 return 1;
391}
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])

References lua_pushstring(), luaL_checkoption(), luaL_optstring, and NULL.

◆ os_time()

static int os_time ( lua_State * L)
static

Definition at line 346 of file lua-5.4.3/src/loslib.c.

346 {
347 time_t t;
348 if (lua_isnoneornil(L, 1)) /* called without args? */
349 t = time(NULL); /* get current time */
350 else {
351 struct tm ts;
353 lua_settop(L, 1); /* make sure table is at the top */
354 ts.tm_year = getfield(L, "year", -1, 1900);
355 ts.tm_mon = getfield(L, "month", -1, 1);
356 ts.tm_mday = getfield(L, "day", -1, 0);
357 ts.tm_hour = getfield(L, "hour", 12, 0);
358 ts.tm_min = getfield(L, "min", 0, 0);
359 ts.tm_sec = getfield(L, "sec", 0, 0);
360 ts.tm_isdst = getboolfield(L, "isdst");
361 t = mktime(&ts);
362 setallfields(L, &ts); /* update fields with normalized values */
363 }
364 if (t != (time_t)(l_timet)t || t == (time_t)(-1))
365 return luaL_error(L,
366 "time result cannot be represented in this installation");
367 l_pushtime(L, t);
368 return 1;
369}
LUA_API void lua_settop(lua_State *L, int idx)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
#define LUA_TTABLE
#define lua_isnoneornil(L, n)
static int getboolfield(lua_State *L, const char *key)
static int getfield(lua_State *L, const char *key, int d, int delta)
#define l_pushtime(L, t)

References getboolfield(), getfield(), l_pushtime, l_timet, lua_isnoneornil, lua_settop(), LUA_TTABLE, luaL_checktype(), luaL_error(), NULL, and setallfields().

◆ os_tmpname()

static int os_tmpname ( lua_State * L)
static

Definition at line 169 of file lua-5.4.3/src/loslib.c.

169 {
170 char buff[LUA_TMPNAMBUFSIZE];
171 int err;
172 lua_tmpnam(buff, err);
173 if (l_unlikely(err))
174 return luaL_error(L, "unable to generate a unique filename");
175 lua_pushstring(L, buff);
176 return 1;
177}
#define LUA_TMPNAMBUFSIZE
#define lua_tmpnam(b, e)

References lua_pushstring(), lua_tmpnam, LUA_TMPNAMBUFSIZE, and luaL_error().

◆ setallfields()

static void setallfields ( lua_State * L,
struct tm * stm )
static

Definition at line 230 of file lua-5.4.3/src/loslib.c.

230 {
231 setfield(L, "year", stm->tm_year, 1900);
232 setfield(L, "month", stm->tm_mon, 1);
233 setfield(L, "day", stm->tm_mday, 0);
234 setfield(L, "hour", stm->tm_hour, 0);
235 setfield(L, "min", stm->tm_min, 0);
236 setfield(L, "sec", stm->tm_sec, 0);
237 setfield(L, "yday", stm->tm_yday, 1);
238 setfield(L, "wday", stm->tm_wday, 1);
239 setboolfield(L, "isdst", stm->tm_isdst);
240}
static void setboolfield(lua_State *L, const char *key, int value)
static void setfield(lua_State *L, const char *key, int value, int delta)

References setboolfield(), and setfield().

Referenced by os_date(), and os_time().

◆ setboolfield()

static void setboolfield ( lua_State * L,
const char * key,
int value )
static

Definition at line 219 of file lua-5.4.3/src/loslib.c.

219 {
220 if (value < 0) /* undefined? */
221 return; /* does not set field */
223 lua_setfield(L, -2, key);
224}
int value
Definition lsqlite3.c:2155
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)

References lua_pushboolean(), lua_setfield(), and value.

Referenced by setallfields().

◆ setfield()

static void setfield ( lua_State * L,
const char * key,
int value,
int delta )
static

Definition at line 209 of file lua-5.4.3/src/loslib.c.

209 {
210 #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX)
211 if (l_unlikely(value > LUA_MAXINTEGER - delta))
212 luaL_error(L, "field '%s' is out-of-bound", key);
213 #endif
214 lua_pushinteger(L, (lua_Integer)value + delta);
215 lua_setfield(L, -2, key);
216}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

References lua_pushinteger(), lua_setfield(), luaL_error(), and value.

Referenced by setallfields().

Variable Documentation

◆ syslib

const luaL_Reg syslib[]
static
Initial value:
= {
{"clock", os_clock},
{"date", os_date},
{"difftime", os_difftime},
{"execute", os_execute},
{"exit", os_exit},
{"getenv", os_getenv},
{"remove", os_remove},
{"rename", os_rename},
{"setlocale", os_setlocale},
{"time", os_time},
{"tmpname", os_tmpname},
}
static int os_setlocale(lua_State *L)
static int os_execute(lua_State *L)
static int os_getenv(lua_State *L)
static int os_date(lua_State *L)
static int os_tmpname(lua_State *L)
static int os_exit(lua_State *L)
static int os_rename(lua_State *L)
static int os_difftime(lua_State *L)
static int os_clock(lua_State *L)
static int os_time(lua_State *L)
static int os_remove(lua_State *L)

Definition at line 407 of file lua-5.4.3/src/loslib.c.

407 {
408 {"clock", os_clock},
409 {"date", os_date},
410 {"difftime", os_difftime},
411 {"execute", os_execute},
412 {"exit", os_exit},
413 {"getenv", os_getenv},
414 {"remove", os_remove},
415 {"rename", os_rename},
416 {"setlocale", os_setlocale},
417 {"time", os_time},
418 {"tmpname", os_tmpname},
419 {NULL, NULL}
420};

Referenced by luaopen_os().