lib_os.c 6.2 KB

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