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