loslib.c 6.7 KB

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