loslib.c 5.7 KB

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