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_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 L_MAXDATEFIELD   (INT_MAX / 2)
 
#define SIZETIMEFMT   250
 

Functions

static time_t l_checktime (lua_State *L, int arg)
 
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)
 
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 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_gmtime

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

Definition at line 92 of file lua-5.3.6/src/loslib.c.

Referenced by os_date().

◆ l_localtime

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

Definition at line 93 of file lua-5.3.6/src/loslib.c.

Referenced by os_date().

◆ L_MAXDATEFIELD

#define L_MAXDATEFIELD   (INT_MAX / 2)

Definition at line 236 of file lua-5.3.6/src/loslib.c.

Referenced by getfield().

◆ l_pushtime

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

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

Referenced by os_time().

◆ L_STRFTIMEC89

#define L_STRFTIMEC89   "aAbBcdHIjmMpSUwWxXyYZ%"

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

◆ L_STRFTIMEC99

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

Definition at line 37 of file lua-5.3.6/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.3.6/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 66 of file lua-5.3.6/src/loslib.c.

Referenced by os_time().

◆ loslib_c

#define loslib_c

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

◆ LUA_LIB

#define LUA_LIB

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

◆ LUA_STRFTIMEOPTIONS

#define LUA_STRFTIMEOPTIONS   L_STRFTIMEC99

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

Referenced by checkoption().

◆ lua_tmpnam

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

Definition at line 131 of file lua-5.3.6/src/loslib.c.

Referenced by os_tmpname().

◆ LUA_TMPNAMBUFSIZE

#define LUA_TMPNAMBUFSIZE   L_tmpnam

Definition at line 130 of file lua-5.3.6/src/loslib.c.

Referenced by os_tmpname().

◆ SIZETIMEFMT

#define SIZETIMEFMT   250

Definition at line 280 of file lua-5.3.6/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 260 of file lua-5.3.6/src/loslib.c.

261 {
262 const char *option = LUA_STRFTIMEOPTIONS;
263 int oplen = 1; /* length of options being checked */
264 for (; *option != '\0' && oplen <= convlen; option += oplen) {
265 if (*option == '|') /* next block? */
266 oplen++; /* will check options with next length (+1) */
267 else if (memcmp(conv, option, oplen) == 0) { /* match? */
268 memcpy(buff, conv, oplen); /* copy valid option to buffer */
269 buff[oplen] = '\0';
270 return conv + oplen; /* return next item */
271 }
272 }
273 luaL_argerror(L, 1,
274 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
275 return conv; /* to avoid warnings */
276}
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 226 of file lua-5.3.6/src/loslib.c.

226 {
227 int res;
228 res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
229 lua_pop(L, 1);
230 return res;
231}
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 239 of file lua-5.3.6/src/loslib.c.

239 {
240 int isnum;
241 int t = lua_getfield(L, -1, key); /* get field and its type */
242 lua_Integer res = lua_tointegerx(L, -1, &isnum);
243 if (!isnum) { /* field is not an integer? */
244 if (t != LUA_TNIL) /* some other value? */
245 return luaL_error(L, "field '%s' is not an integer", key);
246 else if (d < 0) /* absent field; no default? */
247 return luaL_error(L, "field '%s' missing in date table", key);
248 res = d;
249 }
250 else {
251 if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
252 return luaL_error(L, "field '%s' is out-of-bound", key);
253 res -= delta;
254 }
255 lua_pop(L, 1);
256 return (int)res;
257}
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)
#define L_MAXDATEFIELD

References L_MAXDATEFIELD, 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 69 of file lua-5.3.6/src/loslib.c.

69 {
71 luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
72 return (time_t)t;
73}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
#define luaL_argcheck(L, cond, numarg, extramsg)

References arg, luaL_argcheck, and luaL_checkinteger().

Referenced by os_date(), and os_difftime().

◆ luaopen_os()

LUAMOD_API int luaopen_os ( lua_State * L)

Definition at line 405 of file lua-5.3.6/src/loslib.c.

405 {
407 return 1;
408}
#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 183 of file lua-5.3.6/src/loslib.c.

183 {
184 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
185 return 1;
186}
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 283 of file lua-5.3.6/src/loslib.c.

283 {
284 size_t slen;
285 const char *s = luaL_optlstring(L, 1, "%c", &slen);
286 time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
287 const char *se = s + slen; /* 's' end */
288 struct tm tmr, *stm;
289 if (*s == '!') { /* UTC? */
290 stm = l_gmtime(&t, &tmr);
291 s++; /* skip '!' */
292 }
293 else
294 stm = l_localtime(&t, &tmr);
295 if (stm == NULL) /* invalid date? */
296 return luaL_error(L,
297 "time result cannot be represented in this installation");
298 if (strcmp(s, "*t") == 0) {
299 lua_createtable(L, 0, 9); /* 9 = number of fields */
300 setallfields(L, stm);
301 }
302 else {
303 char cc[4]; /* buffer for individual conversion specifiers */
304 luaL_Buffer b;
305 cc[0] = '%';
306 luaL_buffinit(L, &b);
307 while (s < se) {
308 if (*s != '%') /* not a conversion specifier? */
309 luaL_addchar(&b, *s++);
310 else {
311 size_t reslen;
312 char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
313 s++; /* skip '%' */
314 s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
315 reslen = strftime(buff, SIZETIMEFMT, cc, stm);
316 luaL_addsize(&b, reslen);
317 }
318 }
319 luaL_pushresult(&b);
320 }
321 return 1;
322}
#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 351 of file lua-5.3.6/src/loslib.c.

