Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
loslib.c File Reference
#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 LUA_STRFTIMEOPTIONS   { "aAbBcdHIjmMpSUwWxXyYz%", "" }
 
#define LUA_TMPNAMBUFSIZE   L_tmpnam
 
#define lua_tmpnam(b, e)   { e = (tmpnam(b) == NULL); }
 
#define l_gmtime(t, r)   ((void)r, gmtime(t))
 
#define l_localtime(t, r)   ((void)r, localtime(t))
 

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)
 
static void setboolfield (lua_State *L, const char *key, int value)
 
static int getboolfield (lua_State *L, const char *key)
 
static int getfield (lua_State *L, const char *key, int d)
 
static const char * checkoption (lua_State *L, const char *conv, 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, gmtime(t))

Definition at line 73 of file lua-5.2.4/src/loslib.c.

Referenced by os_date().

◆ l_localtime

#define l_localtime ( t,
r )   ((void)r, localtime(t))

Definition at line 74 of file lua-5.2.4/src/loslib.c.

Referenced by os_date().

◆ loslib_c

#define loslib_c

Definition at line 14 of file lua-5.2.4/src/loslib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 15 of file lua-5.2.4/src/loslib.c.

◆ LUA_STRFTIMEOPTIONS

#define LUA_STRFTIMEOPTIONS   { "aAbBcdHIjmMpSUwWxXyYz%", "" }

Definition at line 29 of file lua-5.2.4/src/loslib.c.

Referenced by checkoption().

◆ lua_tmpnam

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

Definition at line 57 of file lua-5.2.4/src/loslib.c.

Referenced by os_tmpname(), and os_tmpname().

◆ LUA_TMPNAMBUFSIZE

#define LUA_TMPNAMBUFSIZE   L_tmpnam

Definition at line 56 of file lua-5.2.4/src/loslib.c.

Referenced by os_tmpname(), and os_tmpname().

Function Documentation

◆ checkoption()

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

Definition at line 171 of file lua-5.2.4/src/loslib.c.

171 {
172 static const char *const options[] = LUA_STRFTIMEOPTIONS;
173 unsigned int i;
174 for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
175 if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
176 buff[1] = *conv;
177 if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
178 buff[2] = '\0'; /* end buffer */
179 return conv + 1;
180 }
181 else if (*(conv + 1) != '\0' &&
182 strchr(options[i + 1], *(conv + 1)) != NULL) {
183 buff[2] = *(conv + 1); /* valid two-char conversion specifier */
184 buff[3] = '\0'; /* end buffer */
185 return conv + 2;
186 }
187 }
188 }
189 luaL_argerror(L, 1,
190 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
191 return conv; /* to avoid warnings */
192}
#define NULL
Definition gmacros.h:924
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, luaL_argerror(), and NULL.

Referenced by os_date().

◆ getboolfield()

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

Definition at line 148 of file lua-5.2.4/src/loslib.c.

148 {
149 int res;
150 lua_getfield(L, -1, key);
151 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
152 lua_pop(L, 1);
153 return res;
154}
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_isnil(L, n)
#define lua_pop(L, n)

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

Referenced by os_time().

◆ getfield()

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

Definition at line 157 of file lua-5.2.4/src/loslib.c.

157 {
158 int res, isnum;
159 lua_getfield(L, -1, key);
160 res = (int)lua_tointegerx(L, -1, &isnum);
161 if (!isnum) {
162 if (d < 0)
163 return luaL_error(L, "field " LUA_QS " missing in date table", key);
164 res = d;
165 }
166 lua_pop(L, 1);
167 return res;
168}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define LUA_QS
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)

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

Referenced by os_time().

◆ luaopen_os()

LUAMOD_API int luaopen_os ( lua_State * L)

Definition at line 319 of file lua-5.2.4/src/loslib.c.

319 {
321 return 1;
322}
#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 122 of file lua-5.2.4/src/loslib.c.

122 {
123 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
124 return 1;
125}
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 195 of file lua-5.2.4/src/loslib.c.

195 {
196 const char *s = luaL_optstring(L, 1, "%c");
197 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
198 struct tm tmr, *stm;
199 if (*s == '!') { /* UTC? */
200 stm = l_gmtime(&t, &tmr);
201 s++; /* skip `!' */
202 }
203 else
204 stm = l_localtime(&t, &tmr);
205 if (stm == NULL) /* invalid date? */
206 lua_pushnil(L);
207 else if (strcmp(s, "*t") == 0) {
208 lua_createtable(L, 0, 9); /* 9 = number of fields */
209 setfield(L, "sec", stm->tm_sec);
210 setfield(L, "min", stm->tm_min);
211 setfield(L, "hour", stm->tm_hour);
212 setfield(L, "day", stm->tm_mday);
213 setfield(L, "month", stm->tm_mon+1);
214 setfield(L, "year", stm->tm_year+1900);
215 setfield(L, "wday", stm->tm_wday+1);
216 setfield(L, "yday", stm->tm_yday+1);
217 setboolfield(L, "isdst", stm->tm_isdst);
218 }
219 else {
220 char cc[4];
221 luaL_Buffer b;
222 cc[0] = '%';
223 luaL_buffinit(L, &b);
224 while (*s) {
225 if (*s != '%') /* no conversion specifier? */
226 luaL_addchar(&b, *s++);
227 else {
228 size_t reslen;
229 char buff[200]; /* should be big enough for any conversion result */
230 s = checkoption(L, s + 1, cc);
231 reslen = strftime(buff, sizeof(buff), cc, stm);
232 luaL_addlstring(&b, buff, reslen);
233 }
234 }
235 luaL_pushresult(&b);
236 }
237 return 1;
238}
LUA_API void lua_pushnil(lua_State *L)
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_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
#define luaL_addchar(B, c)
#define luaL_optstring(L, n, d)
#define luaL_opt(L, f, n, d)
static const char * checkoption(lua_State *L, const char *conv, char *buff)
static void setboolfield(lua_State *L, const char *key, int value)
#define l_localtime(t, r)
static void setfield(lua_State *L, const char *key, int value)
#define l_gmtime(t, r)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References checkoption(), l_gmtime, l_localtime, lua_createtable(), lua_pushnil(), luaL_addchar, luaL_addlstring(), luaL_buffinit(), luaL_checknumber(), luaL_opt, luaL_optstring, luaL_pushresult(), NULL, s, setboolfield(), and setfield().

