loslib.c 8.0 KB

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