loslib.c

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

Generated by  doxygen 1.6.2