aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luajit-2.0/src/lib_os.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-01-13 21:08:31 +1000
committerDavid Walter Seikel2014-01-13 21:08:31 +1000
commit637177eb1397ef1800027bccd50dbdc1af29a15b (patch)
tree3670a48303d05fceb8bf3ec4ee2901b72fe62d4d /libraries/luajit-2.0/src/lib_os.c
parentUpdate Irrlicht to 1.8.1. Include actual change markers this time. lol (diff)
downloadSledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.zip
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.gz
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.bz2
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.xz
Remove LuaJIT source, we can use packaged LuaJIT 2.0 release now.
Also some cleanups related to the other library removals.
Diffstat (limited to '')
-rw-r--r--libraries/luajit-2.0/src/lib_os.c256
1 files changed, 0 insertions, 256 deletions
diff --git a/libraries/luajit-2.0/src/lib_os.c b/libraries/luajit-2.0/src/lib_os.c
deleted file mode 100644
index 7292940..0000000
--- a/libraries/luajit-2.0/src/lib_os.c
+++ /dev/null
@@ -1,256 +0,0 @@
1/*
2** OS library.
3** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4**
5** Major portions taken verbatim or adapted from the Lua interpreter.
6** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7*/
8
9#include <errno.h>
10#include <locale.h>
11#include <time.h>
12
13#define lib_os_c
14#define LUA_LIB
15
16#include "lua.h"
17#include "lauxlib.h"
18#include "lualib.h"
19
20#include "lj_obj.h"
21#include "lj_err.h"
22#include "lj_lib.h"
23
24#if LJ_TARGET_POSIX
25#include <unistd.h>
26#else
27#include <stdio.h>
28#endif
29
30/* ------------------------------------------------------------------------ */
31
32#define LJLIB_MODULE_os
33
34static int os_pushresult(lua_State *L, int i, const char *filename)
35{
36 int en = errno; /* calls to Lua API may change this value */
37 if (i) {
38 setboolV(L->top-1, 1);
39 return 1;
40 } else {
41 setnilV(L->top-1);
42 lua_pushfstring(L, "%s: %s", filename, strerror(en));
43 lua_pushinteger(L, en);
44 return 3;
45 }
46}
47
48LJLIB_CF(os_execute)
49{
50 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
51 return 1;
52}
53
54LJLIB_CF(os_remove)
55{
56 const char *filename = luaL_checkstring(L, 1);
57 return os_pushresult(L, remove(filename) == 0, filename);
58}
59
60LJLIB_CF(os_rename)
61{
62 const char *fromname = luaL_checkstring(L, 1);
63 const char *toname = luaL_checkstring(L, 2);
64 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
65}
66
67LJLIB_CF(os_tmpname)
68{
69#if LJ_TARGET_POSIX
70 char buf[15+1];
71 int fp;
72 strcpy(buf, "/tmp/lua_XXXXXX");
73 fp = mkstemp(buf);
74 if (fp != -1)
75 close(fp);
76 else
77 lj_err_caller(L, LJ_ERR_OSUNIQF);
78#else
79 char buf[L_tmpnam];
80 if (tmpnam(buf) == NULL)
81 lj_err_caller(L, LJ_ERR_OSUNIQF);
82#endif
83 lua_pushstring(L, buf);
84 return 1;
85}
86
87LJLIB_CF(os_getenv)
88{
89 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
90 return 1;
91}
92
93LJLIB_CF(os_exit)
94{
95 int status;
96 if (L->base < L->top && tvisbool(L->base))
97 status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE;
98 else
99 status = lj_lib_optint(L, 1, EXIT_SUCCESS);
100 if (L->base+1 < L->top && tvistruecond(L->base+1))
101 lua_close(L);
102 exit(status);
103 return 0; /* Unreachable. */
104}
105
106LJLIB_CF(os_clock)
107{
108 setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC));
109 return 1;
110}
111
112/* ------------------------------------------------------------------------ */
113
114static void setfield(lua_State *L, const char *key, int value)
115{
116 lua_pushinteger(L, value);
117 lua_setfield(L, -2, key);
118}
119
120static void setboolfield(lua_State *L, const char *key, int value)
121{
122 if (value < 0) /* undefined? */
123 return; /* does not set field */
124 lua_pushboolean(L, value);
125 lua_setfield(L, -2, key);
126}
127
128static int getboolfield(lua_State *L, const char *key)
129{
130 int res;
131 lua_getfield(L, -1, key);
132 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
133 lua_pop(L, 1);
134 return res;
135}
136
137static int getfield(lua_State *L, const char *key, int d)
138{
139 int res;
140 lua_getfield(L, -1, key);
141 if (lua_isnumber(L, -1)) {
142 res = (int)lua_tointeger(L, -1);
143 } else {
144 if (d < 0)
145 lj_err_callerv(L, LJ_ERR_OSDATEF, key);
146 res = d;
147 }
148 lua_pop(L, 1);
149 return res;
150}
151
152LJLIB_CF(os_date)
153{
154 const char *s = luaL_optstring(L, 1, "%c");
155 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
156 struct tm *stm;
157 if (*s == '!') { /* UTC? */
158 stm = gmtime(&t);
159 s++; /* skip `!' */
160 } else {
161 stm = localtime(&t);
162 }
163 if (stm == NULL) { /* invalid date? */
164 setnilV(L->top-1);
165 } else if (strcmp(s, "*t") == 0) {
166 lua_createtable(L, 0, 9); /* 9 = number of fields */
167 setfield(L, "sec", stm->tm_sec);
168 setfield(L, "min", stm->tm_min);
169 setfield(L, "hour", stm->tm_hour);
170 setfield(L, "day", stm->tm_mday);
171 setfield(L, "month", stm->tm_mon+1);
172 setfield(L, "year", stm->tm_year+1900);
173 setfield(L, "wday", stm->tm_wday+1);
174 setfield(L, "yday", stm->tm_yday+1);
175 setboolfield(L, "isdst", stm->tm_isdst);
176 } else {
177 char cc[3];
178 luaL_Buffer b;
179 cc[0] = '%'; cc[2] = '\0';
180 luaL_buffinit(L, &b);
181 for (; *s; s++) {
182 if (*s != '%' || *(s + 1) == '\0') { /* no conversion specifier? */
183 luaL_addchar(&b, *s);
184 } else {
185 size_t reslen;
186 char buff[200]; /* should be big enough for any conversion result */
187 cc[1] = *(++s);
188 reslen = strftime(buff, sizeof(buff), cc, stm);
189 luaL_addlstring(&b, buff, reslen);
190 }
191 }
192 luaL_pushresult(&b);
193 }
194 return 1;
195}
196
197LJLIB_CF(os_time)
198{
199 time_t t;
200 if (lua_isnoneornil(L, 1)) { /* called without args? */
201 t = time(NULL); /* get current time */
202 } else {
203 struct tm ts;
204 luaL_checktype(L, 1, LUA_TTABLE);
205 lua_settop(L, 1); /* make sure table is at the top */
206 ts.tm_sec = getfield(L, "sec", 0);
207 ts.tm_min = getfield(L, "min", 0);
208 ts.tm_hour = getfield(L, "hour", 12);
209 ts.tm_mday = getfield(L, "day", -1);
210 ts.tm_mon = getfield(L, "month", -1) - 1;
211 ts.tm_year = getfield(L, "year", -1) - 1900;
212 ts.tm_isdst = getboolfield(L, "isdst");
213 t = mktime(&ts);
214 }
215 if (t == (time_t)(-1))
216 lua_pushnil(L);
217 else
218 lua_pushnumber(L, (lua_Number)t);
219 return 1;
220}
221
222LJLIB_CF(os_difftime)
223{
224 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
225 (time_t)(luaL_optnumber(L, 2, (lua_Number)0))));
226 return 1;
227}
228
229/* ------------------------------------------------------------------------ */
230
231LJLIB_CF(os_setlocale)
232{
233 GCstr *s = lj_lib_optstr(L, 1);
234 const char *str = s ? strdata(s) : NULL;
235 int opt = lj_lib_checkopt(L, 2, 6,
236 "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all");
237 if (opt == 0) opt = LC_CTYPE;
238 else if (opt == 1) opt = LC_NUMERIC;
239 else if (opt == 2) opt = LC_TIME;
240 else if (opt == 3) opt = LC_COLLATE;
241 else if (opt == 4) opt = LC_MONETARY;
242 else if (opt == 6) opt = LC_ALL;
243 lua_pushstring(L, setlocale(opt, str));
244 return 1;
245}
246
247/* ------------------------------------------------------------------------ */
248
249#include "lj_libdef.h"
250
251LUALIB_API int luaopen_os(lua_State *L)
252{
253 LJ_LIB_REG(L, LUA_OSLIBNAME, os);
254 return 1;
255}
256