1 /*
2 ** $Id$
3 ** Standard Operating System library
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #include <errno.h>
9 #include <locale.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 
14 #define loslib_c
15 #define LUA_LIB
16 
17 #include "lua.h"
18 
19 #include "lauxlib.h"
20 #include "lualib.h"
21 
22 
os_pushresult(lua_State * L,int i,const char * filename)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 */
25   if (i) {
26     lua_pushboolean(L, 1);
27     return 1;
28   }
29   else {
30     lua_pushnil(L);
31     lua_pushfstring(L, "%s: %s", filename, strerror(en));
32     lua_pushinteger(L, en);
33     return 3;
34   }
35 }
36 
37 
os_execute(lua_State * L)38 static int os_execute (lua_State *L) {
39   lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
40   return 1;
41 }
42 
43 
os_remove(lua_State * L)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);
47 }
48 
49 
os_rename(lua_State * L)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);
54 }
55 
56 
os_tmpname(lua_State * L)57 static int os_tmpname (lua_State *L) {
58   char buff[LUA_TMPNAMBUFSIZE];
59   int err;
60   lua_tmpnam(buff, err);
61   if (err)
62     return luaL_error(L, "unable to generate a unique filename");
63   lua_pushstring(L, buff);
64   return 1;
65 }
66 
67 
os_getenv(lua_State * L)68 static int os_getenv (lua_State *L) {
69   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
70   return 1;
71 }
72 
73 
os_clock(lua_State * L)74 static int os_clock (lua_State *L) {
75   lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
76   return 1;
77 }
78 
79 
80 /*
81 ** {======================================================
82 ** Time/Date operations
83 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
84 **   wday=%w+1, yday=%j, isdst=? }
85 ** =======================================================
86 */
87 
setfield(lua_State * L,const char * key,int value)88 static void setfield (lua_State *L, const char *key, int value) {
89   lua_pushinteger(L, value);
90   lua_setfield(L, -2, key);
91 }
92 
setboolfield(lua_State * L,const char * key,int value)93 static void setboolfield (lua_State *L, const char *key, int value) {
94   if (value < 0)  /* undefined? */
95     return;  /* does not set field */
96   lua_pushboolean(L, value);
97   lua_setfield(L, -2, key);
98 }
99 
getboolfield(lua_State * L,const char * key)100 static int getboolfield (lua_State *L, const char *key) {
101   int res;
102   lua_getfield(L, -1, key);
103   res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
104   lua_pop(L, 1);
105   return res;
106 }
107 
108 
getfield(lua_State * L,const char * key,int d)109 static int getfield (lua_State *L, const char *key, int d) {
110   int res;
111   lua_getfield(L, -1, key);
112   if (lua_isnumber(L, -1))
113     res = (int)lua_tointeger(L, -1);
114   else {
115     if (d < 0)
116       return luaL_error(L, "field " LUA_QS " missing in date table", key);
117     res = d;
118   }
119   lua_pop(L, 1);
120   return res;
121 }
122 
123 
os_date(lua_State * L)124 static int os_date (lua_State *L) {
125   const char *s = luaL_optstring(L, 1, "%c");
126   time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
127   struct tm *stm;
128   if (*s == '!') {  /* UTC? */
129     stm = gmtime(&t);
130     s++;  /* skip `!' */
131   }
132   else
133     stm = localtime(&t);
134   if (stm == NULL)  /* invalid date? */
135     lua_pushnil(L);
136   else if (strcmp(s, "*t") == 0) {
137     lua_createtable(L, 0, 9);  /* 9 = number of fields */
138     setfield(L, "sec", stm->tm_sec);
139     setfield(L, "min", stm->tm_min);
140     setfield(L, "hour", stm->tm_hour);
141     setfield(L, "day", stm->tm_mday);
142     setfield(L, "month", stm->tm_mon+1);
143     setfield(L, "year", stm->tm_year+1900);
144     setfield(L, "wday", stm->tm_wday+1);
145     setfield(L, "yday", stm->tm_yday+1);
146     setboolfield(L, "isdst", stm->tm_isdst);
147   }
148   else {
149     char b[256];
150     if (strftime(b, sizeof(b), s, stm))
151       lua_pushstring(L, b);
152     else
153       return luaL_error(L, LUA_QL("date") " format too long");
154   }
155   return 1;
156 }
157 
158 
os_time(lua_State * L)159 static int os_time (lua_State *L) {
160   time_t t;
161   if (lua_isnoneornil(L, 1))  /* called without args? */
162     t = time(NULL);  /* get current time */
163   else {
164     struct tm ts;
165     luaL_checktype(L, 1, LUA_TTABLE);
166     lua_settop(L, 1);  /* make sure table is at the top */
167     ts.tm_sec = getfield(L, "sec", 0);
168     ts.tm_min = getfield(L, "min", 0);
169     ts.tm_hour = getfield(L, "hour", 12);
170     ts.tm_mday = getfield(L, "day", -1);
171     ts.tm_mon = getfield(L, "month", -1) - 1;
172     ts.tm_year = getfield(L, "year", -1) - 1900;
173     ts.tm_isdst = getboolfield(L, "isdst");
174     t = mktime(&ts);
175   }
176   if (t == (time_t)(-1))
177     lua_pushnil(L);
178   else
179     lua_pushnumber(L, (lua_Number)t);
180   return 1;
181 }
182 
183 
os_difftime(lua_State * L)184 static int os_difftime (lua_State *L) {
185   lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
186                              (time_t)(luaL_optnumber(L, 2, 0))));
187   return 1;
188 }
189 
190 /* }====================================================== */
191 
192 
os_setlocale(lua_State * L)193 static int os_setlocale (lua_State *L) {
194   static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
195                       LC_NUMERIC, LC_TIME};
196   static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
197      "numeric", "time", NULL};
198   const char *l = luaL_optstring(L, 1, NULL);
199   int op = luaL_checkoption(L, 2, "all", catnames);
200   lua_pushstring(L, setlocale(cat[op], l));
201   return 1;
202 }
203 
204 
os_exit(lua_State * L)205 static int os_exit (lua_State *L) {
206   exit(luaL_optint(L, 1, EXIT_SUCCESS));
207   return 0;  /* to avoid warnings */
208 }
209 
210 static const luaL_Reg syslib[] = {
211   {"clock",     os_clock},
212   {"date",      os_date},
213   {"difftime",  os_difftime},
214   {"execute",   os_execute},
215   {"exit",      os_exit},
216   {"getenv",    os_getenv},
217   {"remove",    os_remove},
218   {"rename",    os_rename},
219   {"setlocale", os_setlocale},
220   {"time",      os_time},
221   {"tmpname",   os_tmpname},
222   {NULL, NULL}
223 };
224 
225 /* }====================================================== */
226 
227 
228 
luaopen_os(lua_State * L)229 LUALIB_API int luaopen_os (lua_State *L) {
230   luaL_register(L, LUA_OSLIBNAME, syslib);
231   return 1;
232 }
233 
234