loslib.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. ** $Id: loslib.c,v 1.40 2012/10/19 15:54:02 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. /*
  17. ** list of valid conversion specifiers for the 'strftime' function
  18. */
  19. #if !defined(LUA_STRFTIMEOPTIONS)
  20. #if !defined(LUA_USE_POSIX)
  21. #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
  22. #else
  23. #define LUA_STRFTIMEOPTIONS \
  24. { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
  25. "", "E", "cCxXyY", \
  26. "O", "deHImMSuUVwWy" }
  27. #endif
  28. #endif
  29. /*
  30. ** By default, Lua uses tmpnam except when POSIX is available, where it
  31. ** uses mkstemp.
  32. */
  33. #if defined(LUA_USE_MKSTEMP)
  34. #include <unistd.h>
  35. #define LUA_TMPNAMBUFSIZE 32
  36. #define lua_tmpnam(b,e) { \
  37. strcpy(b, "/tmp/lua_XXXXXX"); \
  38. e = mkstemp(b); \
  39. if (e != -1) close(e); \
  40. e = (e == -1); }
  41. #elif !defined(lua_tmpnam)
  42. #define LUA_TMPNAMBUFSIZE L_tmpnam
  43. #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
  44. #endif
  45. /*
  46. ** By default, Lua uses gmtime/localtime, except when POSIX is available,
  47. ** where it uses gmtime_r/localtime_r
  48. */
  49. #if defined(LUA_USE_GMTIME_R)
  50. #define l_gmtime(t,r) gmtime_r(t,r)
  51. #define l_localtime(t,r) localtime_r(t,r)
  52. #elif !defined(l_gmtime)
  53. #define l_gmtime(t,r) ((void)r, gmtime(t))
  54. #define l_localtime(t,r) ((void)r, localtime(t))
  55. #endif
  56. static int os_execute (lua_State *L) {
  57. const char *cmd = luaL_optstring(L, 1, NULL);
  58. int stat = system(cmd);
  59. if (cmd != NULL)
  60. return luaL_execresult(L, stat);
  61. else {
  62. lua_pushboolean(L, stat); /* true if there is a shell */
  63. return 1;
  64. }
  65. }
  66. static int os_remove (lua_State *L) {
  67. const char *filename = luaL_checkstring(L, 1);
  68. return luaL_fileresult(L, remove(filename) == 0, filename);
  69. }
  70. static int os_rename (lua_State *L) {
  71. const char *fromname = luaL_checkstring(L, 1);
  72. const char *toname = luaL_checkstring(L, 2);
  73. return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
  74. }
  75. static int os_tmpname (lua_State *L) {
  76. char buff[LUA_TMPNAMBUFSIZE];
  77. int err;
  78. lua_tmpnam(buff, err);
  79. if (err)
  80. return luaL_error(L, "unable to generate a unique filename");
  81. lua_pushstring(L, buff);
  82. return 1;
  83. }
  84. static int os_getenv (lua_State *L) {
  85. lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
  86. return 1;
  87. }
  88. static int os_clock (lua_State *L) {
  89. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  90. return 1;
  91. }
  92. /*
  93. ** {======================================================
  94. ** Time/Date operations
  95. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  96. ** wday=%w+1, yday=%j, isdst=? }
  97. ** =======================================================
  98. */
  99. static void setfield (lua_State *L, const char *key, int value) {
  100. lua_pushinteger(L, value);
  101. lua_setfield(L, -2, key);
  102. }
  103. static void setboolfield (lua_State *L, const char *key, int value) {
  104. if (value < 0) /* undefined? */
  105. return; /* does not set field */
  106. lua_pushboolean(L, value);
  107. lua_setfield(L, -2, key);
  108. }
  109. static int getboolfield (lua_State *L, const char *key) {
  110. int res;
  111. lua_getfield(L, -1, key);
  112. res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
  113. lua_pop(L, 1);
  114. return res;
  115. }
  116. static int getfield (lua_State *L, const char *key, int d) {
  117. int res, isnum;
  118. lua_getfield(L, -1, key);
  119. res = (int)lua_tointegerx(L, -1, &isnum);
  120. if (!isnum) {
  121. if (d < 0)
  122. return luaL_error(L, "field " LUA_QS " missing in date table", key);
  123. res = d;
  124. }
  125. lua_pop(L, 1);
  126. return res;
  127. }
  128. static const char *checkoption (lua_State *L, const char *conv, char *buff) {
  129. static const char *const options[] = LUA_STRFTIMEOPTIONS;
  130. unsigned int i;
  131. for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
  132. if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
  133. buff[1] = *conv;
  134. if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
  135. buff[2] = '\0'; /* end buffer */
  136. return conv + 1;
  137. }
  138. else if (*(conv + 1) != '\0' &&
  139. strchr(options[i + 1], *(conv + 1)) != NULL) {
  140. buff[2] = *(conv + 1); /* valid two-char conversion specifier */
  141. buff[3] = '\0'; /* end buffer */
  142. return conv + 2;
  143. }
  144. }
  145. }
  146. luaL_argerror(L, 1,
  147. lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
  148. return conv; /* to avoid warnings */
  149. }
  150. static int os_date (lua_State *L) {
  151. const char *s = luaL_optstring(L, 1, "%c");
  152. time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
  153. struct tm tmr, *stm;
  154. if (*s == '!') { /* UTC? */
  155. stm = l_gmtime(&t, &tmr);
  156. s++; /* skip `!' */
  157. }
  158. else
  159. stm = l_localtime(&t, &tmr);
  160. if (stm == NULL) /* invalid date? */
  161. lua_pushnil(L);
  162. else if (strcmp(s, "*t") == 0) {
  163. lua_createtable(L, 0, 9); /* 9 = number of fields */
  164. setfield(L, "sec", stm->tm_sec);
  165. setfield(L, "min", stm->tm_min);
  166. setfield(L, "hour", stm->tm_hour);
  167. setfield(L, "day", stm->tm_mday);
  168. setfield(L, "month", stm->tm_mon+1);
  169. setfield(L, "year", stm->tm_year+1900);
  170. setfield(L, "wday", stm->tm_wday+1);
  171. setfield(L, "yday", stm->tm_yday+1);
  172. setboolfield(L, "isdst", stm->tm_isdst);
  173. }
  174. else {
  175. char cc[4];
  176. luaL_Buffer b;
  177. cc[0] = '%';
  178. luaL_buffinit(L, &b);
  179. while (*s) {
  180. if (*s != '%') /* no conversion specifier? */
  181. luaL_addchar(&b, *s++);
  182. else {
  183. size_t reslen;
  184. char buff[200]; /* should be big enough for any conversion result */
  185. s = checkoption(L, s + 1, cc);
  186. reslen = strftime(buff, sizeof(buff), cc, stm);
  187. luaL_addlstring(&b, buff, reslen);
  188. }
  189. }
  190. luaL_pushresult(&b);
  191. }
  192. return 1;
  193. }
  194. static int os_time (lua_State *L) {
  195. time_t t;
  196. if (lua_isnoneornil(L, 1)) /* called without args? */
  197. t = time(NULL); /* get current time */
  198. else {
  199. struct tm ts;
  200. luaL_checktype(L, 1, LUA_TTABLE);
  201. lua_settop(L, 1); /* make sure table is at the top */
  202. ts.tm_sec = getfield(L, "sec", 0);
  203. ts.tm_min = getfield(L, "min", 0);
  204. ts.tm_hour = getfield(L, "hour", 12);
  205. ts.tm_mday = getfield(L, "day", -1);
  206. ts.tm_mon = getfield(L, "month", -1) - 1;
  207. ts.tm_year = getfield(L, "year", -1) - 1900;
  208. ts.tm_isdst = getboolfield(L, "isdst");
  209. t = mktime(&ts);
  210. }
  211. if (t == (time_t)(-1))
  212. lua_pushnil(L);
  213. else
  214. lua_pushnumber(L, (lua_Number)t);
  215. return 1;
  216. }
  217. static int os_difftime (lua_State *L) {
  218. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  219. (time_t)(luaL_optnumber(L, 2, 0))));
  220. return 1;
  221. }
  222. /* }====================================================== */
  223. static int os_setlocale (lua_State *L) {
  224. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  225. LC_NUMERIC, LC_TIME};
  226. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  227. "numeric", "time", NULL};
  228. const char *l = luaL_optstring(L, 1, NULL);
  229. int op = luaL_checkoption(L, 2, "all", catnames);
  230. lua_pushstring(L, setlocale(cat[op], l));
  231. return 1;
  232. }
  233. static int os_exit (lua_State *L) {
  234. int status;
  235. if (lua_isboolean(L, 1))
  236. status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
  237. else
  238. status = luaL_optint(L, 1, EXIT_SUCCESS);
  239. if (lua_toboolean(L, 2))
  240. lua_close(L);
  241. if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
  242. return 0;
  243. }
  244. static const luaL_Reg syslib[] = {
  245. {"clock", os_clock},
  246. {"date", os_date},
  247. {"difftime", os_difftime},
  248. {"execute", os_execute},
  249. {"exit", os_exit},
  250. {"getenv", os_getenv},
  251. {"remove", os_remove},
  252. {"rename", os_rename},
  253. {"setlocale", os_setlocale},
  254. {"time", os_time},
  255. {"tmpname", os_tmpname},
  256. {NULL, NULL}
  257. };
  258. /* }====================================================== */
  259. LUAMOD_API int luaopen_os (lua_State *L) {
  260. luaL_newlib(L, syslib);
  261. return 1;
  262. }