351 {
352 time_t t1 = l_checktime(L, 1);
353 time_t t2 = l_checktime(L, 2);
354 lua_pushnumber(L, (lua_Number)difftime(t1, t2));
355 return 1;
356}

References l_checktime(), and lua_pushnumber().

◆ os_execute()

static int os_execute ( lua_State * L)
static

Definition at line 141 of file lua-5.3.6/src/loslib.c.

141 {
142 const char *cmd = luaL_optstring(L, 1, NULL);
143 int stat = system(cmd);
144 if (cmd != NULL)
145 return luaL_execresult(L, stat);
146 else {
147 lua_pushboolean(L, stat); /* true if there is a shell */
148 return 1;
149 }
150}
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 373 of file lua-5.3.6/src/loslib.c.

373 {
374 int status;
375 if (lua_isboolean(L, 1))
376 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
377 else
378 status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
379 if (lua_toboolean(L, 2))
380 lua_close(L);
381 if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
382 return 0;
383}
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 177 of file lua-5.3.6/src/loslib.c.

177 {
178 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
179 return 1;
180}
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 153 of file lua-5.3.6/src/loslib.c.

153 {
154 const char *filename = luaL_checkstring(L, 1);
155 return luaL_fileresult(L, remove(filename) == 0, filename);
156}
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 159 of file lua-5.3.6/src/loslib.c.

159 {
160 const char *fromname = luaL_checkstring(L, 1);
161 const char *toname = luaL_checkstring(L, 2);
162 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
163}

References luaL_checkstring, luaL_fileresult(), and NULL.

◆ os_setlocale()

static int os_setlocale ( lua_State * L)
static

Definition at line 361 of file lua-5.3.6/src/loslib.c.

361 {
362 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
363 LC_NUMERIC, LC_TIME};
364 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
365 "numeric", "time", NULL};
366 const char *l = luaL_optstring(L, 1, NULL);
367 int op = luaL_checkoption(L, 2, "all", catnames);
368 lua_pushstring(L, setlocale(cat[op], l));
369 return 1;
370}
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 325 of file lua-5.3.6/src/loslib.c.

325 {
326 time_t t;
327 if (lua_isnoneornil(L, 1)) /* called without args? */
328 t = time(NULL); /* get current time */
329 else {
330 struct tm ts;
332 lua_settop(L, 1); /* make sure table is at the top */
333 ts.tm_sec = getfield(L, "sec", 0, 0);
334 ts.tm_min = getfield(L, "min", 0, 0);
335 ts.tm_hour = getfield(L, "hour", 12, 0);
336 ts.tm_mday = getfield(L, "day", -1, 0);
337 ts.tm_mon = getfield(L, "month", -1, 1);
338 ts.tm_year = getfield(L, "year", -1, 1900);
339 ts.tm_isdst = getboolfield(L, "isdst");
340 t = mktime(&ts);
341 setallfields(L, &ts); /* update fields with normalized values */
342 }
343 if (t != (time_t)(l_timet)t || t == (time_t)(-1))
344 return luaL_error(L,
345 "time result cannot be represented in this installation");
346 l_pushtime(L, t);
347 return 1;
348}
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)
#define l_timet
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 166 of file lua-5.3.6/src/loslib.c.

166 {
167 char buff[LUA_TMPNAMBUFSIZE];
168 int err;
169 lua_tmpnam(buff, err);
170 if (err)
171 return luaL_error(L, "unable to generate a unique filename");
172 lua_pushstring(L, buff);
173 return 1;
174}
#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 213 of file lua-5.3.6/src/loslib.c.

213 {
214 setfield(L, "sec", stm->tm_sec);
215 setfield(L, "min", stm->tm_min);
216 setfield(L, "hour", stm->tm_hour);
217 setfield(L, "day", stm->tm_mday);
218 setfield(L, "month", stm->tm_mon + 1);
219 setfield(L, "year", stm->tm_year + 1900);
220 setfield(L, "wday", stm->tm_wday + 1);
221 setfield(L, "yday", stm->tm_yday + 1);
222 setboolfield(L, "isdst", stm->tm_isdst);
223}
static void setboolfield(lua_State *L, const char *key, int value)
static void setfield(lua_State *L, const char *key, int value)

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 202 of file lua-5.3.6/src/loslib.c.

202 {
203 if (value < 0) /* undefined? */
204 return; /* does not set field */
206 lua_setfield(L, -2, key);
207}
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 )
static

Definition at line 197 of file lua-5.3.6/src/loslib.c.

197 {
199 lua_setfield(L, -2, key);
200}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

References lua_pushinteger(), lua_setfield(), 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 386 of file lua-5.3.6/src/loslib.c.

386 {
387 {"clock", os_clock},
388 {"date", os_date},
389 {"difftime", os_difftime},
390 {"execute", os_execute},
391 {"exit", os_exit},
392 {"getenv", os_getenv},
393 {"remove", os_remove},
394 {"rename", os_rename},
395 {"setlocale", os_setlocale},
396 {"time", os_time},
397 {"tmpname", os_tmpname},
398 {NULL, NULL}
399};

Referenced by luaopen_os().