1 /*
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
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 #if ((defined __MACH__) && (defined __APPLE__))
23   #include <TargetConditionals.h>
24   #if TARGET_OS_IPHONE
25     #define LUA_PLATFORM_IOS 1
26   #endif
27 #endif
28 
29 #ifdef LUA_PLATFORM_IOS
30   #include <spawn.h>
31   extern char **environ;
32 #endif
33 
os_pushresult(lua_State * L,int i,const char * filename)34 static int os_pushresult (lua_State *L, int i, const char *filename) {
35   int en = errno;  /* calls to Lua API may change this value */
36   if (i) {
37     lua_pushboolean(L, 1);
38     return 1;
39   }
40   else {
41     lua_pushnil(L);
42     lua_pushfstring(L, "%s: %s", filename, strerror(en));
43     lua_pushinteger(L, en);
44     return 3;
45   }
46 }
47 
48 
os_execute(lua_State * L)49 static int os_execute (lua_State *L) {
50   const char *command = luaL_optstring(L, 1, NULL);
51   int status;
52 #ifndef LUA_PLATFORM_IOS
53   status = system(command);
54 #else
55   // untested, may require adjustments depending on `command`
56   pid_t pid;
57   char *argv[] = { (char *)command, NULL };
58   posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
59   waitpid(pid, &status, 0);
60 #endif
61   lua_pushinteger(L, status);
62   return 1;
63 }
64 
65 
os_remove(lua_State * L)66 static int os_remove (lua_State *L) {
67   const char *filename = luaL_checkstring(L, 1);
68   return os_pushresult(L, remove(filename) == 0, filename);
69 }
70 
71 
os_rename(lua_State * L)72 static int os_rename (lua_State *L) {
73   const char *fromname = luaL_checkstring(L, 1);
74   const char *toname = luaL_checkstring(L, 2);
75   return os_pushresult(L, rename(fromname, toname) == 0, fromname);
76 }
77 
78 
os_tmpname(lua_State * L)79 static int os_tmpname (lua_State *L) {
80   char buff[LUA_TMPNAMBUFSIZE];
81   int err;
82   lua_tmpnam(buff, err);
83   if (err)
84     return luaL_error(L, "unable to generate a unique filename");
85   lua_pushstring(L, buff);
86   return 1;
87 }
88 
89 
os_getenv(lua_State * L)90 static int os_getenv (lua_State *L) {
91   lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */
92   return 1;
93 }
94 
95 
os_clock(lua_State * L)96 static int os_clock (lua_State *L) {
97   lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
98   return 1;
99 }
100 
101 
102 /*
103 ** {======================================================
104 ** Time/Date operations
105 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
106 **   wday=%w+1, yday=%j, isdst=? }
107 ** =======================================================
108 */
109 
setfield(lua_State * L,const char * key,int value)110 static void setfield (lua_State *L, const char *key, int value) {
111   lua_pushinteger(L, value);
112   lua_setfield(L, -2, key);
113 }
114 
setboolfield(lua_State * L,const char * key,int value)115 static void setboolfield (lua_State *L, const char *key, int value) {
116   if (value < 0)  /* undefined? */
117     return;  /* does not set field */
118   lua_pushboolean(L, value);
119   lua_setfield(L, -2, key);
120 }
121 
getboolfield(lua_State * L,const char * key)122 static int getboolfield (lua_State *L, const char *key) {
123   int res;
124   lua_getfield(L, -1, key);
125   res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
126   lua_pop(L, 1);
127   return res;
128 }
129 
130 
getfield(lua_State * L,const char * key,int d)131 static int getfield (lua_State *L, const char *key, int d) {
132   int res;
133   lua_getfield(L, -1, key);
134   if (lua_isnumber(L, -1))
135     res = (int)lua_tointeger(L, -1);
136   else {
137     if (d < 0)
138       return luaL_error(L, "field " LUA_QS " missing in date table", key);
139     res = d;
140   }
141   lua_pop(L, 1);
142   return res;
143 }
144 
145 
os_date(lua_State * L)146 static int os_date (lua_State *L) {
147   const char *s = luaL_optstring(L, 1, "%c");
148   time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
149   struct tm *stm;
150   if (*s == '!') {  /* UTC? */
151     stm = gmtime(&t);
152     s++;  /* skip `!' */
153   }
154   else
155     stm = localtime(&t);
156   if (stm == NULL)  /* invalid date? */
157     lua_pushnil(L);
158   else if (strcmp(s, "*t") == 0) {
159     lua_createtable(L, 0, 9);  /* 9 = number of fields */
160     setfield(L, "sec", stm->tm_sec);
161     setfield(L, "min", stm->tm_min);
162     setfield(L, "hour", stm->tm_hour);
163     setfield(L, "day", stm->tm_mday);
164     setfield(L, "month", stm->tm_mon+1);
165     setfield(L, "year", stm->tm_year+1900);
166     setfield(L, "wday", stm->tm_wday+1);
167     setfield(L, "yday", stm->tm_yday+1);
168     setboolfield(L, "isdst", stm->tm_isdst);
169   }
170   else {
171     char cc[3];
172     luaL_Buffer b;
173     cc[0] = '%'; cc[2] = '\0';
174     luaL_buffinit(L, &b);
175     for (; *s; s++) {
176       if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */
177         luaL_addchar(&b, *s);
178       else {
179         size_t reslen;
180         char buff[200];  /* should be big enough for any conversion result */
181         cc[1] = *(++s);
182         reslen = strftime(buff, sizeof(buff), cc, stm);
183         luaL_addlstring(&b, buff, reslen);
184       }
185     }
186     luaL_pushresult(&b);
187   }
188   return 1;
189 }
190 
191 
os_time(lua_State * L)192 static int os_time (lua_State *L) {
193   time_t t;
194   if (lua_isnoneornil(L, 1))  /* called without args? */
195     t = time(NULL);  /* get current time */
196   else {
197     struct tm ts;
198     luaL_checktype(L, 1, LUA_TTABLE);
199     lua_settop(L, 1);  /* make sure table is at the top */
200     ts.tm_sec = getfield(L, "sec", 0);
201     ts.tm_min = getfield(L, "min", 0);
202     ts.tm_hour = getfield(L, "hour", 12);
203     ts.tm_mday = getfield(L, "day", -1);
204     ts.tm_mon = getfield(L, "month", -1) - 1;
205     ts.tm_year = getfield(L, "year", -1) - 1900;
206     ts.tm_isdst = getboolfield(L, "isdst");
207     t = mktime(&ts);
208   }
209   if (t == (time_t)(-1))
210     lua_pushnil(L);
211   else
212     lua_pushnumber(L, (lua_Number)t);
213   return 1;
214 }
215 
216 
os_difftime(lua_State * L)217 static int os_difftime (lua_State *L) {
218   lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
219                              (time_t)(luaL_optnumber(L, 2, 0))));
220   return 1;
221 }
222 
223 /* }====================================================== */
224 
225 
os_setlocale(lua_State * L)226 static int os_setlocale (lua_State *L) {
227   static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
228                       LC_NUMERIC, LC_TIME};
229   static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
230      "numeric", "time", NULL};
231   const char *l = luaL_optstring(L, 1, NULL);
232   int op = luaL_checkoption(L, 2, "all", catnames);
233   lua_pushstring(L, setlocale(cat[op], l));
234   return 1;
235 }
236 
237 
os_exit(lua_State * L)238 static int os_exit (lua_State *L) {
239   exit(luaL_optint(L, 1, EXIT_SUCCESS));
240 }
241 
242 static const luaL_Reg syslib[] = {
243   {"clock",     os_clock},
244   {"date",      os_date},
245   {"difftime",  os_difftime},
246   {"execute",   os_execute},
247   {"exit",      os_exit},
248   {"getenv",    os_getenv},
249   {"remove",    os_remove},
250   {"rename",    os_rename},
251   {"setlocale", os_setlocale},
252   {"time",      os_time},
253   {"tmpname",   os_tmpname},
254   {NULL, NULL}
255 };
256 
257 /* }====================================================== */
258 
259 
260 
luaopen_os(lua_State * L)261 LUALIB_API int luaopen_os (lua_State *L) {
262   luaL_register(L, LUA_OSLIBNAME, syslib);
263   return 1;
264 }
265 
266