loslib.c 9.9 KB

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