3 ** Copyright (C) 2005-2014 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
30 /* ------------------------------------------------------------------------ */
32 #define LJLIB_MODULE_os
39 return luaL_fileresult(L
, 0, NULL
);
41 lua_pushinteger(L
, -1);
45 const char *cmd
= luaL_optstring(L
, 1, NULL
);
46 int stat
= system(cmd
);
49 return luaL_execresult(L
, stat
);
50 setboolV(L
->top
++, 1);
52 setintV(L
->top
++, stat
);
60 const char *filename
= luaL_checkstring(L
, 1);
61 return luaL_fileresult(L
, remove(filename
) == 0, filename
);
66 const char *fromname
= luaL_checkstring(L
, 1);
67 const char *toname
= luaL_checkstring(L
, 2);
68 return luaL_fileresult(L
, rename(fromname
, toname
) == 0, fromname
);
73 #if LJ_TARGET_PS3 || LJ_TARGET_PS4
74 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
80 strcpy(buf
, "/tmp/lua_XXXXXX");
85 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
88 if (tmpnam(buf
) == NULL
)
89 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
91 lua_pushstring(L
, buf
);
101 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
109 if (L
->base
< L
->top
&& tvisbool(L
->base
))
110 status
= boolV(L
->base
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
112 status
= lj_lib_optint(L
, 1, EXIT_SUCCESS
);
113 if (L
->base
+1 < L
->top
&& tvistruecond(L
->base
+1))
116 return 0; /* Unreachable. */
121 setnumV(L
->top
++, ((lua_Number
)clock())*(1.0/(lua_Number
)CLOCKS_PER_SEC
));
125 /* ------------------------------------------------------------------------ */
127 static void setfield(lua_State
*L
, const char *key
, int value
)
129 lua_pushinteger(L
, value
);
130 lua_setfield(L
, -2, key
);
133 static void setboolfield(lua_State
*L
, const char *key
, int value
)
135 if (value
< 0) /* undefined? */
136 return; /* does not set field */
137 lua_pushboolean(L
, value
);
138 lua_setfield(L
, -2, key
);
141 static int getboolfield(lua_State
*L
, const char *key
)
144 lua_getfield(L
, -1, key
);
145 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
150 static int getfield(lua_State
*L
, const char *key
, int d
)
153 lua_getfield(L
, -1, key
);
154 if (lua_isnumber(L
, -1)) {
155 res
= (int)lua_tointeger(L
, -1);
158 lj_err_callerv(L
, LJ_ERR_OSDATEF
, key
);
167 const char *s
= luaL_optstring(L
, 1, "%c");
168 time_t t
= luaL_opt(L
, (time_t)luaL_checknumber
, 2, time(NULL
));
173 if (*s
== '!') { /* UTC? */
176 stm
= gmtime_r(&t
, &rtm
);
182 stm
= localtime_r(&t
, &rtm
);
187 if (stm
== NULL
) { /* Invalid date? */
189 } else if (strcmp(s
, "*t") == 0) {
190 lua_createtable(L
, 0, 9); /* 9 = number of fields */
191 setfield(L
, "sec", stm
->tm_sec
);
192 setfield(L
, "min", stm
->tm_min
);
193 setfield(L
, "hour", stm
->tm_hour
);
194 setfield(L
, "day", stm
->tm_mday
);
195 setfield(L
, "month", stm
->tm_mon
+1);
196 setfield(L
, "year", stm
->tm_year
+1900);
197 setfield(L
, "wday", stm
->tm_wday
+1);
198 setfield(L
, "yday", stm
->tm_yday
+1);
199 setboolfield(L
, "isdst", stm
->tm_isdst
);
203 cc
[0] = '%'; cc
[2] = '\0';
204 luaL_buffinit(L
, &b
);
206 if (*s
!= '%' || *(s
+ 1) == '\0') { /* No conversion specifier? */
207 luaL_addchar(&b
, *s
);
210 char buff
[200]; /* Should be big enough for any conversion result. */
212 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
213 luaL_addlstring(&b
, buff
, reslen
);
224 if (lua_isnoneornil(L
, 1)) { /* called without args? */
225 t
= time(NULL
); /* get current time */
228 luaL_checktype(L
, 1, LUA_TTABLE
);
229 lua_settop(L
, 1); /* make sure table is at the top */
230 ts
.tm_sec
= getfield(L
, "sec", 0);
231 ts
.tm_min
= getfield(L
, "min", 0);
232 ts
.tm_hour
= getfield(L
, "hour", 12);
233 ts
.tm_mday
= getfield(L
, "day", -1);
234 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
235 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
236 ts
.tm_isdst
= getboolfield(L
, "isdst");
239 if (t
== (time_t)(-1))
242 lua_pushnumber(L
, (lua_Number
)t
);
246 LJLIB_CF(os_difftime
)
248 lua_pushnumber(L
, difftime((time_t)(luaL_checknumber(L
, 1)),
249 (time_t)(luaL_optnumber(L
, 2, (lua_Number
)0))));
253 /* ------------------------------------------------------------------------ */
255 LJLIB_CF(os_setlocale
)
257 GCstr
*s
= lj_lib_optstr(L
, 1);
258 const char *str
= s
? strdata(s
) : NULL
;
259 int opt
= lj_lib_checkopt(L
, 2, 6,
260 "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");
261 if (opt
== 0) opt
= LC_CTYPE
;
262 else if (opt
== 1) opt
= LC_NUMERIC
;
263 else if (opt
== 2) opt
= LC_TIME
;
264 else if (opt
== 3) opt
= LC_COLLATE
;
265 else if (opt
== 4) opt
= LC_MONETARY
;
266 else if (opt
== 6) opt
= LC_ALL
;
267 lua_pushstring(L
, setlocale(opt
, str
));
271 /* ------------------------------------------------------------------------ */
273 #include "lj_libdef.h"
275 LUALIB_API
int luaopen_os(lua_State
*L
)
277 LJ_LIB_REG(L
, LUA_OSLIBNAME
, os
);