loslib.c 6.9 KB

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