loslib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ** $Id: loslib.c,v 1.12 2005/08/26 17:36:32 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. static int os_pushresult (lua_State *L, int i, const char *filename) {
  17. if (i) {
  18. lua_pushboolean(L, 1);
  19. return 1;
  20. }
  21. else {
  22. lua_pushnil(L);
  23. if (filename)
  24. lua_pushfstring(L, "%s: %s", filename, strerror(errno));
  25. else
  26. lua_pushfstring(L, "%s", strerror(errno));
  27. lua_pushinteger(L, errno);
  28. return 3;
  29. }
  30. }
  31. static int io_execute (lua_State *L) {
  32. lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
  33. return 1;
  34. }
  35. static int io_remove (lua_State *L) {
  36. const char *filename = luaL_checkstring(L, 1);
  37. return os_pushresult(L, remove(filename) == 0, filename);
  38. }
  39. static int io_rename (lua_State *L) {
  40. const char *fromname = luaL_checkstring(L, 1);
  41. const char *toname = luaL_checkstring(L, 2);
  42. return os_pushresult(L, rename(fromname, toname) == 0, fromname);
  43. }
  44. static int io_tmpname (lua_State *L) {
  45. char buff[LUA_TMPNAMBUFSIZE];
  46. int err;
  47. lua_tmpnam(buff, err);
  48. if (err)
  49. return luaL_error(L, "unable to generate a unique filename");
  50. lua_pushstring(L, buff);
  51. return 1;
  52. }
  53. static int io_getenv (lua_State *L) {
  54. lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
  55. return 1;
  56. }
  57. static int io_clock (lua_State *L) {
  58. lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  59. return 1;
  60. }
  61. /*
  62. ** {======================================================
  63. ** Time/Date operations
  64. ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
  65. ** wday=%w+1, yday=%j, isdst=? }
  66. ** =======================================================
  67. */
  68. static void setfield (lua_State *L, const char *key, int value) {
  69. lua_pushinteger(L, value);
  70. lua_setfield(L, -2, key);
  71. }
  72. static void setboolfield (lua_State *L, const char *key, int value) {
  73. if (value < 0) /* undefined? */
  74. return; /* does not set field */
  75. lua_pushboolean(L, value);
  76. lua_setfield(L, -2, key);
  77. }
  78. static int getboolfield (lua_State *L, const char *key) {
  79. int res;
  80. lua_getfield(L, -1, key);
  81. res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
  82. lua_pop(L, 1);
  83. return res;
  84. }
  85. static int getfield (lua_State *L, const char *key, int d) {
  86. int res;
  87. lua_getfield(L, -1, key);
  88. if (lua_isnumber(L, -1))
  89. res = (int)lua_tointeger(L, -1);
  90. else {
  91. if (d < 0)
  92. return luaL_error(L, "field " LUA_QS " missing in date table", key);
  93. res = d;
  94. }
  95. lua_pop(L, 1);
  96. return res;
  97. }
  98. static int io_date (lua_State *L) {
  99. const char *s = luaL_optstring(L, 1, "%c");
  100. lua_Number n = luaL_optnumber(L, 2, -1);
  101. time_t t = (n == -1) ? time(NULL) : (time_t)n;
  102. struct tm *stm;
  103. if (*s == '!') { /* UTC? */
  104. stm = gmtime(&t);
  105. s++; /* skip `!' */
  106. }
  107. else
  108. stm = localtime(&t);
  109. if (stm == NULL) /* invalid date? */
  110. lua_pushnil(L);
  111. else if (strcmp(s, "*t") == 0) {
  112. lua_createtable(L, 0, 9); /* 9 = number of fields */
  113. setfield(L, "sec", stm->tm_sec);
  114. setfield(L, "min", stm->tm_min);
  115. setfield(L, "hour", stm->tm_hour);
  116. setfield(L, "day", stm->tm_mday);
  117. setfield(L, "month", stm->tm_mon+1);
  118. setfield(L, "year", stm->tm_year+1900);
  119. setfield(L, "wday", stm->tm_wday+1);
  120. setfield(L, "yday", stm->tm_yday+1);
  121. setboolfield(L, "isdst", stm->tm_isdst);
  122. }
  123. else {
  124. char b[256];
  125. if (strftime(b, sizeof(b), s, stm))
  126. lua_pushstring(L, b);
  127. else
  128. return luaL_error(L, LUA_QL("date") " format too long");
  129. }
  130. return 1;
  131. }
  132. static int io_time (lua_State *L) {
  133. time_t t;
  134. if (lua_isnoneornil(L, 1)) /* called without args? */
  135. t = time(NULL); /* get current time */
  136. else {
  137. struct tm ts;
  138. luaL_checktype(L, 1, LUA_TTABLE);
  139. lua_settop(L, 1); /* make sure table is at the top */
  140. ts.tm_sec = getfield(L, "sec", 0);
  141. ts.tm_min = getfield(L, "min", 0);
  142. ts.tm_hour = getfield(L, "hour", 12);
  143. ts.tm_mday = getfield(L, "day", -1);
  144. ts.tm_mon = getfield(L, "month", -1) - 1;
  145. ts.tm_year = getfield(L, "year", -1) - 1900;
  146. ts.tm_isdst = getboolfield(L, "isdst");
  147. t = mktime(&ts);
  148. }
  149. if (t == (time_t)(-1))
  150. lua_pushnil(L);
  151. else
  152. lua_pushnumber(L, t);
  153. return 1;
  154. }
  155. static int io_difftime (lua_State *L) {
  156. lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
  157. (time_t)(luaL_optnumber(L, 2, 0))));
  158. return 1;
  159. }
  160. /* }====================================================== */
  161. static int io_setloc (lua_State *L) {
  162. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  163. LC_NUMERIC, LC_TIME};
  164. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  165. "numeric", "time", NULL};
  166. const char *l = lua_tostring(L, 1);
  167. int op = luaL_checkoption(L, 2, "all", catnames);
  168. luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected");
  169. lua_pushstring(L, setlocale(cat[op], l));
  170. return 1;
  171. }
  172. static int io_exit (lua_State *L) {
  173. exit(luaL_optint(L, 1, EXIT_SUCCESS));
  174. return 0; /* to avoid warnings */
  175. }
  176. static const luaL_Reg syslib[] = {
  177. {"clock", io_clock},
  178. {"date", io_date},
  179. {"difftime", io_difftime},
  180. {"execute", io_execute},
  181. {"exit", io_exit},
  182. {"getenv", io_getenv},
  183. {"remove", io_remove},
  184. {"rename", io_rename},
  185. {"setlocale", io_setloc},
  186. {"time", io_time},
  187. {"tmpname", io_tmpname},
  188. {NULL, NULL}
  189. };
  190. /* }====================================================== */
  191. LUALIB_API int luaopen_os (lua_State *L) {
  192. luaL_register(L, LUA_OSLIBNAME, syslib);
  193. return 1;
  194. }