lua/loslib.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 19:50:43 -0400
changeset 57 4974e5368a29
parent 0 d7ee4e2ed49d
permissions -rw-r--r--
Minor type cleanup ("unsigned char" -> "uint8_t").
icculus@0
     1
/*
icculus@0
     2
** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $
icculus@0
     3
** Standard Operating System library
icculus@0
     4
** See Copyright Notice in lua.h
icculus@0
     5
*/
icculus@0
     6
icculus@0
     7
icculus@0
     8
#include <errno.h>
icculus@0
     9
#include <locale.h>
icculus@0
    10
#include <stdlib.h>
icculus@0
    11
#include <string.h>
icculus@0
    12
#include <time.h>
icculus@0
    13
icculus@0
    14
#define loslib_c
icculus@0
    15
#define LUA_LIB
icculus@0
    16
icculus@0
    17
#include "lua.h"
icculus@0
    18
icculus@0
    19
#include "lauxlib.h"
icculus@0
    20
#include "lualib.h"
icculus@0
    21
icculus@0
    22
icculus@0
    23
/*
icculus@0
    24
** list of valid conversion specifiers for the 'strftime' function
icculus@0
    25
*/
icculus@0
    26
#if !defined(LUA_STRFTIMEOPTIONS)
icculus@0
    27
icculus@0
    28
#if !defined(LUA_USE_POSIX)
icculus@0
    29
#define LUA_STRFTIMEOPTIONS	{ "aAbBcdHIjmMpSUwWxXyYz%", "" }
icculus@0
    30
#else
icculus@0
    31
#define LUA_STRFTIMEOPTIONS \
icculus@0
    32
	{ "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
icculus@0
    33
	  "", "E", "cCxXyY",  \
icculus@0
    34
	  "O", "deHImMSuUVwWy" }
icculus@0
    35
#endif
icculus@0
    36
icculus@0
    37
#endif
icculus@0
    38
icculus@0
    39
icculus@0
    40
icculus@0
    41
/*
icculus@0
    42
** By default, Lua uses tmpnam except when POSIX is available, where it
icculus@0
    43
** uses mkstemp.
icculus@0
    44
*/
icculus@0
    45
#if defined(LUA_USE_MKSTEMP)
icculus@0
    46
#include <unistd.h>
icculus@0
    47
#define LUA_TMPNAMBUFSIZE	32
icculus@0
    48
#define lua_tmpnam(b,e) { \
icculus@0
    49
        strcpy(b, "/tmp/lua_XXXXXX"); \
icculus@0
    50
        e = mkstemp(b); \
icculus@0
    51
        if (e != -1) close(e); \
icculus@0
    52
        e = (e == -1); }
icculus@0
    53
icculus@0
    54
#elif !defined(lua_tmpnam)
icculus@0
    55
icculus@0
    56
#define LUA_TMPNAMBUFSIZE	L_tmpnam
icculus@0
    57
#define lua_tmpnam(b,e)		{ e = (tmpnam(b) == NULL); }
icculus@0
    58
icculus@0
    59
#endif
icculus@0
    60
icculus@0
    61
icculus@0
    62
/*
icculus@0
    63
** By default, Lua uses gmtime/localtime, except when POSIX is available,
icculus@0
    64
** where it uses gmtime_r/localtime_r
icculus@0
    65
*/
icculus@0
    66
#if defined(LUA_USE_GMTIME_R)
icculus@0
    67
icculus@0
    68
#define l_gmtime(t,r)		gmtime_r(t,r)
icculus@0
    69
#define l_localtime(t,r)	localtime_r(t,r)
icculus@0
    70
icculus@0
    71
#elif !defined(l_gmtime)
icculus@0
    72
icculus@0
    73
#define l_gmtime(t,r)		((void)r, gmtime(t))
icculus@0
    74
#define l_localtime(t,r)  	((void)r, localtime(t))
icculus@0
    75
icculus@0
    76
#endif
icculus@0
    77
icculus@0
    78
icculus@0
    79
icculus@0
    80
