loslib.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
  3. ** Standard Operating System library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <errno.h>
  7. #include <locale.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #define loslib_c
  12. #define LUA_LIB
  13. #include "lua.h"
  14. #include "lauxlib.h"
  15. #include "lualib.h"
  16. #include "lrotable.h"
  17. static int os_pushresult(lua_State *L, int i, const char *filename)
  18. {
  19. int en = errno; /* calls to Lua API may change this value */
  20. if (i)
  21. {
  22. lua_pushboolean(L, 1);
  23. return 1;
  24. }
  25. else
  26. {
  27. lua_pushnil(L);
  28. lua_pushfstring(L, "%s: %s", filename, strerror(en));
  29. lua_pushinteger(L, en);
  30. return 3;
  31. }
  32. }
  33. static int os_execute(lua_State *L)
  34. {
  35. lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
  36. return 1;
  37. }
  38. static int os_remove(lua_State *L)
  39. {
  40. const char *filename = luaL_checkstring(L, 1);
  41. return os_pushresult(L, remove(filename) == 0, filename);
  42. }
  43. static int os_rename(lua_State *L)
  44. {
  45. const char *fromname = luaL_checkstring(L, 1);
  46. const char *toname = luaL_checkstring(L, 2);
  47. return os_pushresult(L, rename(fromname, toname) == 0, fromname);
  48. }
  49. static int os_tmpname(lua_State *L)
  50. {
  51. char buff[LUA_TMPNAMBUFSIZE];
  52. int err;
  53. lua_tmpnam(buff, err);
  54. if (err)
  55. return luaL_error(L, "unable to generate a unique filename");
  56. lua_pushstring(L, buff);
  57. return 1;
  58. }
  59. static int os_getenv(lua_State *L)
  60. {
  61. lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
  62. return 1;
  63. }
  64. #include <rtconfig.h>
  65. static int os_clock(lua_State *L)
  66. {
  67. // lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)CLOCKS_PER_SEC);
  68. lua_pushnumber(L, ((lua_Number)clock()) / (lua_Number)RT_TICK_PER_SECOND);
  69. return 1;
  70. }
  71. /*
  72. ** {======================================================
  73. ** Time/Date operations
  74. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  75. ** wday=%w+1, yday=%j, isdst=? }
  76. ** =======================================================
  77. */
  78. static void setfield(lua_State *L, const char *key, int value)
  79. {
  80. lua_pushinteger(L, value);
  81. lua_setfield(L, -2, key);
  82. }
  83. static void setboolfield(lua_State *L, const char *key, int value)
  84. {
  85. if (value < 0) /* undefined? */
  86. return; /* does not set field */
  87. lua_pushboolean(L, value);
  88. lua_setfield(L, -2, key);
  89. }
  90. static int getboolfield(lua_State *L, const char *key)
  91. {
  92. int res;
  93. lua_getfield(L, -1, key);
  94. res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
  95. lua_pop(L, 1);
  96. return res;
  97. }
  98. static int getfield(lua_State *L, const char *key, int d)
  99. {
  100. int res;
  101. lua_getfield(L, -1, key);
  102. if (lua_isnumber(L, -1))
  103. res = (int)lua_tointeger(L, -1);
  104. else
  105. {
  106. if (d < 0)
  107. return luaL_error(L, "field " LUA_QS " missing in date table", key);
  108. res = d;
  109. }
  110. lua_pop(L, 1);
  111. return res;
  112. }
  113. static int os_date(lua_State *L)
  114. {
  115. const char *s = luaL_optstring(L, 1, "%c");
  116. time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
  117. struct tm *stm;
  118. if (*s == '!') /* UTC? */
  119. {
  120. stm = gmtime(&t);
  121. s++; /* skip `!' */
  122. }
  123. else
  124. {
  125. stm = localtime(&t);
  126. }
  127. if (stm == NULL) /* invalid date? */
  128. {
  129. lua_pushnil(L);
  130. }
  131. else if (strcmp(s, "*t") == 0)
  132. {
  133. lua_createtable(L, 0, 9); /* 9 = number of fields */
  134. setfield(L, "sec", stm->tm_sec);
  135. setfield(L, "min", stm->tm_min);
  136. setfield(L, "hour", stm->tm_hour);
  137. setfield(L, "day", stm->tm_mday);
  138. setfield(L, "month", stm->tm_mon + 1);
  139. setfield(L, "year", stm->tm_year + 1900);
  140. setfield(L, "wday", stm->tm_wday + 1);
  141. setfield(L, "yday", stm->tm_yday + 1);
  142. setboolfield(L, "isdst", stm->tm_isdst);
  143. }
  144. else
  145. {
  146. char cc[3];
  147. luaL_Buffer b;
  148. cc[0] = '%';
  149. cc[2] = '\0';
  150. luaL_buffinit(L, &b);
  151. for (; *s; s++)
  152. {
  153. if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
  154. luaL_addchar(&b, *s);
  155. else
  156. {
  157. size_t reslen;
  158. char buff[200]; /* should be big enough for any conversion result */
  159. cc[1] = *(++s);
  160. reslen = strftime(buff, sizeof(buff), cc, stm);
  161. luaL_addlstring(&b, buff, reslen);
  162. }
  163. }
  164. luaL_pushresult(&b);
  165. }
  166. return 1;
  167. }
  168. static int os_time(lua_State *L)
  169. {
  170. time_t t;
  171. if (lua_isnoneornil(L, 1)) /* called without args? */
  172. t = time(NULL); /* get current time */
  173. else
  174. {
  175. struct tm ts;
  176. luaL_checktype(L, 1, LUA_TTABLE);
  177. lua_settop(L, 1); /* make sure table is at the top */
  178. ts.tm_sec = getfield(L, "sec", 0);
  179. ts.tm_min = getfield(L, "min", 0);
  180. ts.tm_hour = getfield(L, "hour", 12);
  181. ts.tm_mday = getfield(L, "day", -1);
  182. ts.tm_mon = getfield(L, "month", -1) - 1;
  183. ts.tm_year = getfield(L, "year", -1) - 1900;
  184. ts.tm_isdst = getboolfield(L, "isdst");
  185. t = mktime(&ts);
  186. }
  187. if (t == (time_t)(-1))
  188. lua_pushnil(L);
  189. else
  190. lua_pushnumber(L, (lua_Number)t);
  191. return 1;
  192. }
  193. #if !defined LUA_NUMBER_INTEGRAL
  194. static int os_difftime(lua_State *L)
  195. {
  196. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  197. (time_t)(luaL_optnumber(L, 2, 0))));
  198. return 1;
  199. }
  200. #endif
  201. /* }====================================================== */
  202. static int os_setlocale(lua_State *L)
  203. {
  204. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  205. LC_NUMERIC, LC_TIME
  206. };
  207. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  208. "numeric", "time", NULL
  209. };
  210. const char *l = luaL_optstring(L, 1, NULL);
  211. int op = luaL_checkoption(L, 2, "all", catnames);
  212. lua_pushstring(L, setlocale(cat[op], l));
  213. return 1;
  214. }
  215. static int os_exit(lua_State *L)
  216. {
  217. exit(luaL_optint(L, 1, EXIT_SUCCESS));
  218. }
  219. #define MIN_OPT_LEVEL 1
  220. #include "lrodefs.h"
  221. const LUA_REG_TYPE syslib[] =
  222. {
  223. {LSTRKEY("clock"), LFUNCVAL(os_clock) },
  224. {LSTRKEY("date"), LFUNCVAL(os_date) },
  225. #if !defined LUA_NUMBER_INTEGRAL
  226. {LSTRKEY("difftime"), LFUNCVAL(os_difftime) },
  227. #endif
  228. {LSTRKEY("execute"), LFUNCVAL(os_execute) },
  229. {LSTRKEY("exit"), LFUNCVAL(os_exit) },
  230. {LSTRKEY("getenv"), LFUNCVAL(os_getenv) },
  231. {LSTRKEY("remove"), LFUNCVAL(os_remove) },
  232. {LSTRKEY("rename"), LFUNCVAL(os_rename) },
  233. {LSTRKEY("setlocale"), LFUNCVAL(os_setlocale)},
  234. {LSTRKEY("time"), LFUNCVAL(os_time) },
  235. {LSTRKEY("tmpname"), LFUNCVAL(os_tmpname) },
  236. {LNILKEY, LNILVAL}
  237. };
  238. LUALIB_API int luaopen_os(lua_State *L)
  239. {
  240. LREGISTER(L, LUA_OSLIBNAME, syslib);
  241. }