00001 /* 00002 ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 00003 ** Standard Operating System library 00004 ** See Copyright Notice in lua.h 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 #ifdef LUA_USE_SYMBIAN_PIPS 00023 #define CLOCKS_PER_SEC 128 00024 #endif 00025 00026 00027 static int os_pushresult (lua_State *L, int i, const char *filename) { 00028 int en = errno; /* calls to Lua API may change this value */ 00029 if (i) { 00030 lua_pushboolean(L, 1); 00031 return 1; 00032 } 00033 else { 00034 lua_pushnil(L); 00035 lua_pushfstring(L, "%s: %s", filename, strerror(en)); 00036 lua_pushinteger(L, en); 00037 return 3; 00038 } 00039 } 00040 00041 00042 static int os_execute (lua_State *L) { 00043 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); 00044 return 1; 00045 } 00046 00047 00048 static int os_remove (lua_State *L) { 00049 const char *filename = luaL_checkstring(L, 1); 00050 return os_pushresult(L, remove(filename) == 0, filename); 00051 } 00052 00053 00054 static int os_rename (lua_State *L) { 00055 const char *fromname = luaL_checkstring(L, 1); 00056 const char *toname = luaL_checkstring(L, 2); 00057 return os_pushresult(L, rename(fromname, toname) == 0, fromname); 00058 } 00059 00060 00061 static int os_tmpname (lua_State *L) { 00062 char buff[LUA_TMPNAMBUFSIZE]; 00063 int err; 00064 lua_tmpnam(buff, err); 00065 if (err) 00066 return luaL_error(L, "unable to generate a unique filename"); 00067 lua_pushstring(L, buff); 00068 return 1; 00069 } 00070 00071 00072 static int os_getenv (lua_State *L) { 00073 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ 00074 return 1; 00075 } 00076 00077 00078 static int os_clock (lua_State *L) { 00079 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); 00080 return 1; 00081 } 00082 00083 00084 /* 00085 ** {====================================================== 00086 ** Time/Date operations 00087 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 00088 ** wday=%w+1, yday=%j, isdst=? } 00089 ** ======================================================= 00090 */ 00091 00092 static void setfield (lua_State *L, const char *key, int value) { 00093 lua_pushinteger(L, value); 00094 lua_setfield(L, -2, key); 00095 } 00096 00097 static void setboolfield (lua_State *L, const char *key, int value) { 00098 if (value < 0) /* undefined? */ 00099 return; /* does not set field */ 00100 lua_pushboolean(L, value); 00101 lua_setfield(L, -2, key); 00102 } 00103 00104 static int getboolfield (lua_State *L, const char *key) { 00105 int res; 00106 lua_getfield(L, -1, key); 00107 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 00108 lua_pop(L, 1); 00109 return res; 00110 } 00111 00112 00113 static int getfield (lua_State *L, const char *key, int d) { 00114 int res; 00115 lua_getfield(L, -1, key); 00116 if (lua_isnumber(L, -1)) 00117 res = (int)lua_tointeger(L, -1); 00118 else { 00119 if (d < 0) 00120 return luaL_error(L, "field " LUA_QS " missing in date table", key); 00121 res = d; 00122 } 00123 lua_pop(L, 1); 00124 return res; 00125 } 00126 00127 00128 static int os_date (lua_State *L) { 00129 const char *s = luaL_optstring(L, 1, "%c"); 00130 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 00131 struct tm *stm; 00132 if (*s == '!') { /* UTC? */ 00133 stm = gmtime(&t); 00134 s++; /* skip `!' */ 00135 } 00136 else 00137 stm = localtime(&t); 00138 if (stm == NULL) /* invalid date? */ 00139 lua_pushnil(L); 00140 else if (strcmp(s, "*t") == 0) { 00141 lua_createtable(L, 0, 9); /* 9 = number of fields */ 00142 setfield(L, "sec", stm->tm_sec); 00143 setfield(L, "min", stm->tm_min); 00144 setfield(L, "hour", stm->tm_hour); 00145 setfield(L, "day", stm->tm_mday); 00146 setfield(L, "month", stm->tm_mon+1); 00147 setfield(L, "year", stm->tm_year+1900); 00148 setfield(L, "wday", stm->tm_wday+1); 00149 setfield(L, "yday", stm->tm_yday+1); 00150 setboolfield(L, "isdst", stm->tm_isdst); 00151 } 00152 else { 00153 char cc[3]; 00154 luaL_Buffer b; 00155 cc[0] = '%'; cc[2] = '\0'; 00156 luaL_buffinit(L, &b); 00157 for (; *s; s++) { 00158 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 00159 luaL_addchar(&b, *s); 00160 else { 00161 size_t reslen; 00162 char buff[200]; /* should be big enough for any conversion result */ 00163 cc[1] = *(++s); 00164 reslen = strftime(buff, sizeof(buff), cc, stm); 00165 luaL_addlstring(&b, buff, reslen); 00166 } 00167 } 00168 luaL_pushresult(&b); 00169 } 00170 return 1; 00171 } 00172 00173 00174 static int os_time (lua_State *L) { 00175 time_t t; 00176 if (lua_isnoneornil(L, 1)) /* called without args? */ 00177 t = time(NULL); /* get current time */ 00178 else { 00179 struct tm ts; 00180 luaL_checktype(L, 1, LUA_TTABLE); 00181 lua_settop(L, 1); /* make sure table is at the top */ 00182 ts.tm_sec = getfield(L, "sec", 0); 00183 ts.tm_min = getfield(L, "min", 0); 00184 ts.tm_hour = getfield(L, "hour", 12); 00185 ts.tm_mday = getfield(L, "day", -1); 00186 ts.tm_mon = getfield(L, "month", -1) - 1; 00187 ts.tm_year = getfield(L, "year", -1) - 1900; 00188 ts.tm_isdst = getboolfield(L, "isdst"); 00189 t = mktime(&ts); 00190 } 00191 if (t == (time_t)(-1)) 00192 lua_pushnil(L); 00193 else 00194 lua_pushnumber(L, (lua_Number)t); 00195 return 1; 00196 } 00197 00198 00199 static int os_difftime (lua_State *L) { 00200 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), 00201 (time_t)(luaL_optnumber(L, 2, 0)))); 00202 return 1; 00203 } 00204 00205 /* }====================================================== */ 00206 00207 00208 static int os_setlocale (lua_State *L) { 00209 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, 00210 LC_NUMERIC, LC_TIME}; 00211 static const char *const catnames[] = {"all", "collate", "ctype", "monetary", 00212 "numeric", "time", NULL}; 00213 const char *l = luaL_optstring(L, 1, NULL); 00214 int op = luaL_checkoption(L, 2, "all", catnames); 00215 lua_pushstring(L, setlocale(cat[op], l)); 00216 return 1; 00217 } 00218 00219 00220 static int os_exit (lua_State *L) { 00221 exit(luaL_optint(L, 1, EXIT_SUCCESS)); 00222 } 00223 00224 static const luaL_Reg syslib[] = { 00225 {"clock", os_clock}, 00226 {"date", os_date}, 00227 {"difftime", os_difftime}, 00228 {"execute", os_execute}, 00229 {"exit", os_exit}, 00230 {"getenv", os_getenv}, 00231 {"remove", os_remove}, 00232 {"rename", os_rename}, 00233 {"setlocale", os_setlocale}, 00234 {"time", os_time}, 00235 {"tmpname", os_tmpname}, 00236 {NULL, NULL} 00237 }; 00238 00239 /* }====================================================== */ 00240 00241 00242 00243 LUALIB_API int luaopen_os (lua_State *L) { 00244 luaL_register(L, LUA_OSLIBNAME, syslib); 00245 return 1; 00246 } 00247
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1