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
 

Functions

static int os_pushresult (lua_State *L, int i, const char *filename)
 
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 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)
 
LUALIB_API int luaopen_os (lua_State *L)
 

Variables

static const luaL_Reg syslib []
 

Macro Definition Documentation

◆ loslib_c

#define loslib_c

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

◆ LUA_LIB

#define LUA_LIB

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

Function Documentation

◆ getboolfield()

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

Definition at line 100 of file lua-5.1.5/src/loslib.c.

100 {
101 int res;
102 lua_getfield(L, -1, key);
103 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
104 lua_pop(L, 1);
105 return res;
106}
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 109 of file lua-5.1.5/src/loslib.c.

109 {
110 int res;
111 lua_getfield(L, -1, key);
112 if (lua_isnumber(L, -1))
113 res = (int)lua_tointeger(L, -1);
114 else {
115 if (d < 0)
116 return luaL_error(L, "field " LUA_QS " missing in date table", key);
117 res = d;
118 }
119 lua_pop(L, 1);
120 return res;
121}
LUA_API int lua_isnumber(lua_State *L, int idx)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define LUA_QS
#define lua_tointeger(L, i)

References lua_getfield(), lua_isnumber(), lua_pop, LUA_QS, lua_tointeger, and luaL_error().

Referenced by os_time().

◆ luaopen_os()

LUALIB_API int luaopen_os ( lua_State * L)

Definition at line 239 of file lua-5.1.5/src/loslib.c.

239 {
241 return 1;
242}
LUALIB_API void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
static const luaL_Reg syslib[]
#define LUA_OSLIBNAME

References LUA_OSLIBNAME, luaL_register(), and syslib.

◆ os_clock()

static int os_clock ( lua_State * L)
static

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

74 {
75 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
76 return 1;
77}
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 124 of file lua-5.1.5/src/loslib.c.

124 {
125 const char *s = luaL_optstring(L, 1, "%c");
126 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
127 struct tm *stm;
128 if (*s == '!') { /* UTC? */
129 stm = gmtime(&t);
130 s++; /* skip `!' */
131 }
132 else
133 stm = localtime(&t);
134 if (stm == NULL) /* invalid date? */
135 lua_pushnil(L);
136 else if (strcmp(s, "*t") == 0) {
137 lua_createtable(L, 0, 9); /* 9 = number of fields */
138 setfield(L, "sec", stm->tm_sec);
139 setfield(L, "min", stm->tm_min);
140 setfield(L, "hour", stm->tm_hour);
141 setfield(L, "day", stm->tm_mday);
142 setfield(L, "month", stm->tm_mon+1);
143 setfield(L, "year", stm->tm_year+1900);
144 setfield(L, "wday", stm->tm_wday+1);
145 setfield(L, "yday", stm->tm_yday+1);
146 setboolfield(L, "isdst", stm->tm_isdst);
147 }
148 else {
149 char cc[3];
150 luaL_Buffer b;
151 cc[0] = '%'; cc[2] = '\0';
152 luaL_buffinit(L, &b);
153 for (; *s; s++) {
154 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
155 luaL_addchar(&b, *s);
156 else {
157 size_t reslen;
158 char buff[200]; /* should be big enough for any conversion result */
159 cc[1] = *(++s);
160 reslen = strftime(buff, sizeof(buff), cc, stm);
161 luaL_addlstring(&b, buff, reslen);
162 }
163 }
164 luaL_pushresult(&b);
165 }
166 return 1;
167}
#define NULL
Definition gmacros.h:924
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 void setboolfield(lua_State *L, const char *key, int value)
static void setfield(lua_State *L, const char *key, int value)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References 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 195 of file lua-5.1.5/src/loslib.c.

195 {
196 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
197 (time_t)(luaL_optnumber(L, 2, 0))));
198 return 1;
199}
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 38 of file lua-5.1.5/src/loslib.c.

38 {
39 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
40 return 1;
41}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)

References lua_pushinteger(), luaL_optstring, and NULL.

◆ os_exit()

static int os_exit ( lua_State * L)
static

Definition at line 216 of file lua-5.1.5/src/loslib.c.

216 {
217 exit(luaL_optint(L, 1, EXIT_SUCCESS));
218}
#define luaL_optint(L, n, d)

References luaL_optint.

◆ os_getenv()

static int os_getenv ( lua_State * L)
static

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

