diff src/lua/loslib.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/loslib.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/loslib.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,243 @@
     1.4 +/*
     1.5 +** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
     1.6 +** Standard Operating System library
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <errno.h>
    1.12 +#include <locale.h>
    1.13 +#include <stdlib.h>
    1.14 +#include <string.h>
    1.15 +#include <time.h>
    1.16 +
    1.17 +#define loslib_c
    1.18 +#define LUA_LIB
    1.19 +
    1.20 +#include "lua.h"
    1.21 +
    1.22 +#include "lauxlib.h"
    1.23 +#include "lualib.h"
    1.24 +
    1.25 +
    1.26 +static int os_pushresult (lua_State *L, int i, const char *filename) {
    1.27 +  int en = errno;  /* calls to Lua API may change this value */
    1.28 +  if (i) {
    1.29 +    lua_pushboolean(L, 1);
    1.30 +    return 1;
    1.31 +  }
    1.32 +  else {
    1.33 +    lua_pushnil(L);
    1.34 +    lua_pushfstring(L, "%s: %s", filename, strerror(en));
    1.35 +    lua_pushinteger(L, en);
    1.36 +    return 3;
    1.37 +  }
    1.38 +}
    1.39 +
    1.40 +
    1.41 +static int os_execute (lua_State *L) {
    1.42 +  lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
    1.43 +  return 1;
    1.44 +}
    1.45 +
    1.46 +
    1.47 +static int os_remove (lua_State *L) {
    1.48 +  const char *filename = luaL_checkstring(L, 1);
    1.49 +  return os_pushresult(L, remove(filename) == 0, filename);
    1.50 +}
    1.51 +
    1.52 +
    1.53 +static int os_rename (lua_State *L) {
    1.54 +  const char *fromname = luaL_checkstring(L, 1);
    1.55 +  const char *toname = luaL_checkstring(L, 2);
    1.56 +  return os_pushresult(L, rename(fromname, toname) == 0, fromname);
    1.57 +}
    1.58 +
    1.59 +
    1.60 +static int os_tmpname (lua_State *L) {
    1.61 +  char buff[LUA_TMPNAMBUFSIZE];
    1.62 +  int err;
    1.63 +  lua_tmpnam(buff, err);
    1.64 +  if (err)
    1.65 +    return luaL_error(L, "unable to generate a unique filename");
    1.66 +  lua_pushstring(L, buff);
    1.67 +  return 1;
    1.68 +}
    1.69 +
    1.70 +
    1.71 +static int os_getenv (lua_State *L) {
    1.72 +  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
    1.73 +  return 1;
    1.74 +}
    1.75 +
    1.76 +
    1.77 +static int os_clock (lua_State *L) {
    1.78 +  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
    1.79 +  return 1;
    1.80 +}
    1.81 +
    1.82 +
    1.83 +/*
    1.84 +** {======================================================
    1.85 +** Time/Date operations
    1.86 +** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
    1.87 +**   wday=%w+1, yday=%j, isdst=? }
    1.88 +** =======================================================
    1.89 +*/
    1.90 +
    1.91 +static void setfield (lua_State *L, const char *key, int value) {
    1.92 +  lua_pushinteger(L, value);
    1.93 +  lua_setfield(L, -2, key);
    1.94 +}
    1.95 +
    1.96 +static void setboolfield (lua_State *L, const char *key, int value) {
    1.97 +  if (value < 0)  /* undefined? */
    1.98 +    return;  /* does not set field */
    1.99 +  lua_pushboolean(L, value);
   1.100 +  lua_setfield(L, -2, key);
   1.101 +}
   1.102 +
   1.103 +static int getboolfield (lua_State *L, const char *key) {
   1.104 +  int res;
   1.105 +  lua_getfield(L, -1, key);
   1.106 +  res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
   1.107 +  lua_pop(L, 1);
   1.108 +  return res;
   1.109 +}
   1.110 +
   1.111 +
   1.112 +static int getfield (lua_State *L, const char *key, int d) {
   1.113 +  int res;
   1.114 +  lua_getfield(L, -1, key);
   1.115 +  if (lua_isnumber(L, -1))
   1.116 +    res = (int)lua_tointeger(L, -1);
   1.117 +  else {
   1.118 +    if (d < 0)
   1.119 +      return luaL_error(L, "field " LUA_QS " missing in date table", key);
   1.120 +    res = d;
   1.121 +  }
   1.122 +  lua_pop(L, 1);
   1.123 +  return res;
   1.124 +}
   1.125 +
   1.126 +
   1.127 +static int os_date (lua_State *L) {
   1.128 +  const char *s = luaL_optstring(L, 1, "%c");
   1.129 +  time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
   1.130 +  struct tm *stm;
   1.131 +  if (*s == '!') {  /* UTC? */
   1.132 +    stm = gmtime(&t);
   1.133 +    s++;  /* skip `!' */
   1.134 +  }
   1.135 +  else
   1.136 +    stm = localtime(&t);
   1.137 +  if (stm == NULL)  /* invalid date? */
   1.138 +    lua_pushnil(L);
   1.139 +  else if (strcmp(s, "*t") == 0) {
   1.140 +    lua_createtable(L, 0, 9);  /* 9 = number of fields */
   1.141 +    setfield(L, "sec", stm->tm_sec);
   1.142 +    setfield(L, "min", stm->tm_min);
   1.143 +    setfield(L, "hour", stm->tm_hour);
   1.144 +    setfield(L, "day", stm->tm_mday);
   1.145 +    setfield(L, "month", stm->tm_mon+1);
   1.146 +    setfield(L, "year", stm->tm_year+1900);
   1.147 +    setfield(L, "wday", stm->tm_wday+1);
   1.148 +    setfield(L, "yday", stm->tm_yday+1);
   1.149 +    setboolfield(L, "isdst", stm->tm_isdst);
   1.150 +  }
   1.151 +  else {
   1.152 +    char cc[3];
   1.153 +    luaL_Buffer b;
   1.154 +    cc[0] = '%'; cc[2] = '\0';
   1.155 +    luaL_buffinit(L, &b);
   1.156 +    for (; *s; s++) {
   1.157 +      if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */
   1.158 +        luaL_addchar(&b, *s);
   1.159 +      else {
   1.160 +        size_t reslen;
   1.161 +        char buff[200];  /* should be big enough for any conversion result */
   1.162 +        cc[1] = *(++s);
   1.163 +        reslen = strftime(buff, sizeof(buff), cc, stm);
   1.164 +        luaL_addlstring(&b, buff, reslen);
   1.165 +      }
   1.166 +    }
   1.167 +    luaL_pushresult(&b);
   1.168 +  }
   1.169 +  return 1;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +static int os_time (lua_State *L) {
   1.174 +  time_t t;
   1.175 +  if (lua_isnoneornil(L, 1))  /* called without args? */
   1.176 +    t = time(NULL);  /* get current time */
   1.177 +  else {
   1.178 +    struct tm ts;
   1.179 +    luaL_checktype(L, 1, LUA_TTABLE);
   1.180 +    lua_settop(L, 1);  /* make sure table is at the top */
   1.181 +    ts.tm_sec = getfield(L, "sec", 0);
   1.182 +    ts.tm_min = getfield(L, "min", 0);
   1.183 +    ts.tm_hour = getfield(L, "hour", 12);
   1.184 +    ts.tm_mday = getfield(L, "day", -1);
   1.185 +    ts.tm_mon = getfield(L, "month", -1) - 1;
   1.186 +    ts.tm_year = getfield(L, "year", -1) - 1900;
   1.187 +    ts.tm_isdst = getboolfield(L, "isdst");
   1.188 +    t = mktime(&ts);
   1.189 +  }
   1.190 +  if (t == (time_t)(-1))
   1.191 +    lua_pushnil(L);
   1.192 +  else
   1.193 +    lua_pushnumber(L, (lua_Number)t);
   1.194 +  return 1;
   1.195 +}
   1.196 +
   1.197 +
   1.198 +static int os_difftime (lua_State *L) {
   1.199 +  lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
   1.200 +                             (time_t)(luaL_optnumber(L, 2, 0))));
   1.201 +  return 1;
   1.202 +}
   1.203 +
   1.204 +/* }====================================================== */
   1.205 +
   1.206 +
   1.207 +static int os_setlocale (lua_State *L) {
   1.208 +  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
   1.209 +                      LC_NUMERIC, LC_TIME};
   1.210 +  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
   1.211 +     "numeric", "time", NULL};
   1.212 +  const char *l = luaL_optstring(L, 1, NULL);
   1.213 +  int op = luaL_checkoption(L, 2, "all", catnames);
   1.214 +  lua_pushstring(L, setlocale(cat[op], l));
   1.215 +  return 1;
   1.216 +}
   1.217 +
   1.218 +
   1.219 +static int os_exit (lua_State *L) {
   1.220 +  exit(luaL_optint(L, 1, EXIT_SUCCESS));
   1.221 +}
   1.222 +
   1.223 +static const luaL_Reg syslib[] = {
   1.224 +  {"clock",     os_clock},
   1.225 +  {"date",      os_date},
   1.226 +  {"difftime",  os_difftime},
   1.227 +  {"execute",   os_execute},
   1.228 +  {"exit",      os_exit},
   1.229 +  {"getenv",    os_getenv},
   1.230 +  {"remove",    os_remove},
   1.231 +  {"rename",    os_rename},
   1.232 +  {"setlocale", os_setlocale},
   1.233 +  {"time",      os_time},
   1.234 +  {"tmpname",   os_tmpname},
   1.235 +  {NULL, NULL}
   1.236 +};
   1.237 +
   1.238 +/* }====================================================== */
   1.239 +
   1.240 +
   1.241 +
   1.242 +LUALIB_API int luaopen_os (lua_State *L) {
   1.243 +  luaL_register(L, LUA_OSLIBNAME, syslib);
   1.244 +  return 1;
   1.245 +}
   1.246 +