loslib.c 8.4 KB

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