3 ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
36 /* ------------------------------------------------------------------------ */
38 #define LJLIB_MODULE_os
45 return luaL_fileresult(L
, 0, NULL
);
47 lua_pushinteger(L
, -1);
51 const char *cmd
= luaL_optstring(L
, 1, NULL
);
52 int stat
= system(cmd
);
55 return luaL_execresult(L
, stat
);
56 setboolV(L
->top
++, 1);
58 setintV(L
->top
++, stat
);
66 const char *filename
= luaL_checkstring(L
, 1);
67 return luaL_fileresult(L
, remove(filename
) == 0, filename
);
72 const char *fromname
= luaL_checkstring(L
, 1);
73 const char *toname
= luaL_checkstring(L
, 2);
74 return luaL_fileresult(L
, rename(fromname
, toname
) == 0, fromname
);
79 #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PSVITA
80 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
86 strcpy(buf
, "/tmp/lua_XXXXXX");
91 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
94 if (tmpnam(buf
) == NULL
)
95 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
97 lua_pushstring(L
, buf
);
104 #if LJ_TARGET_CONSOLE
107 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
115 if (L
->base
< L
->top
&& tvisbool(L
->base
))
116 status
= boolV(L
->base
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
118 status
= lj_lib_optint(L
, 1, EXIT_SUCCESS
);
119 if (L
->base
+1 < L
->top
&& tvistruecond(L
->base
+1))
122 return 0; /* Unreachable. */
127 setnumV(L
->top
++, ((lua_Number
)clock())*(1.0/(lua_Number
)CLOCKS_PER_SEC
));
131 /* ------------------------------------------------------------------------ */
133 static void setfield(lua_State
*L
, const char *key
, int value
)
135 lua_pushinteger(L
, value
);
136 lua_setfield(L
, -2, key
);
139 static void setboolfield(lua_State
*L
, const char *key
, int value
)
141 if (value
< 0) /* undefined? */
142 return; /* does not set field */
143 lua_pushboolean(L
, value
);
144 lua_setfield(L
, -2, key
);
147 static int getboolfield(lua_State
*L
, const char *key
)
150 lua_getfield(L
, -1, key
);
151 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
156 static int getfield(lua_State
*L
, const char *key
, int d
)
159 lua_getfield(L
, -1, key
);
160 if (lua_isnumber(L
, -1)) {
161 res
= (int)lua_tointeger(L
, -1);
164 lj_err_callerv(L
, LJ_ERR_OSDATEF
, key
);
173 const char *s
= luaL_optstring(L
, 1, "%c");
174 time_t t
= luaL_opt(L
, (time_t)luaL_checknumber
, 2, time(NULL
));
179 if (*s
== '!') { /* UTC? */
182 stm
= gmtime_r(&t
, &rtm
);
188 stm
= localtime_r(&t
, &rtm
);
193 if (stm
== NULL
) { /* Invalid date? */
195 } else if (strcmp(s
, "*t") == 0) {
196 lua_createtable(L
, 0, 9); /* 9 = number of fields */
197 setfield(L
, "sec", stm
->tm_sec
);
198 setfield(L
, "min", stm
->tm_min
);
199 setfield(L
, "hour", stm
->tm_hour
);
200 setfield(L
, "day", stm
->tm_mday
);
201 setfield(L
, "month", stm
->tm_mon
+1);
202 setfield(L
, "year", stm
->tm_year
+1900);
203 setfield(L
, "wday", stm
->tm_wday
+1);
204 setfield(L
, "yday", stm
->tm_yday
+1);
205 setboolfield(L
, "isdst", stm
->tm_isdst
);
207 SBuf
*sb
= &G(L
)->tmpbuf
;
211 sz
+= (*q
== '%') ? 30 : 1; /* Overflow doesn't matter. */
214 char *buf
= lj_buf_need(sb
, sz
);
215 size_t len
= strftime(buf
, sbufsz(sb
), s
, stm
);
217 setstrV(L
, L
->top
++, lj_str_new(L
, buf
, len
));
224 setstrV(L
, L
->top
++, &G(L
)->strempty
);
232 if (lua_isnoneornil(L
, 1)) { /* called without args? */
233 t
= time(NULL
); /* get current time */
236 luaL_checktype(L
, 1, LUA_TTABLE
);
237 lua_settop(L
, 1); /* make sure table is at the top */
238 ts
.tm_sec
= getfield(L
, "sec", 0);
239 ts
.tm_min
= getfield(L
, "min", 0);
240 ts
.tm_hour
= getfield(L
, "hour", 12);
241 ts
.tm_mday
= getfield(L
, "day", -1);
242 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
243 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
244 ts
.tm_isdst
= getboolfield(L
, "isdst");
247 if (t
== (time_t)(-1))
250 lua_pushnumber(L
, (lua_Number
)t
);
254 LJLIB_CF(os_difftime
)
256 lua_pushnumber(L
, difftime((time_t)(luaL_checknumber(L
, 1)),
257 (time_t)(luaL_optnumber(L
, 2, (lua_Number
)0))));
261 /* ------------------------------------------------------------------------ */
263 LJLIB_CF(os_setlocale
)
266 lua_pushliteral(L
, "C");
268 GCstr
*s
= lj_lib_optstr(L
, 1);
269 const char *str
= s
? strdata(s
) : NULL
;
270 int opt
= lj_lib_checkopt(L
, 2, 6,
271 "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");
272 if (opt
== 0) opt
= LC_CTYPE
;
273 else if (opt
== 1) opt
= LC_NUMERIC
;
274 else if (opt
== 2) opt
= LC_TIME
;
275 else if (opt
== 3) opt
= LC_COLLATE
;
276 else if (opt
== 4) opt
= LC_MONETARY
;
277 else if (opt
== 6) opt
= LC_ALL
;
278 lua_pushstring(L
, setlocale(opt
, str
));
283 /* ------------------------------------------------------------------------ */
285 #include "lj_libdef.h"
287 LUALIB_API
int luaopen_os(lua_State
*L
)
289 LJ_LIB_REG(L
, LUA_OSLIBNAME
, os
);