loslib.c 6.2 KB

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