loslib.c 8.5 KB

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