◆ os_difftime()

static int os_difftime ( lua_State * L)
static

Definition at line 266 of file lua-5.2.4/src/loslib.c.

266 {
267 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
268 (time_t)(luaL_optnumber(L, 2, 0))));
269 return 1;
270}
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)

References lua_pushnumber(), luaL_checknumber(), and luaL_optnumber().

◆ os_execute()

static int os_execute ( lua_State * L)
static

Definition at line 80 of file lua-5.2.4/src/loslib.c.

80 {
81 const char *cmd = luaL_optstring(L, 1, NULL);
82 int stat = system(cmd);
83 if (cmd != NULL)
84 return luaL_execresult(L, stat);
85 else {
86 lua_pushboolean(L, stat); /* true if there is a shell */
87 return 1;
88 }
89}
LUA_API void lua_pushboolean(lua_State *L, int b)
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 287 of file lua-5.2.4/src/loslib.c.

287 {
288 int status;
289 if (lua_isboolean(L, 1))
290 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
291 else
292 status = luaL_optint(L, 1, EXIT_SUCCESS);
293 if (lua_toboolean(L, 2))
294 lua_close(L);
295 if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
296 return 0;
297}
#define luaL_optint(L, n, d)
LUA_API void lua_close(lua_State *L)
#define lua_isboolean(L, n)

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

◆ os_getenv()

static int os_getenv ( lua_State * L)
static

Definition at line 116 of file lua-5.2.4/src/loslib.c.

116 {
117 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
118 return 1;
119}
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 92 of file lua-5.2.4/src/loslib.c.

92 {
93 const char *filename = luaL_checkstring(L, 1);
94 return luaL_fileresult(L, remove(filename) == 0, filename);
95}
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 98 of file lua-5.2.4/src/loslib.c.

98 {
99 const char *fromname = luaL_checkstring(L, 1);
100 const char *toname = luaL_checkstring(L, 2);
101 return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
102}

References luaL_checkstring, luaL_fileresult(), and NULL.

◆ os_setlocale()

static int os_setlocale ( lua_State * L)
static

Definition at line 275 of file lua-5.2.4/src/loslib.c.

275 {
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 const char *l = luaL_optstring(L, 1, NULL);
281 int op = luaL_checkoption(L, 2, "all", catnames);
282 lua_pushstring(L, setlocale(cat[op], l));
283 return 1;
284}
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 241 of file lua-5.2.4/src/loslib.c.

241 {
242 time_t t;
243 if (lua_isnoneornil(L, 1)) /* called without args? */
244 t = time(NULL); /* get current time */
245 else {
246 struct tm ts;
248 lua_settop(L, 1); /* make sure table is at the top */
249 ts.tm_sec = getfield(L, "sec", 0);
250 ts.tm_min = getfield(L, "min", 0);
251 ts.tm_hour = getfield(L, "hour", 12);
252 ts.tm_mday = getfield(L, "day", -1);
253 ts.tm_mon = getfield(L, "month", -1) - 1;
254 ts.tm_year = getfield(L, "year", -1) - 1900;
255 ts.tm_isdst = getboolfield(L, "isdst");
256 t = mktime(&ts);
257 }
258 if (t == (time_t)(-1))
259 lua_pushnil(L);
260 else
262 return 1;
263}
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 getfield(lua_State *L, const char *key, int d)
static int getboolfield(lua_State *L, const char *key)

References getboolfield(), getfield(), lua_isnoneornil, lua_pushnil(), lua_pushnumber(), lua_settop(), LUA_TTABLE, luaL_checktype(), and NULL.

◆ os_tmpname()

static int os_tmpname ( lua_State * L)
static

Definition at line 105 of file lua-5.2.4/src/loslib.c.

105 {
106 char buff[LUA_TMPNAMBUFSIZE];
107 int err;
108 lua_tmpnam(buff, err);
109 if (err)
110 return luaL_error(L, "unable to generate a unique filename");
111 lua_pushstring(L, buff);
112 return 1;
113}
#define LUA_TMPNAMBUFSIZE
#define lua_tmpnam(b, e)

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

◆ setboolfield()

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

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

141 {
142 if (value < 0) /* undefined? */
143 return; /* does not set field */
145 lua_setfield(L, -2, key);
146}
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 os_date().

◆ setfield()

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

Definition at line 136 of file lua-5.2.4/src/loslib.c.

136 {
138 lua_setfield(L, -2, key);
139}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

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

Referenced by os_date().

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

300 {
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};

Referenced by luaopen_os().