static int os_execute (lua_State *L) {
icculus@0
    81
  const char *cmd = luaL_optstring(L, 1, NULL);
icculus@0
    82
  int stat = system(cmd);
icculus@0
    83
  if (cmd != NULL)
icculus@0
    84
    return luaL_execresult(L, stat);
icculus@0
    85
  else {
icculus@0
    86
    lua_pushboolean(L, stat);  /* true if there is a shell */
icculus@0
    87
    return 1;
icculus@0
    88
  }
icculus@0
    89
}
icculus@0
    90
icculus@0
    91
icculus@0
    92
static int os_remove (lua_State *L) {
icculus@0
    93
  const char *filename = luaL_checkstring(L, 1);
icculus@0
    94
  return luaL_fileresult(L, remove(filename) == 0, filename);
icculus@0
    95
}
icculus@0
    96
icculus@0
    97
icculus@0
    98
static int os_rename (lua_State *L) {
icculus@0
    99
  const char *fromname = luaL_checkstring(L, 1);
icculus@0
   100
  const char *toname = luaL_checkstring(L, 2);
icculus@0
   101
  return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
icculus@0
   102
}
icculus@0
   103
icculus@0
   104
icculus@0
   105
static int os_tmpname (lua_State *L) {
icculus@0
   106
  char buff[LUA_TMPNAMBUFSIZE];
icculus@0
   107
  int err;
icculus@0
   108
  lua_tmpnam(buff, err);
icculus@0
   109
  if (err)
icculus@0
   110
    return luaL_error(L, "unable to generate a unique filename");
icculus@0
   111
  lua_pushstring(L, buff);
icculus@0
   112
  return 1;
icculus@0
   113
}
icculus@0
   114
icculus@0
   115
icculus@0
   116
static int os_getenv (lua_State *L) {
icculus@0
   117
  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
icculus@0
   118
  return 1;
icculus@0
   119
}
icculus@0
   120
icculus@0
   121
icculus@0
   122
static int os_clock (lua_State *L) {
icculus@0
   123
  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
icculus@0
   124
  return 1;
icculus@0
   125
}
icculus@0
   126
icculus@0
   127
icculus@0
   128
/*
icculus@0
   129
** {======================================================
icculus@0
   130
** Time/Date operations
icculus@0
   131
** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
icculus@0
   132
**   wday=%w+1, yday=%j, isdst=? }
icculus@0
   133
** =======================================================
icculus@0
   134
*/
icculus@0
   135
icculus@0
   136
static void setfield (lua_State *L, const char *key, int value) {
icculus@0
   137
  lua_pushinteger(L, value);
icculus@0
   138
  lua_setfield(L, -2, key);
icculus@0
   139
}
icculus@0
   140
icculus@0
   141
static void setboolfield (lua_State *L, const char *key, int value) {
icculus@0
   142
  if (value < 0)  /* undefined? */
icculus@0
   143
    return;  /* does not set field */
icculus@0
   144
  lua_pushboolean(L, value);
icculus@0
   145
  lua_setfield(L, -2, key);
icculus@0
   146
}
icculus@0
   147
icculus@0
   148
static int getboolfield (lua_State *L, const char *key) {
icculus@0
   149
  int res;
icculus@0
   150
  lua_getfield(L, -1, key);
icculus@0
   151
  res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
icculus@0
   152
  lua_pop(L, 1);
icculus@0
   153
  return res;
icculus@0
   154
}
icculus@0
   155
icculus@0
   156
icculus@0
   157
static int getfield (lua_State *L, const char *key, int d) {
icculus@0
   158
  int res, isnum;
icculus@0
   159
  lua_getfield(L, -1, key);
icculus@0
   160
  res = (int)lua_tointegerx(L, -1, &isnum);
icculus@0
   161
  if (!isnum) {
icculus@0
   162
    if (d < 0)
icculus@0
   163
      return luaL_error(L, "field " LUA_QS " missing in date table", key);
icculus@0
   164
    res = d;
icculus@0
   165
  }
icculus@0
   166
  lua_pop(L, 1);
icculus@0
   167
  return res;
icculus@0
   168
}
icculus@0
   169
icculus@0
   170
icculus@0
   171
