00001
00002
00003
00004
00005
00006
00007
00008 #include <errno.h>
00009 #include <locale.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <time.h>
00013
00014 #define loslib_c
00015 #define LUA_LIB
00016
00017 #include "lua.h"
00018
00019 #include "lauxlib.h"
00020 #include "lualib.h"
00021
00022
00023
00024
00025
00026 #if !defined(LUA_STRFTIMEOPTIONS)
00027
00028 #if !defined(LUA_USE_POSIX)
00029 #define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
00030 #else
00031 #define LUA_STRFTIMEOPTIONS { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
00032 "E", "cCxXyY", \
00033 "O", "deHImMSuUVwWy" }
00034 #endif
00035
00036 #endif
00037
00038
00039
00040
00041
00042
00043
00044 #if defined(LUA_USE_MKSTEMP)
00045 #include <unistd.h>
00046 #define LUA_TMPNAMBUFSIZE 32
00047 #define lua_tmpnam(b,e) { \
00048 strcpy(b, "/tmp/lua_XXXXXX"); \
00049 e = mkstemp(b); \
00050 if (e != -1) close(e); \
00051 e = (e == -1); }
00052
00053 #elif !defined(lua_tmpnam)
00054
00055 #define LUA_TMPNAMBUFSIZE L_tmpnam
00056 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
00057
00058 #endif
00059
00060
00061
00062
00063
00064
00065 #if defined(LUA_USE_GMTIME_R)
00066
00067 #define l_gmtime(t,r) gmtime_r(t,r)
00068 #define l_localtime(t,r) localtime_r(t,r)
00069
00070 #elif !defined(l_gmtime)
00071
00072 #define l_gmtime(t,r) ((void)r, gmtime(t))
00073 #define l_localtime(t,r) ((void)r, localtime(t))
00074
00075 #endif
00076
00077
00078
00079 static int os_execute (lua_State *L) {
00080 const char *cmd = luaL_optstring(L, 1, NULL);
00081 int stat = system(cmd);
00082 if (cmd != NULL)
00083 return luaL_execresult(L, stat);
00084 else {
00085 lua_pushboolean(L, stat);
00086 return 1;
00087 }
00088 }
00089
00090
00091 static int os_remove (lua_State *L) {
00092 const char *filename = luaL_checkstring(L, 1);
00093 return luaL_fileresult(L, remove(filename) == 0, filename);
00094 }
00095
00096
00097 static int os_rename (lua_State *L) {
00098 const char *fromname = luaL_checkstring(L, 1);
00099 const char *toname = luaL_checkstring(L, 2);
00100 return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);
00101 }
00102
00103
00104 static int os_tmpname (lua_State *L) {
00105 char buff[LUA_TMPNAMBUFSIZE];
00106 int err;
00107 lua_tmpnam(buff, err);
00108 if (err)
00109 return luaL_error(L, "unable to generate a unique filename");
00110 lua_pushstring(L, buff);
00111 return 1;
00112 }
00113
00114
00115 static int os_getenv (lua_State *L) {
00116 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
00117 return 1;
00118 }
00119
00120
00121 static int os_clock (lua_State *L) {
00122 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
00123 return 1;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 static void setfield (lua_State *L, const char *key, int value) {
00136 lua_pushinteger(L, value);
00137 lua_setfield(L, -2, key);
00138 }
00139
00140 static void setboolfield (lua_State *L, const char *key, int value) {
00141 if (value < 0)
00142 return;
00143 lua_pushboolean(L, value);
00144 lua_setfield(L, -2, key);
00145 }
00146
00147 static int getboolfield (lua_State *L, const char *key) {
00148 int res;
00149 lua_getfield(L, -1, key);
00150 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
00151 lua_pop(L, 1);
00152 return res;
00153 }
00154
00155
00156 static int getfield (lua_State *L, const char *key, int d) {
00157 int res, isnum;
00158 lua_getfield(L, -1, key);
00159 res = (int)lua_tointegerx(L, -1, &isnum);
00160 if (!isnum) {
00161 if (d < 0)
00162 return luaL_error(L, "field " LUA_QS " missing in date table", key);
00163 res = d;
00164 }
00165 lua_pop(L, 1);
00166 return res;
00167 }
00168
00169
00170 static const char *checkoption (lua_State *L, const char *conv, char *buff) {
00171 static const char *const options[] = LUA_STRFTIMEOPTIONS;
00172 unsigned int i;
00173 for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
00174 if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
00175 buff[1] = *conv;
00176 if (*options[i + 1] == '\0') {
00177 buff[2] = '\0';
00178 return conv + 1;
00179 }
00180 else if (*(conv + 1) != '\0' &&
00181 strchr(options[i + 1], *(conv + 1)) != NULL) {
00182 buff[2] = *(conv + 1);
00183 buff[3] = '\0';
00184 return conv + 2;
00185 }
00186 }
00187 }
00188 luaL_argerror(L, 1,
00189 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
00190 return conv;
00191 }
00192
00193
00194 static int os_date (lua_State *L) {
00195 const char *s = luaL_optstring(L, 1, "%c");
00196 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
00197 struct tm tmr, *stm;
00198 if (*s == '!') {
00199 stm = l_gmtime(&t, &tmr);
00200 s++;
00201 }
00202 else
00203 stm = l_localtime(&t, &tmr);
00204 if (stm == NULL)
00205 lua_pushnil(L);
00206 else if (strcmp(s, "*t") == 0) {
00207 lua_createtable(L, 0, 9);
00208 setfield(L, "sec", stm->tm_sec);
00209 setfield(L, "min", stm->tm_min);
00210 setfield(L, "hour", stm->tm_hour);
00211 setfield(L, "day", stm->tm_mday);
00212 setfield(L, "month", stm->tm_mon+1);
00213 setfield(L, "year", stm->tm_year+1900);
00214 setfield(L, "wday", stm->tm_wday+1);
00215 setfield(L, "yday", stm->tm_yday+1);
00216 setboolfield(L, "isdst", stm->tm_isdst);
00217 }
00218 else {
00219 char cc[4];
00220 luaL_Buffer b;
00221 cc[0] = '%';
00222 luaL_buffinit(L, &b);
00223 while (*s) {
00224 if (*s != '%')
00225 luaL_addchar(&b, *s++);
00226 else {
00227 size_t reslen;
00228 char buff[200];
00229 s = checkoption(L, s + 1, cc);
00230 reslen = strftime(buff, sizeof(buff), cc, stm);
00231 luaL_addlstring(&b, buff, reslen);
00232 }
00233 }
00234 luaL_pushresult(&b);
00235 }
00236 return 1;
00237 }
00238
00239
00240 static int os_time (lua_State *L) {
00241 time_t t;
00242 if (lua_isnoneornil(L, 1))
00243 t = time(NULL);
00244 else {
00245 struct tm ts;
00246 luaL_checktype(L, 1, LUA_TTABLE);
00247 lua_settop(L, 1);
00248 ts.tm_sec = getfield(L, "sec", 0);
00249 ts.tm_min = getfield(L, "min", 0);
00250 ts.tm_hour = getfield(L, "hour", 12);
00251 ts.tm_mday = getfield(L, "day", -1);
00252 ts.tm_mon = getfield(L, "month", -1) - 1;
00253 ts.tm_year = getfield(L, "year", -1) - 1900;
00254 ts.tm_isdst = getboolfield(L, "isdst");
00255 t = mktime(&ts);
00256 }
00257 if (t == (time_t)(-1))
00258 lua_pushnil(L);
00259 else
00260 lua_pushnumber(L, (lua_Number)t);
00261 return 1;
00262 }
00263
00264
00265 static int os_difftime (lua_State *L) {
00266 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
00267 (time_t)(luaL_optnumber(L, 2, 0))));
00268 return 1;
00269 }
00270
00271
00272
00273
00274 static int os_setlocale (lua_State *L) {
00275 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
00276 LC_NUMERIC, LC_TIME};
00277 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
00278 "numeric", "time", NULL};
00279 const char *l = luaL_optstring(L, 1, NULL);
00280 int op = luaL_checkoption(L, 2, "all", catnames);
00281 lua_pushstring(L, setlocale(cat[op], l));
00282 return 1;
00283 }
00284
00285
00286 static int os_exit (lua_State *L) {
00287 int status;
00288 if (lua_isboolean(L, 1))
00289 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
00290 else
00291 status = luaL_optint(L, 1, EXIT_SUCCESS);
00292 if (lua_toboolean(L, 2))
00293 lua_close(L);
00294 if (L) exit(status);
00295 return 0;
00296 }
00297
00298
00299 static const luaL_Reg syslib[] = {
00300 {"clock", os_clock},
00301 {"date", os_date},
00302 {"difftime", os_difftime},
00303 {"execute", os_execute},
00304 {"exit", os_exit},
00305 {"getenv", os_getenv},
00306 {"remove", os_remove},
00307 {"rename", os_rename},
00308 {"setlocale", os_setlocale},
00309 {"time", os_time},
00310 {"tmpname", os_tmpname},
00311 {NULL, NULL}
00312 };
00313
00314
00315
00316
00317
00318 LUAMOD_API int luaopen_os (lua_State *L) {
00319 luaL_newlib(L, syslib);
00320 return 1;
00321 }
00322