loslib.c 7.7 KB

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