1 /*
2 ** OS library.
3 ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
4 **
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
7 */
8 
9 #include <errno.h>
10 #include <time.h>
11 
12 #define lib_os_c
13 #define LUA_LIB
14 
15 #include "lua.h"
16 #include "lauxlib.h"
17 #include "lualib.h"
18 
19 #include "lj_obj.h"
20 #include "lj_gc.h"
21 #include "lj_err.h"
22 #include "lj_buf.h"
23 #include "lj_str.h"
24 #include "lj_lib.h"
25 
26 #if LJ_TARGET_POSIX
27 #include <unistd.h>
28 #else
29 #include <stdio.h>
30 #endif
31 
32 #if !LJ_TARGET_PSVITA
33 #include <locale.h>
34 #endif
35 
36 /* ------------------------------------------------------------------------ */
37 
38 #define LJLIB_MODULE_os
39 
LJLIB_CF(os_execute)40 LJLIB_CF(os_execute)
41 {
42 #if LJ_NO_SYSTEM
43 #if LJ_52
44   errno = ENOSYS;
45   return luaL_fileresult(L, 0, NULL);
46 #else
47   lua_pushinteger(L, -1);
48   return 1;
49 #endif
50 #else
51   const char *cmd = luaL_optstring(L, 1, NULL);
52   int stat = system(cmd);
53 #if LJ_52
54   if (cmd)
55     return luaL_execresult(L, stat);
56   setboolV(L->top++, 1);
57 #else
58   setintV(L->top++, stat);
59 #endif
60   return 1;
61 #endif
62 }
63 
LJLIB_CF(os_remove)64 LJLIB_CF(os_remove)
65 {
66   const char *filename = luaL_checkstring(L, 1);
67   return luaL_fileresult(L, remove(filename) == 0, filename);
68 }
69 
LJLIB_CF(os_rename)70 LJLIB_CF(os_rename)
71 {
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);
75 }
76 
LJLIB_CF(os_tmpname)77 LJLIB_CF(os_tmpname)
78 {
79 #if LJ_TARGET_PS3 || LJ_TARGET_PS4 || LJ_TARGET_PSVITA
80   lj_err_caller(L, LJ_ERR_OSUNIQF);
81   return 0;
82 #else
83 #if LJ_TARGET_POSIX
84   char buf[15+1];
85   int fp;
86   strcpy(buf, "/tmp/lua_XXXXXX");
87   fp = mkstemp(buf);
88   if (fp != -1)
89     close(fp);
90   else
91     lj_err_caller(L, LJ_ERR_OSUNIQF);
92 #else
93   char buf[L_tmpnam];
94   if (tmpnam(buf) == NULL)
95     lj_err_caller(L, LJ_ERR_OSUNIQF);
96 #endif
97   lua_pushstring(L, buf);
98   return 1;
99 #endif
100 }
101 
LJLIB_CF(os_getenv)102 LJLIB_CF(os_getenv)
103 {
104 #if LJ_TARGET_CONSOLE
105   lua_pushnil(L);
106 #else
107   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
108 #endif
109   return 1;
110 }
111 
LJLIB_CF(os_exit)112 LJLIB_CF(os_exit)
113 {
114   int status;
115   if (L->base < L->top && tvisbool(L->base))
116     status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE;
117   else
118     status = lj_lib_optint(L, 1, EXIT_SUCCESS);
119   if (L->base+1 < L->top && tvistruecond(L->base+1))
120     lua_close(L);
121   exit(status);
122   return 0;  /* Unreachable. */
123 }
124 
LJLIB_CF(os_clock)125 LJLIB_CF(os_clock)
126 {
127   setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC));
128   return 1;
129 }
130 
131 /* ------------------------------------------------------------------------ */
132 
setfield(lua_State * L,const char * key,int value)133 static void setfield(lua_State *L, const char *key, int value)
134 {
135   lua_pushinteger(L, value);
136   lua_setfield(L, -2, key);
137 }
138 
setboolfield(lua_State * L,const char * key,int value)139 static void setboolfield(lua_State *L, const char *key, int value)
140 {
141   if (value < 0)  /* undefined? */
142     return;  /* does not set field */
143   lua_pushboolean(L, value);
144   lua_setfield(L, -2, key);
145 }
146 
getboolfield(lua_State * L,const char * key)147 static int getboolfield(lua_State *L, const char *key)
148 {
149   int res;
150   lua_getfield(L, -1, key);
151   res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
152   lua_pop(L, 1);
153   return res;
154 }
155 
getfield(lua_State * L,const char * key,int d)156 static int getfield(lua_State *L, const char *key, int d)
157 {
158   int res;
159   lua_getfield(L, -1, key);
160   if (lua_isnumber(L, -1)) {
161     res = (int)lua_tointeger(L, -1);
162   } else {
163     if (d < 0)
164       lj_err_callerv(L, LJ_ERR_OSDATEF, key);
165     res = d;
166   }
167   lua_pop(L, 1);
168   return res;
169 }
170 
LJLIB_CF(os_date)171 LJLIB_CF(os_date)
172 {
173   const char *s = luaL_optstring(L, 1, "%c");
174   time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
175   struct tm *stm;
176 #if LJ_TARGET_POSIX
177   struct tm rtm;
178 #endif
179   if (*s == '!') {  /* UTC? */
180     s++;  /* Skip '!' */
181 #if LJ_TARGET_POSIX
182     stm = gmtime_r(&t, &rtm);
183 #else
184     stm = gmtime(&t);
185 #endif
186   } else {
187 #if LJ_TARGET_POSIX
188     tzset();
189     stm = localtime_r(&t, &rtm);
190 #else
191     stm = localtime(&t);
192 #endif
193   }
194   if (stm == NULL) {  /* Invalid date? */
195     setnilV(L->top++);
196   } else if (strcmp(s, "*t") == 0) {
197     lua_createtable(L, 0, 9);  /* 9 = number of fields */
198     setfield(L, "sec", stm->tm_sec);
199     setfield(L, "min", stm->tm_min);
200     setfield(L, "hour", stm->tm_hour);
201     setfield(L, "day", stm->tm_mday);
202     setfield(L, "month", stm->tm_mon+1);
203     setfield(L, "year", stm->tm_year+1900);
204     setfield(L, "wday", stm->tm_wday+1);
205     setfield(L, "yday", stm->tm_yday+1);
206     setboolfield(L, "isdst", stm->tm_isdst);
207   } else if (*s) {
208     SBuf *sb = &G(L)->tmpbuf;
209     MSize sz = 0, retry = 4;
210     const char *q;
211     for (q = s; *q; q++)
212       sz += (*q == '%') ? 30 : 1;  /* Overflow doesn't matter. */
213     setsbufL(sb, L);
214     while (retry--) {  /* Limit growth for invalid format or empty result. */
215       char *buf = lj_buf_need(sb, sz);
216       size_t len = strftime(buf, sbufsz(sb), s, stm);
217       if (len) {
218 	setstrV(L, L->top++, lj_str_new(L, buf, len));
219 	lj_gc_check(L);
220 	break;
221       }
222       sz += (sz|1);
223     }
224   } else {
225     setstrV(L, L->top++, &G(L)->strempty);
226   }
227   return 1;
228 }
229 
LJLIB_CF(os_time)230 LJLIB_CF(os_time)
231 {
232   time_t t;
233   if (lua_isnoneornil(L, 1)) {  /* called without args? */
234     t = time(NULL);  /* get current time */
235   } else {
236     struct tm ts;
237     luaL_checktype(L, 1, LUA_TTABLE);
238     lua_settop(L, 1);  /* make sure table is at the top */
239     ts.tm_sec = getfield(L, "sec", 0);
240     ts.tm_min = getfield(L, "min", 0);
241     ts.tm_hour = getfield(L, "hour", 12);
242     ts.tm_mday = getfield(L, "day", -1);
243     ts.tm_mon = getfield(L, "month", -1) - 1;
244     ts.tm_year = getfield(L, "year", -1) - 1900;
245     ts.tm_isdst = getboolfield(L, "isdst");
246     t = mktime(&ts);
247   }
248   if (t == (time_t)(-1))
249     lua_pushnil(L);
250   else
251     lua_pushnumber(L, (lua_Number)t);
252   return 1;
253 }
254 
LJLIB_CF(os_difftime)255 LJLIB_CF(os_difftime)
256 {
257   lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
258 			     (time_t)(luaL_optnumber(L, 2, (lua_Number)0))));
259   return 1;
260 }
261 
262 /* ------------------------------------------------------------------------ */
263 
LJLIB_CF(os_setlocale)264 LJLIB_CF(os_setlocale)
265 {
266 #if LJ_TARGET_PSVITA
267   lua_pushliteral(L, "C");
268 #else
269   GCstr *s = lj_lib_optstr(L, 1);
270   const char *str = s ? strdata(s) : NULL;
271   int opt = lj_lib_checkopt(L, 2, 6,
272     "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");
273   if (opt == 0) opt = LC_CTYPE;
274   else if (opt == 1) opt = LC_NUMERIC;
275   else if (opt == 2) opt = LC_TIME;
276   else if (opt == 3) opt = LC_COLLATE;
277   else if (opt == 4) opt = LC_MONETARY;
278   else if (opt == 6) opt = LC_ALL;
279   lua_pushstring(L, setlocale(opt, str));
280 #endif
281   return 1;
282 }
283 
284 /* ------------------------------------------------------------------------ */
285 
286 #include "lj_libdef.h"
287 
luaopen_os(lua_State * L)288 LUALIB_API int luaopen_os(lua_State *L)
289 {
290   LJ_LIB_REG(L, LUA_OSLIBNAME, os);
291   return 1;
292 }
293 
294