| 1 | /* |
| 2 | ** $Id: loslib.c $ |
| 3 | ** Standard Operating System library |
| 4 | ** See Copyright Notice in lua.h |
| 5 | */ |
| 6 | |
| 7 | #define loslib_c |
| 8 | #define LUA_LIB |
| 9 | |
| 10 | #include "lprefix.h" |
| 11 | |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <locale.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | |
| 19 | #include "lua.h" |
| 20 | |
| 21 | #include "lauxlib.h" |
| 22 | #include "lualib.h" |
| 23 | |
| 24 | |
| 25 | /* |
| 26 | ** {================================================================== |
| 27 | ** List of valid conversion specifiers for the 'strftime' function; |
| 28 | ** options are grouped by length; group of length 2 start with '||'. |
| 29 | ** =================================================================== |
| 30 | */ |
| 31 | #if !defined(LUA_STRFTIMEOPTIONS) /* { */ |
| 32 | |
| 33 | /* options for ANSI C 89 (only 1-char options) */ |
| 34 | #define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" |
| 35 | |
| 36 | /* options for ISO C 99 and POSIX */ |
| 37 | #define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ |
| 38 | "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ |
| 39 | |
| 40 | /* options for Windows */ |
| 41 | #define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ |
| 42 | "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ |
| 43 | |
| 44 | #if defined(LUA_USE_WINDOWS) |
| 45 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN |
| 46 | #elif defined(LUA_USE_C89) |
| 47 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 |
| 48 | #else /* C99 specification */ |
| 49 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 |
| 50 | #endif |
| 51 | |
| 52 | #endif /* } */ |
| 53 | /* }================================================================== */ |
| 54 | |
| 55 | |
| 56 | /* |
| 57 | ** {================================================================== |
| 58 | ** Configuration for time-related stuff |
| 59 | ** =================================================================== |
| 60 | */ |
| 61 | |
| 62 | /* |
| 63 | ** type to represent time_t in Lua |
| 64 | */ |
| 65 | #if !defined(LUA_NUMTIME) /* { */ |
| 66 | |
| 67 | #define l_timet lua_Integer |
| 68 | #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) |
| 69 | #define l_gettime(L,arg) luaL_checkinteger(L, arg) |
| 70 | |
| 71 | #else /* }{ */ |
| 72 | |
| 73 | #define l_timet lua_Number |
| 74 | #define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) |
| 75 | #define l_gettime(L,arg) luaL_checknumber(L, arg) |
| 76 | |
| 77 | #endif /* } */ |
| 78 | |
| 79 | |
| 80 | #if !defined(l_gmtime) /* { */ |
| 81 | /* |
| 82 | ** By default, Lua uses gmtime/localtime, except when POSIX is available, |
| 83 | ** where it uses gmtime_r/localtime_r |
| 84 | */ |
| 85 | |
| 86 | #if defined(LUA_USE_POSIX) /* { */ |
| 87 | |
| 88 | #define l_gmtime(t,r) gmtime_r(t,r) |
| 89 | #define l_localtime(t,r) localtime_r(t,r) |
| 90 | |
| 91 | #else /* }{ */ |
| 92 | |
| 93 | /* ISO C definitions */ |
| 94 | #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) |
| 95 | #define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) |
| 96 | |
| 97 | #endif /* } */ |
| 98 | |
| 99 | #endif /* } */ |
| 100 | |
| 101 | /* }================================================================== */ |
| 102 | |
| 103 | |
| 104 | /* |
| 105 | ** {================================================================== |
| 106 | ** Configuration for 'tmpnam': |
| 107 | ** By default, Lua uses tmpnam except when POSIX is available, where |
| 108 | ** it uses mkstemp. |
| 109 | ** =================================================================== |
| 110 | */ |
| 111 | #if !defined(lua_tmpnam) /* { */ |
| 112 | |
| 113 | #if defined(LUA_USE_POSIX) /* { */ |
| 114 | |
| 115 | #include <unistd.h> |
| 116 | |
| 117 | #define LUA_TMPNAMBUFSIZE 32 |
| 118 | |
| 119 | #if !defined(LUA_TMPNAMTEMPLATE) |
| 120 | #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX" |
| 121 | #endif |
| 122 | |
| 123 | #define lua_tmpnam(b,e) { \ |
| 124 | strcpy(b, LUA_TMPNAMTEMPLATE); \ |
| 125 | e = mkstemp(b); \ |
| 126 | if (e != -1) close(e); \ |
| 127 | e = (e == -1); } |
| 128 | |
| 129 | #else /* }{ */ |
| 130 | |
| 131 | /* ISO C definitions */ |
| 132 | #define LUA_TMPNAMBUFSIZE L_tmpnam |
| 133 | #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } |
| 134 | |
| 135 | #endif /* } */ |
| 136 | |
| 137 | #endif /* } */ |
| 138 | /* }================================================================== */ |
| 139 | |
| 140 | |
| 141 | |
| 142 | static int os_execute (lua_State *L) { |
| 143 | #if HAVE_SYSTEM |
| 144 | const char *cmd = luaL_optstring(L, 1, NULL); |
| 145 | int stat; |
| 146 | errno = 0; |
| 147 | stat = system(cmd); |
| 148 | if (cmd != NULL) |
| 149 | return luaL_execresult(L, stat); |
| 150 | else { |
| 151 | lua_pushboolean(L, stat); /* true if there is a shell */ |
| 152 | return 1; |
| 153 | } |
| 154 | #else |
| 155 | return 0; |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | |
| 160 | static int os_remove (lua_State *L) { |
| 161 | const char *filename = luaL_checkstring(L, 1); |
| 162 | return luaL_fileresult(L, remove(filename) == 0, filename); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int os_rename (lua_State *L) { |
| 167 | const char *fromname = luaL_checkstring(L, 1); |
| 168 | const char *toname = luaL_checkstring(L, 2); |
| 169 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int os_tmpname (lua_State *L) { |
| 174 | #if 0 |
| 175 | char buff[LUA_TMPNAMBUFSIZE]; |
| 176 | int err; |
| 177 | lua_tmpnam(buff, err); |
| 178 | if (l_unlikely(err)) |
| 179 | return luaL_error(L, "unable to generate a unique filename" ); |
| 180 | lua_pushstring(L, buff); |
| 181 | return 1; |
| 182 | #else |
| 183 | return 0; |
| 184 | #endif |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static int os_getenv (lua_State *L) { |
| 189 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static int os_clock (lua_State *L) { |
| 195 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /* |
| 201 | ** {====================================================== |
| 202 | ** Time/Date operations |
| 203 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, |
| 204 | ** wday=%w+1, yday=%j, isdst=? } |
| 205 | ** ======================================================= |
| 206 | */ |
| 207 | |
| 208 | /* |
| 209 | ** About the overflow check: an overflow cannot occur when time |
| 210 | ** is represented by a lua_Integer, because either lua_Integer is |
| 211 | ** large enough to represent all int fields or it is not large enough |
| 212 | ** to represent a time that cause a field to overflow. However, if |
| 213 | ** times are represented as doubles and lua_Integer is int, then the |
| 214 | ** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 |
| 215 | ** to compute the year. |
| 216 | */ |
| 217 | static void setfield (lua_State *L, const char *key, int value, int delta) { |
| 218 | #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) |
| 219 | if (l_unlikely(value > LUA_MAXINTEGER - delta)) |
| 220 | luaL_error(L, "field '%s' is out-of-bound" , key); |
| 221 | #endif |
| 222 | lua_pushinteger(L, (lua_Integer)value + delta); |
| 223 | lua_setfield(L, -2, key); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void setboolfield (lua_State *L, const char *key, int value) { |
| 228 | if (value < 0) /* undefined? */ |
| 229 | return; /* does not set field */ |
| 230 | lua_pushboolean(L, value); |
| 231 | lua_setfield(L, -2, key); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* |
| 236 | ** Set all fields from structure 'tm' in the table on top of the stack |
| 237 | */ |
| 238 | static void setallfields (lua_State *L, struct tm *stm) { |
| 239 | setfield(L, "year" , stm->tm_year, 1900); |
| 240 | setfield(L, "month" , stm->tm_mon, 1); |
| 241 | setfield(L, "day" , stm->tm_mday, 0); |
| 242 | setfield(L, "hour" , stm->tm_hour, 0); |
| 243 | setfield(L, "min" , stm->tm_min, 0); |
| 244 | setfield(L, "sec" , stm->tm_sec, 0); |
| 245 | setfield(L, "yday" , stm->tm_yday, 1); |
| 246 | setfield(L, "wday" , stm->tm_wday, 1); |
| 247 | setboolfield(L, "isdst" , stm->tm_isdst); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | static int getboolfield (lua_State *L, const char *key) { |
| 252 | int res; |
| 253 | res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1); |
| 254 | lua_pop(L, 1); |
| 255 | return res; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | static int getfield (lua_State *L, const char *key, int d, int delta) { |
| 260 | int isnum; |
| 261 | int t = lua_getfield(L, -1, key); /* get field and its type */ |
| 262 | lua_Integer res = lua_tointegerx(L, -1, &isnum); |
| 263 | if (!isnum) { /* field is not an integer? */ |
| 264 | if (l_unlikely(t != LUA_TNIL)) /* some other value? */ |
| 265 | return luaL_error(L, "field '%s' is not an integer" , key); |
| 266 | else if (l_unlikely(d < 0)) /* absent field; no default? */ |
| 267 | return luaL_error(L, "field '%s' missing in date table" , key); |
| 268 | res = d; |
| 269 | } |
| 270 | else { |
| 271 | /* unsigned avoids overflow when lua_Integer has 32 bits */ |
| 272 | if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta |
| 273 | : (lua_Integer)INT_MIN + delta <= res)) |
| 274 | return luaL_error(L, "field '%s' is out-of-bound" , key); |
| 275 | res -= delta; |
| 276 | } |
| 277 | lua_pop(L, 1); |
| 278 | return (int)res; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | static const char *checkoption (lua_State *L, const char *conv, |
| 283 | ptrdiff_t convlen, char *buff) { |
| 284 | const char *option = LUA_STRFTIMEOPTIONS; |
| 285 | int oplen = 1; /* length of options being checked */ |
| 286 | for (; *option != '\0' && oplen <= convlen; option += oplen) { |
| 287 | if (*option == '|') /* next block? */ |
| 288 | oplen++; /* will check options with next length (+1) */ |
| 289 | else if (memcmp(conv, option, oplen) == 0) { /* match? */ |
| 290 | memcpy(buff, conv, oplen); /* copy valid option to buffer */ |
| 291 | buff[oplen] = '\0'; |
| 292 | return conv + oplen; /* return next item */ |
| 293 | } |
| 294 | } |
| 295 | luaL_argerror(L, 1, |
| 296 | lua_pushfstring(L, "invalid conversion specifier '%%%s'" , conv)); |
| 297 | return conv; /* to avoid warnings */ |
| 298 | } |
| 299 | |
| 300 | |
| 301 | static time_t l_checktime (lua_State *L, int arg) { |
| 302 | l_timet t = l_gettime(L, arg); |
| 303 | luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds" ); |
| 304 | return (time_t)t; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* maximum size for an individual 'strftime' item */ |
| 309 | #define SIZETIMEFMT 250 |
| 310 | |
| 311 | |
| 312 | static int os_date (lua_State *L) { |
| 313 | size_t slen; |
| 314 | const char *s = luaL_optlstring(L, 1, "%c" , &slen); |
| 315 | time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); |
| 316 | const char *se = s + slen; /* 's' end */ |
| 317 | struct tm tmr, *stm; |
| 318 | if (*s == '!') { /* UTC? */ |
| 319 | stm = l_gmtime(&t, &tmr); |
| 320 | s++; /* skip '!' */ |
| 321 | } |
| 322 | else |
| 323 | stm = l_localtime(&t, &tmr); |
| 324 | if (stm == NULL) /* invalid date? */ |
| 325 | return luaL_error(L, |
| 326 | "date result cannot be represented in this installation" ); |
| 327 | if (strcmp(s, "*t" ) == 0) { |
| 328 | lua_createtable(L, 0, 9); /* 9 = number of fields */ |
| 329 | setallfields(L, stm); |
| 330 | } |
| 331 | else { |
| 332 | char cc[4]; /* buffer for individual conversion specifiers */ |
| 333 | luaL_Buffer b; |
| 334 | cc[0] = '%'; |
| 335 | luaL_buffinit(L, &b); |
| 336 | while (s < se) { |
| 337 | if (*s != '%') /* not a conversion specifier? */ |
| 338 | luaL_addchar(&b, *s++); |
| 339 | else { |
| 340 | size_t reslen; |
| 341 | char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); |
| 342 | s++; /* skip '%' */ |
| 343 | s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ |
| 344 | reslen = strftime(buff, SIZETIMEFMT, cc, stm); |
| 345 | luaL_addsize(&b, reslen); |
| 346 | } |
| 347 | } |
| 348 | luaL_pushresult(&b); |
| 349 | } |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static int os_time (lua_State *L) { |
| 355 | time_t t; |
| 356 | if (lua_isnoneornil(L, 1)) /* called without args? */ |
| 357 | t = time(NULL); /* get current time */ |
| 358 | else { |
| 359 | struct tm ts; |
| 360 | luaL_checktype(L, 1, LUA_TTABLE); |
| 361 | lua_settop(L, 1); /* make sure table is at the top */ |
| 362 | ts.tm_year = getfield(L, "year" , -1, 1900); |
| 363 | ts.tm_mon = getfield(L, "month" , -1, 1); |
| 364 | ts.tm_mday = getfield(L, "day" , -1, 0); |
| 365 | ts.tm_hour = getfield(L, "hour" , 12, 0); |
| 366 | ts.tm_min = getfield(L, "min" , 0, 0); |
| 367 | ts.tm_sec = getfield(L, "sec" , 0, 0); |
| 368 | ts.tm_isdst = getboolfield(L, "isdst" ); |
| 369 | t = mktime(&ts); |
| 370 | setallfields(L, &ts); /* update fields with normalized values */ |
| 371 | } |
| 372 | if (t != (time_t)(l_timet)t || t == (time_t)(-1)) |
| 373 | return luaL_error(L, |
| 374 | "time result cannot be represented in this installation" ); |
| 375 | l_pushtime(L, t); |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | static int os_difftime (lua_State *L) { |
| 381 | time_t t1 = l_checktime(L, 1); |
| 382 | time_t t2 = l_checktime(L, 2); |
| 383 | lua_pushnumber(L, (lua_Number)difftime(t1, t2)); |
| 384 | return 1; |
| 385 | } |
| 386 | |
| 387 | /* }====================================================== */ |
| 388 | |
| 389 | |
| 390 | static int os_setlocale (lua_State *L) { |
| 391 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, |
| 392 | LC_NUMERIC, LC_TIME}; |
| 393 | static const char *const catnames[] = {"all" , "collate" , "ctype" , "monetary" , |
| 394 | "numeric" , "time" , NULL}; |
| 395 | const char *l = luaL_optstring(L, 1, NULL); |
| 396 | int op = luaL_checkoption(L, 2, "all" , catnames); |
| 397 | lua_pushstring(L, setlocale(cat[op], l)); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | static int os_exit (lua_State *L) { |
| 403 | int status; |
| 404 | if (lua_isboolean(L, 1)) |
| 405 | status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE); |
| 406 | else |
| 407 | status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS); |
| 408 | if (lua_toboolean(L, 2)) |
| 409 | lua_close(L); |
| 410 | if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */ |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | static const luaL_Reg syslib[] = { |
| 416 | {"clock" , os_clock}, |
| 417 | {"date" , os_date}, |
| 418 | {"difftime" , os_difftime}, |
| 419 | {"execute" , os_execute}, |
| 420 | {"exit" , os_exit}, |
| 421 | {"getenv" , os_getenv}, |
| 422 | {"remove" , os_remove}, |
| 423 | {"rename" , os_rename}, |
| 424 | {"setlocale" , os_setlocale}, |
| 425 | {"time" , os_time}, |
| 426 | {"tmpname" , os_tmpname}, |
| 427 | {NULL, NULL} |
| 428 | }; |
| 429 | |
| 430 | /* }====================================================== */ |
| 431 | |
| 432 | |
| 433 | |
| 434 | LUAMOD_API int luaopen_os (lua_State *L) { |
| 435 | luaL_newlib(L, syslib); |
| 436 | return 1; |
| 437 | } |
| 438 | |