lib_os.c 6.4 KB

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