loslib.c 7.5 KB

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