Lua 5.1.4: loslib.c
L0001
L0006
L0007
L0008 #include <errno.h>
L0009 #include <locale.h>
L0010 #include <stdlib.h>
L0011 #include <string.h>
L0012 #include <time.h>
L0013
L0014 #define loslib_c
L0015 #define LUA_LIB
L0016
L0017 #include "lua.h"
L0018
L0019 #include "lauxlib.h"
L0020 #include "lualib.h"
L0021
L0022
L0023 static int os_pushresult (lua_State *L, int i, const char *filename) {
L0024 int en = errno;
L0025 if (i) {
L0026 lua_pushboolean(L, 1);
L0027 return 1;
L0028 }
L0029 else {
L0030 lua_pushnil(L);
L0031 lua_pushfstring(L, "%s: %s", filename, strerror(en));
L0032 lua_pushinteger(L, en);
L0033 return 3;
L0034 }
L0035 }
L0036
L0037
L0038 static int os_execute (lua_State *L) {
L0039 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
L0040 return 1;
L0041 }
L0042
L0043
L0044 static int os_remove (lua_State *L) {
L0045 const char *filename = luaL_checkstring(L, 1);
L0046 return os_pushresult(L, remove(filename) == 0, filename);
L0047 }
L0048
L0049
L0050 static int os_rename (lua_State *L) {
L0051 const char *fromname = luaL_checkstring(L, 1);
L0052 const char *toname = luaL_checkstring(L, 2);
L0053 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
L0054 }
L0055
L0056
L0057 static int os_tmpname (lua_State *L) {
L0058 char buff[LUA_TMPNAMBUFSIZE];
L0059 int err;
L0060 lua_tmpnam(buff, err);
L0061 if (err)
L0062 return luaL_error(L, "unable to generate a unique filename");
L0063 lua_pushstring(L, buff);
L0064 return 1;
L0065 }
L0066
L0067
L0068 static int os_getenv (lua_State *L) {
L0069 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
L0070 return 1;
L0071 }
L0072
L0073
L0074 static int os_clock (lua_State *L) {
L0075 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
L0076 return 1;
L0077 }
L0078
L0079
L0080
L0087
L0088 static void setfield (lua_State *L, const char *key, int value) {
L0089 lua_pushinteger(L, value);
L0090 lua_setfield(L, -2, key);
L0091 }
L0092
L0093 static void setboolfield (lua_State *L, const char *key, int value) {
L0094 if (value < 0)
L0095 return;
L0096 lua_pushboolean(L, value);
L0097 lua_setfield(L, -2, key);
L0098 }
L0099
L0100 static int getboolfield (lua_State *L, const char *key) {
L0101 int res;
L0102 lua_getfield(L, -1, key);
L0103 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
L0104 lua_pop(L, 1);
L0105 return res;
L0106 }
L0107
L0108
L0109 static int getfield (lua_State *L, const char *key, int d) {
L0110 int res;
L0111 lua_getfield(L, -1, key);
L0112 if (lua_isnumber(L, -1))
L0113 res = (int)lua_tointeger(L, -1);
L0114 else {
L0115 if (d < 0)
L0116 return luaL_error(L, "field " LUA_QS " missing in date table", key);
L0117 res = d;
L0118 }
L0119 lua_pop(L, 1);
L0120 return res;
L0121 }
L0122
L0123
L0124 static int os_date (lua_State *L) {
L0125 const char *s = luaL_optstring(L, 1, "%c");
L0126 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
L0127 struct tm *stm;
L0128 if (*s == '!') {
L0129 stm = gmtime(&t);
L0130 s++;
L0131 }
L0132 else
L0133 stm = localtime(&t);
L0134 if (stm == NULL)
L0135 lua_pushnil(L);
L0136 else if (strcmp(s, "*t") == 0) {
L0137 lua_createtable(L, 0, 9);
L0138 setfield(L, "sec", stm->tm_sec);
L0139 setfield(L, "min", stm->tm_min);
L0140 setfield(L, "hour", stm->tm_hour);
L0141 setfield(L, "day", stm->tm_mday);
L0142 setfield(L, "month", stm->tm_mon+1);
L0143 setfield(L, "year", stm->tm_year+1900);
L0144 setfield(L, "wday", stm->tm_wday+1);
L0145 setfield(L, "yday", stm->tm_yday+1);
L0146 setboolfield(L, "isdst", stm->tm_isdst);
L0147 }
L0148 else {
L0149 char cc[3];
L0150 luaL_Buffer b;
L0151 cc[0] = '%'; cc[2] = '\0';
L0152 luaL_buffinit(L, &b);
L0153 for (; *s; s++) {
L0154 if (*s != '%' || *(s + 1) == '\0')
L0155 luaL_addchar(&b, *s);
L0156 else {
L0157 size_t reslen;
L0158 char buff[200];
L0159 cc[1] = *(++s);
L0160 reslen = strftime(buff, sizeof(buff), cc, stm);
L0161 luaL_addlstring(&b, buff, reslen);
L0162 }
L0163 }
L0164 luaL_pushresult(&b);
L0165 }
L0166 return 1;
L0167 }
L0168
L0169
L0170 static int os_time (lua_State *L) {
L0171 time_t t;
L0172 if (lua_isnoneornil(L, 1))
L0173 t = time(NULL);
L0174 else {
L0175 struct tm ts;
L0176 luaL_checktype(L, 1, LUA_TTABLE);
L0177 lua_settop(L, 1);
L0178 ts.tm_sec = getfield(L, "sec", 0);
L0179 ts.tm_min = getfield(L, "min", 0);
L0180 ts.tm_hour = getfield(L, "hour", 12);
L0181 ts.tm_mday = getfield(L, "day", -1);
L0182 ts.tm_mon = getfield(L, "month", -1) - 1;
L0183 ts.tm_year = getfield(L, "year", -1) - 1900;
L0184 ts.tm_isdst = getboolfield(L, "isdst");
L0185 t = mktime(&ts);
L0186 }
L0187 if (t == (time_t)(-1))
L0188 lua_pushnil(L);
L0189 else
L0190 lua_pushnumber(L, (lua_Number)t);
L0191 return 1;
L0192 }
L0193
L0194
L0195 static int os_difftime (lua_State *L) {
L0196 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
L0197 (time_t)(luaL_optnumber(L, 2, 0))));
L0198 return 1;
L0199 }
L0200
L0201
L0202
L0203
L0204 static int os_setlocale (lua_State *L) {
L0205 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
L0206 LC_NUMERIC, LC_TIME};
L0207 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
L0208 "numeric", "time", NULL};
L0209 const char *l = luaL_optstring(L, 1, NULL);
L0210 int op = luaL_checkoption(L, 2, "all", catnames);
L0211 lua_pushstring(L, setlocale(cat[op], l));
L0212 return 1;
L0213 }
L0214
L0215
L0216 static int os_exit (lua_State *L) {
L0217 exit(luaL_optint(L, 1, EXIT_SUCCESS));
L0218 }
L0219
L0220 static const luaL_Reg syslib[] = {
L0221 {"clock", os_clock},
L0222 {"date", os_date},
L0223 {"difftime", os_difftime},
L0224 {"execute", os_execute},
L0225 {"exit", os_exit},
L0226 {"getenv", os_getenv},
L0227 {"remove", os_remove},
L0228 {"rename", os_rename},
L0229 {"setlocale", os_setlocale},
L0230 {"time", os_time},
L0231 {"tmpname", os_tmpname},
L0232 {NULL, NULL}
L0233 };
L0234
L0235
L0236
L0237
L0238
L0239 LUALIB_API int luaopen_os (lua_State *L) {
L0240 luaL_register(L, LUA_OSLIBNAME, syslib);
L0241 return 1;
L0242 }
L0243
Generated by pretty.lua