static const char *checkoption (lua_State *L, const char *conv, char *buff) {
icculus@0
   172
  static const char *const options[] = LUA_STRFTIMEOPTIONS;
icculus@0
   173
  unsigned int i;
icculus@0
   174
  for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
icculus@0
   175
    if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
icculus@0
   176
      buff[1] = *conv;
icculus@0
   177
      if (*options[i + 1] == '\0') {  /* one-char conversion specifier? */
icculus@0
   178
        buff[2] = '\0';  /* end buffer */
icculus@0
   179
        return conv + 1;
icculus@0
   180
      }
icculus@0
   181
      else if (*(conv + 1) != '\0' &&
icculus@0
   182
               strchr(options[i + 1], *(conv + 1)) != NULL) {
icculus@0
   183
        buff[2] = *(conv + 1);  /* valid two-char conversion specifier */
icculus@0
   184
        buff[3] = '\0';  /* end buffer */
icculus@0
   185
        return conv + 2;
icculus@0
   186
      }
icculus@0
   187
    }
icculus@0
   188
  }
icculus@0
   189
  luaL_argerror(L, 1,
icculus@0
   190
    lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
icculus@0
   191
  return conv;  /* to avoid warnings */
icculus@0
   192
}
icculus@0
   193
icculus@0
   194
icculus@0
   195
static int os_date (lua_State *L) {
icculus@0
   196
  const char *s = luaL_optstring(L, 1, "%c");
icculus@0
   197
  time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
icculus@0
   198
  struct tm tmr, *stm;
icculus@0
   199
  if (*s == '!') {  /* UTC? */
icculus@0
   200
    stm = l_gmtime(&t, &tmr);
icculus@0
   201
    s++;  /* skip `!' */
icculus@0
   202
  }
icculus@0
   203
  else
icculus@0
   204
    stm = l_localtime(&t, &tmr);
icculus@0
   205
  if (stm == NULL)  /* invalid date? */
icculus@0
   206
    lua_pushnil(L);
icculus@0
   207
  else if (strcmp(s, "*t") == 0) {
icculus@0
   208
    lua_createtable(L, 0, 9);  /* 9 = number of fields */
icculus@0
   209
    setfield(L, "sec", stm->tm_sec);
icculus@0
   210
    setfield(L, "min", stm->tm_min);
icculus@0
   211
    setfield(L, "hour", stm->tm_hour);
icculus@0
   212
    setfield(L, "day", stm->tm_mday);
icculus@0
   213
    setfield(L, "month", stm->tm_mon+1);
icculus@0
   214
    setfield(L, "year", stm->tm_year+1900);
icculus@0
   215
    setfield(L, "wday", stm->tm_wday+1);
icculus@0
   216
    setfield(L, "yday", stm->tm_yday+1);
icculus@0
   217
    setboolfield(L, "isdst", stm->tm_isdst);
icculus@0
   218
  }
icculus@0
   219
  else {
icculus@0
   220
    char cc[4];
icculus@0
   221
    luaL_Buffer b;
icculus@0
   222
    cc[0] = '%';
icculus@0
   223
    luaL_buffinit(L, &b);
icculus@0
   224
    while (*s) {
icculus@0
   225
      if (*s != '%')  /* no conversion specifier? */
icculus@0
   226
        luaL_addchar(&b, *s++);
icculus@0
   227
      else {
icculus@0
   228
        size_t reslen;
icculus@0
   229
        char buff[200];  /* should be big enough for any conversion result */
icculus@0
   230
        s = checkoption(L, s + 1, cc);
icculus@0
   231
        reslen = strftime(buff, sizeof(buff), cc, stm);
icculus@0
   232
        luaL_addlstring(&b, buff, reslen);
icculus@0
   233
      }
icculus@0
   234
    }
icculus@0
   235
    luaL_pushresult(&b);
icculus@0
   236
  }
icculus@0
   237
  return 1;
icculus@0
   238
}
icculus@0
   239
icculus@0
   240
icculus@0
   241
