3 ** Copyright (C) 2005-2011 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
34 static int os_pushresult(lua_State
*L
, int i
, const char *filename
)
36 int en
= errno
; /* calls to Lua API may change this value */
38 setboolV(L
->top
-1, 1);
42 lua_pushfstring(L
, "%s: %s", filename
, strerror(en
));
43 lua_pushinteger(L
, en
);
50 lua_pushinteger(L
, system(luaL_optstring(L
, 1, NULL
)));
56 const char *filename
= luaL_checkstring(L
, 1);
57 return os_pushresult(L
, remove(filename
) == 0, filename
);
62 const char *fromname
= luaL_checkstring(L
, 1);
63 const char *toname
= luaL_checkstring(L
, 2);
64 return os_pushresult(L
, rename(fromname
, toname
) == 0, fromname
);
72 strcpy(buf
, "/tmp/lua_XXXXXX");
77 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
80 if (tmpnam(buf
) == NULL
)
81 lj_err_caller(L
, LJ_ERR_OSUNIQF
);
83 lua_pushstring(L
, buf
);
89 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
96 if (L
->base
< L
->top
&& tvisbool(L
->base
))
97 status
= boolV(L
->base
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
99 status
= lj_lib_optint(L
, 1, EXIT_SUCCESS
);
100 if (L
->base
+1 < L
->top
&& tvistruecond(L
->base
+1))
103 return 0; /* Unreachable. */
108 setnumV(L
->top
++, ((lua_Number
)clock())*(1.0/(lua_Number
)CLOCKS_PER_SEC
));
112 /* ------------------------------------------------------------------------ */
114 static void setfield(lua_State
*L
, const char *key
, int value
)
116 lua_pushinteger(L
, value
);
117 lua_setfield(L
, -2, key
);
120 static void setboolfield(lua_State
*L
, const char *key
, int value
)
122 if (value
< 0) /* undefined? */
123 return; /* does not set field */
124 lua_pushboolean(L
, value
);
125 lua_setfield(L
, -2, key
);
128 static int getboolfield(lua_State
*L
, const char *key
)
131 lua_getfield(L
, -1, key
);
132 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
137 static int getfield(lua_State
*L
, const char *key
, int d
)
140 lua_getfield(L
, -1, key
);
141 if (lua_isnumber(L
, -1)) {
142 res
= (int)lua_tointeger(L
, -1);
145 lj_err_callerv(L
, LJ_ERR_OSDATEF
, key
);
154 const char *s
= luaL_optstring(L
, 1, "%c");
155 time_t t
= luaL_opt(L
, (time_t)luaL_checknumber
, 2, time(NULL
));
157 if (*s
== '!') { /* UTC? */
163 if (stm
== NULL
) { /* invalid date? */
165 } else if (strcmp(s
, "*t") == 0) {
166 lua_createtable(L
, 0, 9); /* 9 = number of fields */
167 setfield(L
, "sec", stm
->tm_sec
);
168 setfield(L
, "min", stm
->tm_min
);
169 setfield(L
, "hour", stm
->tm_hour
);
170 setfield(L
, "day", stm
->tm_mday
);
171 setfield(L
, "month", stm
->tm_mon
+1);
172 setfield(L
, "year", stm
->tm_year
+1900);
173 setfield(L
, "wday", stm
->tm_wday
+1);
174 setfield(L
, "yday", stm
->tm_yday
+1);
175 setboolfield(L
, "isdst", stm
->tm_isdst
);
179 cc
[0] = '%'; cc
[2] = '\0';
180 luaL_buffinit(L
, &b
);
182 if (*s
!= '%' || *(s
+ 1) == '\0') { /* no conversion specifier? */
183 luaL_addchar(&b
, *s
);
186 char buff
[200]; /* should be big enough for any conversion result */
188 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
189 luaL_addlstring(&b
, buff
, reslen
);
200 if (lua_isnoneornil(L
, 1)) { /* called without args? */
201 t
= time(NULL
); /* get current time */
204 luaL_checktype(L
, 1, LUA_TTABLE
);
205 lua_settop(L
, 1); /* make sure table is at the top */
206 ts
.tm_sec
= getfield(L
, "sec", 0);
207 ts
.tm_min
= getfield(L
, "min", 0);
208 ts
.tm_hour
= getfield(L
, "hour", 12);
209 ts
.tm_mday
= getfield(L
, "day", -1);
210 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
211 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
212 ts
.tm_isdst
= getboolfield(L
, "isdst");
215 if (t
== (time_t)(-1))
218 lua_pushnumber(L
, (lua_Number
)t
);
222 LJLIB_CF(os_difftime
)
224 lua_pushnumber(L
, difftime((time_t)(luaL_checknumber(L
, 1)),
225 (time_t)(luaL_optnumber(L
, 2, (lua_Number
)0))));
229 /* ------------------------------------------------------------------------ */
231 LJLIB_CF(os_setlocale
)
233 GCstr
*s
= lj_lib_optstr(L
, 1);
234 const char *str
= s
? strdata(s
) : NULL
;
235 int opt
= lj_lib_checkopt(L
, 2, 6,
236 "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");
237 if (opt
== 0) opt
= LC_CTYPE
;
238 else if (opt
== 1) opt
= LC_NUMERIC
;
239 else if (opt
== 2) opt
= LC_TIME
;
240 else if (opt
== 3) opt
= LC_COLLATE
;
241 else if (opt
== 4) opt
= LC_MONETARY
;
242 else if (opt
== 6) opt
= LC_ALL
;
243 lua_pushstring(L
, setlocale(opt
, str
));
247 /* ------------------------------------------------------------------------ */
249 #include "lj_libdef.h"
251 LUALIB_API
int luaopen_os(lua_State
*L
)
253 LJ_LIB_REG(L
, LUA_OSLIBNAME
, os
);