2 ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
3 ** Standard Operating System library
4 ** See Copyright Notice in lua.h
23 static int os_pushresult (lua_State
*L
, int i
, const char *filename
) {
24 int en
= errno
; /* calls to Lua API may change this value */
26 lua_pushboolean(L
, 1);
31 lua_pushfstring(L
, "%s: %s", filename
, strerror(en
));
32 lua_pushinteger(L
, en
);
38 static int os_execute (lua_State
*L
) {
39 lua_pushinteger(L
, system(luaL_optstring(L
, 1, NULL
)));
44 static int os_remove (lua_State
*L
) {
45 const char *filename
= luaL_checkstring(L
, 1);
46 return os_pushresult(L
, remove(filename
) == 0, filename
);
50 static int os_rename (lua_State
*L
) {
51 const char *fromname
= luaL_checkstring(L
, 1);
52 const char *toname
= luaL_checkstring(L
, 2);
53 return os_pushresult(L
, rename(fromname
, toname
) == 0, fromname
);
57 static int os_tmpname (lua_State
*L
) {
58 char buff
[LUA_TMPNAMBUFSIZE
];
60 lua_tmpnam(buff
, err
);
62 return luaL_error(L
, "unable to generate a unique filename");
63 lua_pushstring(L
, buff
);
68 static int os_getenv (lua_State
*L
) {
70 lua_pushstring(L
, NULL
); /* if NULL push nil */
72 lua_pushstring(L
, getenv(luaL_checkstring(L
, 1))); /* if NULL push nil */
78 static int os_clock (lua_State
*L
) {
79 lua_pushnumber(L
, ((lua_Number
)clock())/(lua_Number
)CLOCKS_PER_SEC
);
85 ** {======================================================
86 ** Time/Date operations
87 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
88 ** wday=%w+1, yday=%j, isdst=? }
89 ** =======================================================
92 static void setfield (lua_State
*L
, const char *key
, int value
) {
93 lua_pushinteger(L
, value
);
94 lua_setfield(L
, -2, key
);
97 static void setboolfield (lua_State
*L
, const char *key
, int value
) {
98 if (value
< 0) /* undefined? */
99 return; /* does not set field */
100 lua_pushboolean(L
, value
);
101 lua_setfield(L
, -2, key
);
104 static int getboolfield (lua_State
*L
, const char *key
) {
106 lua_getfield(L
, -1, key
);
107 res
= lua_isnil(L
, -1) ? -1 : lua_toboolean(L
, -1);
113 static int getfield (lua_State
*L
, const char *key
, int d
) {
115 lua_getfield(L
, -1, key
);
116 if (lua_isnumber(L
, -1))
117 res
= (int)lua_tointeger(L
, -1);
120 return luaL_error(L
, "field " LUA_QS
" missing in date table", key
);
128 static int os_date (lua_State
*L
) {
129 const char *s
= luaL_optstring(L
, 1, "%c");
130 time_t t
= luaL_opt(L
, (time_t)luaL_checknumber
, 2, time(NULL
));
132 if (*s
== '!') { /* UTC? */
138 if (stm
== NULL
) /* invalid date? */
140 else if (strcmp(s
, "*t") == 0) {
141 lua_createtable(L
, 0, 9); /* 9 = number of fields */
142 setfield(L
, "sec", stm
->tm_sec
);
143 setfield(L
, "min", stm
->tm_min
);
144 setfield(L
, "hour", stm
->tm_hour
);
145 setfield(L
, "day", stm
->tm_mday
);
146 setfield(L
, "month", stm
->tm_mon
+1);
147 setfield(L
, "year", stm
->tm_year
+1900);
148 setfield(L
, "wday", stm
->tm_wday
+1);
149 setfield(L
, "yday", stm
->tm_yday
+1);
150 setboolfield(L
, "isdst", stm
->tm_isdst
);
155 cc
[0] = '%'; cc
[2] = '\0';
156 luaL_buffinit(L
, &b
);
158 if (*s
!= '%' || *(s
+ 1) == '\0') /* no conversion specifier? */
159 luaL_addchar(&b
, *s
);
162 char buff
[200]; /* should be big enough for any conversion result */
164 reslen
= strftime(buff
, sizeof(buff
), cc
, stm
);
165 luaL_addlstring(&b
, buff
, reslen
);
174 static int os_time (lua_State
*L
) {
176 if (lua_isnoneornil(L
, 1)) /* called without args? */
177 t
= time(NULL
); /* get current time */
180 luaL_checktype(L
, 1, LUA_TTABLE
);
181 lua_settop(L
, 1); /* make sure table is at the top */
182 ts
.tm_sec
= getfield(L
, "sec", 0);
183 ts
.tm_min
= getfield(L
, "min", 0);
184 ts
.tm_hour
= getfield(L
, "hour", 12);
185 ts
.tm_mday
= getfield(L
, "day", -1);
186 ts
.tm_mon
= getfield(L
, "month", -1) - 1;
187 ts
.tm_year
= getfield(L
, "year", -1) - 1900;
188 ts
.tm_isdst
= getboolfield(L
, "isdst");
191 if (t
== (time_t)(-1))
194 lua_pushnumber(L
, (lua_Number
)t
);
199 #if !defined LUA_NUMBER_INTEGRAL
200 static int os_difftime (lua_State
*L
) {
201 lua_pushnumber(L
, difftime((time_t)(luaL_checknumber(L
, 1)),
202 (time_t)(luaL_optnumber(L
, 2, 0))));
207 /* }====================================================== */
210 static int os_setlocale (lua_State
*L
) {
211 static const int cat
[] = {LC_ALL
, LC_COLLATE
, LC_CTYPE
, LC_MONETARY
,
212 LC_NUMERIC
, LC_TIME
};
213 static const char *const catnames
[] = {"all", "collate", "ctype", "monetary",
214 "numeric", "time", NULL
};
215 const char *l
= luaL_optstring(L
, 1, NULL
);
216 int op
= luaL_checkoption(L
, 2, "all", catnames
);
217 lua_pushstring(L
, setlocale(cat
[op
], l
));
222 static int os_exit (lua_State
*L
) {
223 exit(luaL_optint(L
, 1, EXIT_SUCCESS
));
226 static const luaL_Reg syslib
[] = {
229 #if !defined LUA_NUMBER_INTEGRAL
230 {"difftime", os_difftime
},
232 {"execute", os_execute
},
234 {"getenv", os_getenv
},
235 {"remove", os_remove
},
236 {"rename", os_rename
},
237 {"setlocale", os_setlocale
},
239 {"tmpname", os_tmpname
},
243 /* }====================================================== */
247 LUALIB_API
int luaopen_os (lua_State
*L
) {
248 luaL_register(L
, LUA_OSLIBNAME
, syslib
);