1 /* $NetBSD: loslib.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */
4 ** Id: loslib.c,v 1.57 2015/04/10 17:41:04 roberto Exp
5 ** Standard Operating System library
6 ** See Copyright Notice in lua.h
28 ** {==================================================================
29 ** list of valid conversion specifiers for the 'strftime' function
30 ** ===================================================================
32 #if !defined(LUA_STRFTIMEOPTIONS) /* { */
34 #if defined(LUA_USE_C89)
35 #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
36 #else /* C99 specification */
37 #define LUA_STRFTIMEOPTIONS \
38 { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
40 "O", "deHImMSuUVwWy" }
44 /* }================================================================== */
48 ** {==================================================================
49 ** Configuration for time-related stuff
50 ** ===================================================================
53 #if !defined(l_time_t) /* { */
55 ** type to represent time_t in Lua
57 #define l_timet lua_Integer
58 #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
59 #define l_checktime(L,a) ((time_t)luaL_checkinteger(L,a))
64 #if !defined(l_gmtime) /* { */
66 ** By default, Lua uses gmtime/localtime, except when POSIX is available,
67 ** where it uses gmtime_r/localtime_r
70 #if defined(LUA_USE_POSIX) /* { */
72 #define l_gmtime(t,r) gmtime_r(t,r)
73 #define l_localtime(t,r) localtime_r(t,r)
77 /* ISO C definitions */
78 #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
79 #define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
85 /* }================================================================== */
89 ** {==================================================================
90 ** Configuration for 'tmpnam':
91 ** By default, Lua uses tmpnam except when POSIX is available, where
93 ** ===================================================================
95 #if !defined(lua_tmpnam) /* { */
97 #if defined(LUA_USE_POSIX) /* { */
101 #define LUA_TMPNAMBUFSIZE 32
103 #if !defined(LUA_TMPNAMTEMPLATE)
104 #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
107 #define lua_tmpnam(b,e) { \
108 strcpy(b, LUA_TMPNAMTEMPLATE); \
110 if (e != -1) close(e); \
115 /* ISO C definitions */
116 #define LUA_TMPNAMBUFSIZE L_tmpnam
117 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
122 /* }================================================================== */
127 static int os_execute (lua_State
*L
) {
128 const char *cmd
= luaL_optstring(L
, 1, NULL
);
129 int stat
= system(cmd
);
131 return luaL_execresult(L
, stat
);
133 lua_pushboolean(L
, stat
); /* true if there is a shell */
139 static int os_remove (lua_State
*L
) {
140 const char *filename
= luaL_checkstring(L
, 1);
141 return luaL_fileresult(L
, remove(filename
) == 0, filename
);
145 static int os_rename (lua_State
*L
) {
146 const char *fromname
= luaL_checkstring(L
, 1);
147 const char *toname
= luaL_checkstring(L
, 2);
148 return luaL_fileresult(L
, rename(fromname
, toname
) == 0, NULL
);
152 static int os_tmpname (lua_State
*L
) {
153 char buff
[LUA_TMPNAMBUFSIZE
];
155 lua_tmpnam(buff
, err
);
157 return luaL_error(L
, "unable to generate a unique filename");
158 lua_pushstring(L
, buff
);
163 static int os_getenv (lua_State
*L
) {
164 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
169 static int os_clock (lua_State
*L
) {
170 lua_pushnumber(L
, ((lua_Number
)clock())/(lua_Number
)CLOCKS_PER_SEC
);
176 ** {======================================================
177 ** Time/Date operations
178 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
179 ** wday=%w+1, yday=%j, isdst=? }
180 ** =======================================================
183 static void setfield (lua_State
*L
, const char *key
, int value
) {
184 lua_pushinteger(L
, value
);
185 lua_setfield(L
, -2, key
);
188 static void setboolfield (lua_State
*L
, const char *key
, int value
) {
189 if (value
< 0) /* undefined? */
190 return; /* does not set field */
191 lua_pushboolean(L
, value
);
192 lua_setfield(L
, -2, key
);
195 static int getboolfield (lua_State
*L
, const char *key
) {
197 res
= (lua_getfield(L
, -1, key
) == LUA_TNIL
) ? -1 : lua_toboolean(L
, -1);
203 static int getfield (lua_State
*L
, const char *key
, int d
) {
205 lua_getfield(L
, -1, key
);
206 res
= (int)lua_tointegerx(L
, -1, &isnum
);
209 return luaL_error(L
, "field '%s' missing in date table", key
);
217 static const char *checkoption (lua_State
*L
, const char *conv
, char *buff
) {
218 static const char *const options
[] = LUA_STRFTIMEOPTIONS
;
220 for (i
= 0; i
< sizeof(options
)/sizeof(options
[0]); i
+= 2) {
221 if (*conv
!= '\0' && strchr(options
[i
], *conv
) != NULL
) {
223 if (*options
[i
+ 1] == '\0') { /* one-char conversion specifier? */
224 buff
[2] = '\0'; /* end buffer */
227 else if (*(conv
+ 1) != '\0' &&
228 strchr(options
[i
+ 1], *(conv
+ 1)) != NULL
) {
229 buff
[2] = *(conv
+ 1); /* valid two-char conversion specifier */
230 buff
[3] = '\0'; /* end buffer */
236 lua_pushfstring(L
, "invalid conversion specifier '%%%s'", conv
));
237 return conv
; /* to avoid warnings */
241 static int os_date (lua_State
*L
) {
242 const char *s
= luaL_optstring(L
, 1, "%c");
243 time_t t
= luaL_opt(L
, l_checktime
, 2, time(NULL
));
245 if (*s
== '!') { /* UTC? */
246 stm
= l_gmtime(&t
, &tmr
);
250 stm
= l_localtime(&t
, &tmr
);
251 if (stm
== NULL
) /* invalid date? */
253 else if (strcmp(s
, "*t") == 0) {
254 lua_createtable(L
, 0, 9); /* 9 = number of fields */
255 setfield(L
, "sec", stm
->tm_sec
);
256 setfield(L
, "min", stm
->tm_min
);
257 setfield(L
, "hour", stm
->tm_hour
);
258 setfield(L
, "day", stm
->tm_mday
);
259 setfield(L
, "month", stm
->tm_mon
+1);
260 setfield(L
, "year", stm
->tm_year
+1900);
261 setfield(L
, "wday", stm
->tm_wday
+1);
262 setfield(L
, "yday", stm
->tm_yday
+1);
263 setboolfield(L
, "isdst", stm
->tm_isdst
);
269 luaL_buffinit(L
, &b
);
271 if (*s
!= '%') /* no conversion specifier? */
272 luaL_addchar(&b
, *s
++);
275 char buff
[200]; /* should be big enough for any conversion result */
276 s
= checkoption(L
, s
+ 1, cc
);
277 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
278 luaL_addlstring(&b
, buff
, reslen
);
287 static int os_time (lua_State
*L
) {
289 if (lua_isnoneornil(L
, 1)) /* called without args? */
290 t
= time(NULL
); /* get current time */
293 luaL_checktype(L
, 1, LUA_TTABLE
);
294 lua_settop(L
, 1); /* make sure table is at the top */
295 ts
.tm_sec
= getfield(L
, "sec", 0);
296 ts
.tm_min
= getfield(L
, "min", 0);
297 ts
.tm_hour
= getfield(L
, "hour", 12);
298 ts
.tm_mday
= getfield(L
, "day", -1);
299 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
300 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
301 ts
.tm_isdst
= getboolfield(L
, "isdst");
304 if (t
!= (time_t)(l_timet
)t
)
305 luaL_error(L
, "time result cannot be represented in this Lua installation");
306 else if (t
== (time_t)(-1))
314 static int os_difftime (lua_State
*L
) {
315 time_t t1
= l_checktime(L
, 1);
316 time_t t2
= l_checktime(L
, 2);
317 lua_pushnumber(L
, (lua_Number
)difftime(t1
, t2
));
321 /* }====================================================== */
324 static int os_setlocale (lua_State
*L
) {
325 static const int cat
[] = {LC_ALL
, LC_COLLATE
, LC_CTYPE
, LC_MONETARY
,
326 LC_NUMERIC
, LC_TIME
};
327 static const char *const catnames
[] = {"all", "collate", "ctype", "monetary",
328 "numeric", "time", NULL
};
329 const char *l
= luaL_optstring(L
, 1, NULL
);
330 int op
= luaL_checkoption(L
, 2, "all", catnames
);
331 lua_pushstring(L
, setlocale(cat
[op
], l
));
336 static int os_exit (lua_State
*L
) {
338 if (lua_isboolean(L
, 1))
339 status
= (lua_toboolean(L
, 1) ? EXIT_SUCCESS
: EXIT_FAILURE
);
341 status
= (int)luaL_optinteger(L
, 1, EXIT_SUCCESS
);
342 if (lua_toboolean(L
, 2))
344 if (L
) exit(status
); /* 'if' to avoid warnings for unreachable 'return' */
349 static const luaL_Reg syslib
[] = {
352 {"difftime", os_difftime
},
353 {"execute", os_execute
},
355 {"getenv", os_getenv
},
356 {"remove", os_remove
},
357 {"rename", os_rename
},
358 {"setlocale", os_setlocale
},
360 {"tmpname", os_tmpname
},
364 /* }====================================================== */
368 LUAMOD_API
int luaopen_os (lua_State
*L
) {
369 luaL_newlib(L
, syslib
);