static int os_time (lua_State *L) {
icculus@0
   242
  time_t t;
icculus@0
   243
  if (lua_isnoneornil(L, 1))  /* called without args? */
icculus@0
   244
    t = time(NULL);  /* get current time */
icculus@0
   245
  else {
icculus@0
   246
    struct tm ts;
icculus@0
   247
    luaL_checktype(L, 1, LUA_TTABLE);
icculus@0
   248
    lua_settop(L, 1);  /* make sure table is at the top */
icculus@0
   249
    ts.tm_sec = getfield(L, "sec", 0);
icculus@0
   250
    ts.tm_min = getfield(L, "min", 0);
icculus@0
   251
    ts.tm_hour = getfield(L, "hour", 12);
icculus@0
   252
    ts.tm_mday = getfield(L, "day", -1);
icculus@0
   253
    ts.tm_mon = getfield(L, "month", -1) - 1;
icculus@0
   254
    ts.tm_year = getfield(L, "year", -1) - 1900;
icculus@0
   255
    ts.tm_isdst = getboolfield(L, "isdst");
icculus@0
   256
    t = mktime(&ts);
icculus@0
   257
  }
icculus@0
   258
  if (t == (time_t)(-1))
icculus@0
   259
    lua_pushnil(L);
icculus@0
   260
  else
icculus@0
   261
    lua_pushnumber(L, (lua_Number)t);
icculus@0
   262
  return 1;
icculus@0
   263
}
icculus@0
   264
icculus@0
   265
icculus@0
   266
static int os_difftime (lua_State *L) {
icculus@0
   267
  lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
icculus@0
   268
                             (time_t)(luaL_optnumber(L, 2, 0))));
icculus@0
   269
  return 1;
icculus@0
   270
}
icculus@0
   271
icculus@0
   272
/* }====================================================== */
icculus@0
   273
icculus@0
   274
icculus@0
   275
static int os_setlocale (lua_State *L) {
icculus@0
   276
  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
icculus@0
   277
                      LC_NUMERIC, LC_TIME};
icculus@0
   278
  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
icculus@0
   279
     "numeric", "time", NULL};
icculus@0
   280
  const char *l = luaL_optstring(L, 1, NULL);
icculus@0
   281
  int op = luaL_checkoption(L, 2, "all", catnames);
icculus@0
   282
  lua_pushstring(L, setlocale(cat[op], l));
icculus@0
   283
  return 1;
icculus@0
   284
}
icculus@0
   285
icculus@0
   286
icculus@0
   287
static int os_exit (lua_State *L) {
icculus@0
   288
  int status;
icculus@0
   289
  if (lua_isboolean(L, 1))
icculus@0
   290
    status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
icculus@0
   291
  else
icculus@0
   292
    status = luaL_optint(L, 1, EXIT_SUCCESS);
icculus@0
   293
  if (lua_toboolean(L, 2))
icculus@0
   294
    lua_close(L);
icculus@0
   295
  if (L) exit(status);  /* 'if' to avoid warnings for unreachable 'return' */
icculus@0
   296
  return 0;
icculus@0
   297
}
icculus@0
   298
icculus@0
   299
icculus@0
   300
static const luaL_Reg syslib[] = {
icculus@0
   301
  {"clock",     os_clock},
icculus@0
   302
  {"date",      os_date},
icculus@0
   303
  {"difftime",  os_difftime},
icculus@0
   304
  {"execute",   os_execute},
icculus@0
   305
  {"exit",      os_exit},
icculus@0
   306
  {"getenv",    os_getenv},
icculus@0
   307
  {"remove",    os_remove},
icculus@0
   308
  {"rename",    os_rename},
icculus@0
   309
  {"setlocale", os_setlocale},
icculus@0
   310
  {"time",      os_time},
icculus@0
   311
  {"tmpname",   os_tmpname},
icculus@0
   312
  {NULL, NULL}
icculus@0
   313
};
icculus@0
   314
icculus@0
   315
/* }====================================================== */
icculus@0
   316
icculus@0
   317
icculus@0
   318
icculus@0
   319
LUAMOD_API int luaopen_os (lua_State *L) {
icculus@0
   320
  luaL_newlib(L, syslib);
icculus@0
   321
  return 1;
icculus@0
   322
}
icculus@0
   323