2
0

loslib.c 5.7 KB

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