68 {
69 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
70 return 1;
71}
LUA_API void lua_pushstring(lua_State *L, const char *s)
#define luaL_checkstring(L, n)

References lua_pushstring(), and luaL_checkstring.

◆ os_pushresult()

static int os_pushresult ( lua_State * L,
int i,
const char * filename )
static

Definition at line 23 of file lua-5.1.5/src/loslib.c.

23 {
24 int en = errno; /* calls to Lua API may change this value */
25 if (i) {
26 lua_pushboolean(L, 1);
27 return 1;
28 }
29 else {
30 lua_pushnil(L);
31 lua_pushfstring(L, "%s: %s", filename, strerror(en));
32 lua_pushinteger(L, en);
33 return 3;
34 }
35}
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API void lua_pushboolean(lua_State *L, int b)

References lua_pushboolean(), lua_pushfstring(), lua_pushinteger(), and lua_pushnil().

Referenced by os_remove(), and os_rename().

◆ os_remove()

static int os_remove ( lua_State * L)
static

Definition at line 44 of file lua-5.1.5/src/loslib.c.

44 {
45 const char *filename = luaL_checkstring(L, 1);
46 return os_pushresult(L, remove(filename) == 0, filename);
47}
static int os_pushresult(lua_State *L, int i, const char *filename)

References luaL_checkstring, and os_pushresult().

◆ os_rename()

static int os_rename ( lua_State * L)
static

Definition at line 50 of file lua-5.1.5/src/loslib.c.

50 {
51 const char *fromname = luaL_checkstring(L, 1);
52 const char *toname = luaL_checkstring(L, 2);
53 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
54}

References luaL_checkstring, and os_pushresult().

◆ os_setlocale()

static int os_setlocale ( lua_State * L)
static

Definition at line 204 of file lua-5.1.5/src/loslib.c.

204 {
205 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
206 LC_NUMERIC, LC_TIME};
207 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
208 "numeric", "time", NULL};
209 const char *l = luaL_optstring(L, 1, NULL);
210 int op = luaL_checkoption(L, 2, "all", catnames);
211 lua_pushstring(L, setlocale(cat[op], l));
212 return 1;
213}
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 170 of file lua-5.1.5/src/loslib.c.

170 {
171 time_t t;
172 if (lua_isnoneornil(L, 1)) /* called without args? */
173 t = time(NULL); /* get current time */
174 else {
175 struct tm ts;
177 lua_settop(L, 1); /* make sure table is at the top */
178 ts.tm_sec = getfield(L, "sec", 0);
179 ts.tm_min = getfield(L, "min", 0);
180 ts.tm_hour = getfield(L, "hour", 12);
181 ts.tm_mday = getfield(L, "day", -1);
182 ts.tm_mon = getfield(L, "month", -1) - 1;
183 ts.tm_year = getfield(L, "year", -1) - 1900;
184 ts.tm_isdst = getboolfield(L, "isdst");
185 t = mktime(&ts);
186 }
187 if (t == (time_t)(-1))
188 lua_pushnil(L);
189 else
191 return 1;
192}
LUA_API void lua_settop(lua_State *L, int idx)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
static int getfield(lua_State *L, const char *key, int d)
static int getboolfield(lua_State *L, const char *key)
#define LUA_TTABLE
#define lua_isnoneornil(L, n)

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

57 {
58 char buff[LUA_TMPNAMBUFSIZE];
59 int err;
60 lua_tmpnam(buff, err);
61 if (err)
62 return luaL_error(L, "unable to generate a unique filename");
63 lua_pushstring(L, buff);
64 return 1;
65}
#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 93 of file lua-5.1.5/src/loslib.c.

93 {
94 if (value < 0) /* undefined? */
95 return; /* does not set field */
97 lua_setfield(L, -2, key);
98}
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 88 of file lua-5.1.5/src/loslib.c.

88 {
90 lua_setfield(L, -2, key);
91}

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

220 {
221 {"clock", os_clock},
222 {"date", os_date},
223 {"difftime", os_difftime},
224 {"execute", os_execute},
225 {"exit", os_exit},
226 {"getenv", os_getenv},
227 {"remove", os_remove},
228 {"rename", os_rename},
229 {"setlocale", os_setlocale},
230 {"time", os_time},
231 {"tmpname", os_tmpname},
232 {NULL, NULL}
233};

Referenced by luaopen_os().