loslib.c 8.3 KB

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