2
0

loslib.c 9.2 KB

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