loslib.c 8.0 KB

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