tmueller@193
|
1 |
|
tmueller@193
|
2 |
/*
|
tmueller@251
|
3 |
** tek.lib.posix - tek POSIX library
|
tmueller@193
|
4 |
** Written by Timm S. Mueller <tmueller at neoscientists.org>
|
tmueller@193
|
5 |
** See copyright notice in COPYRIGHT
|
tmueller@193
|
6 |
*/
|
tmueller@0
|
7 |
|
tmueller@251
|
8 |
|
tmueller@0
|
9 |
#include <stdlib.h>
|
tmueller@0
|
10 |
#include <string.h>
|
tmueller@0
|
11 |
#include <sys/types.h>
|
tmueller@0
|
12 |
#include <sys/stat.h>
|
tmueller@0
|
13 |
#include <unistd.h>
|
tmueller@0
|
14 |
#include <dirent.h>
|
tmueller@0
|
15 |
#include <errno.h>
|
tmueller@172
|
16 |
#include <math.h>
|
tmueller@172
|
17 |
#include <time.h>
|
tmueller@0
|
18 |
#include <lua.h>
|
tmueller@0
|
19 |
#include <lualib.h>
|
tmueller@0
|
20 |
#include <lauxlib.h>
|
tmueller@0
|
21 |
|
tmueller@0
|
22 |
|
tmueller@0
|
23 |
#define DIRCLASSNAME "dir*"
|
tmueller@0
|
24 |
|
tmueller@0
|
25 |
|
tmueller@0
|
26 |
static void
|
tmueller@0
|
27 |
setfield(lua_State *L, const char *attr, const char *key, lua_Integer n)
|
tmueller@0
|
28 |
{
|
tmueller@0
|
29 |
if (attr == NULL || strcmp(attr, key) == 0)
|
tmueller@0
|
30 |
lua_pushinteger(L, n);
|
tmueller@0
|
31 |
if (attr == NULL)
|
tmueller@0
|
32 |
lua_setfield(L, -2, key);
|
tmueller@0
|
33 |
}
|
tmueller@0
|
34 |
|
tmueller@18
|
35 |
|
tmueller@0
|
36 |
static int
|
tmueller@0
|
37 |
setstat(lua_State *L, int result, struct stat *s, const char *attr)
|
tmueller@0
|
38 |
{
|
tmueller@0
|
39 |
if (result != 0)
|
tmueller@0
|
40 |
{
|
tmueller@0
|
41 |
lua_pushnil(L);
|
tmueller@0
|
42 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
43 |
return 2;
|
tmueller@0
|
44 |
}
|
tmueller@0
|
45 |
|
tmueller@0
|
46 |
if (attr == NULL)
|
tmueller@0
|
47 |
lua_newtable(L);
|
tmueller@198
|
48 |
|
tmueller@0
|
49 |
setfield(L, attr, "dev", s->st_dev);
|
tmueller@0
|
50 |
setfield(L, attr, "ino", s->st_ino);
|
tmueller@198
|
51 |
|
tmueller@0
|
52 |
if (attr == NULL || strcmp(attr, "mode") == 0)
|
tmueller@0
|
53 |
{
|
tmueller@0
|
54 |
if (S_ISREG(s->st_mode))
|
tmueller@0
|
55 |
lua_pushstring(L, "file");
|
tmueller@0
|
56 |
else if (S_ISDIR(s->st_mode))
|
tmueller@0
|
57 |
lua_pushstring(L, "directory");
|
tmueller@0
|
58 |
else if (S_ISLNK(s->st_mode))
|
tmueller@0
|
59 |
lua_pushstring(L, "link");
|
tmueller@0
|
60 |
else if (S_ISSOCK(s->st_mode))
|
tmueller@0
|
61 |
lua_pushstring(L, "socket");
|
tmueller@0
|
62 |
else if (S_ISFIFO(s->st_mode))
|
tmueller@0
|
63 |
lua_pushstring(L, "named pipe");
|
tmueller@0
|
64 |
else if (S_ISCHR(s->st_mode))
|
tmueller@0
|
65 |
lua_pushstring(L, "char device");
|
tmueller@0
|
66 |
else if (S_ISBLK(s->st_mode))
|
tmueller@0
|
67 |
lua_pushstring(L, "block device");
|
tmueller@0
|
68 |
else
|
tmueller@0
|
69 |
lua_pushstring(L, "other");
|
tmueller@0
|
70 |
if (attr == NULL)
|
tmueller@0
|
71 |
lua_setfield(L, -2, "mode");
|
tmueller@0
|
72 |
}
|
tmueller@198
|
73 |
|
tmueller@0
|
74 |
setfield(L, attr, "nlink", s->st_nlink);
|
tmueller@0
|
75 |
setfield(L, attr, "uid", s->st_uid);
|
tmueller@0
|
76 |
setfield(L, attr, "gid", s->st_gid);
|
tmueller@0
|
77 |
setfield(L, attr, "rdev", s->st_rdev);
|
tmueller@0
|
78 |
setfield(L, attr, "access", s->st_atime);
|
tmueller@251
|
79 |
setfield(L, attr, "modification", s->st_mtime);
|
tmueller@0
|
80 |
setfield(L, attr, "change", s->st_ctime);
|
tmueller@165
|
81 |
setfield(L, attr, "size", s->st_size);
|
tmueller@0
|
82 |
setfield(L, attr, "blocks", s->st_blocks);
|
tmueller@0
|
83 |
setfield(L, attr, "blksize", s->st_blksize);
|
tmueller@198
|
84 |
|
tmueller@0
|
85 |
return 1;
|
tmueller@0
|
86 |
}
|
tmueller@0
|
87 |
|
tmueller@0
|
88 |
|
tmueller@198
|
89 |
static int
|
tmueller@0
|
90 |
posix_stat(lua_State *L)
|
tmueller@0
|
91 |
{
|
tmueller@0
|
92 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
93 |
const char *attr = luaL_optstring(L, 2, NULL);
|
tmueller@0
|
94 |
struct stat s;
|
tmueller@0
|
95 |
return setstat(L, stat(path, &s), &s, attr);
|
tmueller@198
|
96 |
}
|
tmueller@0
|
97 |
|
tmueller@0
|
98 |
|
tmueller@198
|
99 |
static int
|
tmueller@0
|
100 |
posix_lstat(lua_State *L)
|
tmueller@0
|
101 |
{
|
tmueller@0
|
102 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
103 |
const char *attr = luaL_optstring(L, 2, NULL);
|
tmueller@0
|
104 |
struct stat s;
|
tmueller@0
|
105 |
return setstat(L, lstat(path, &s), &s, attr);
|
tmueller@0
|
106 |
}
|
tmueller@0
|
107 |
|
tmueller@0
|
108 |
|
tmueller@198
|
109 |
static int
|
tmueller@0
|
110 |
posix_opendir(lua_State *L)
|
tmueller@0
|
111 |
{
|
tmueller@0
|
112 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
113 |
DIR **pdir = lua_newuserdata(L, sizeof(void *));
|
tmueller@251
|
114 |
/* s: udata */
|
tmueller@0
|
115 |
*pdir = opendir(path);
|
tmueller@0
|
116 |
if (*pdir == NULL)
|
tmueller@0
|
117 |
{
|
tmueller@0
|
118 |
lua_pushnil(L);
|
tmueller@0
|
119 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
120 |
return 2;
|
tmueller@0
|
121 |
}
|
tmueller@251
|
122 |
lua_getfield(L, LUA_REGISTRYINDEX, DIRCLASSNAME);
|
tmueller@251
|
123 |
/* s: udata, meta */
|
tmueller@251
|
124 |
lua_setmetatable(L, -2);
|
tmueller@251
|
125 |
/* s: udata */
|
tmueller@0
|
126 |
return 1;
|
tmueller@0
|
127 |
}
|
tmueller@0
|
128 |
|
tmueller@0
|
129 |
|
tmueller@0
|
130 |
static DIR **
|
tmueller@0
|
131 |
getinstptr(lua_State *L, int narg, const char *classname)
|
tmueller@0
|
132 |
{
|
tmueller@0
|
133 |
DIR **pinst = luaL_checkudata(L, narg, classname);
|
tmueller@0
|
134 |
if (*pinst) return pinst;
|
tmueller@0
|
135 |
luaL_argerror(L, narg, "Closed handle");
|
tmueller@0
|
136 |
return NULL;
|
tmueller@0
|
137 |
}
|
tmueller@0
|
138 |
|
tmueller@0
|
139 |
|
tmueller@198
|
140 |
static int
|
tmueller@0
|
141 |
posix_closedir(lua_State *L)
|
tmueller@0
|
142 |
{
|
tmueller@0
|
143 |
DIR **pdir = getinstptr(L, 1, DIRCLASSNAME);
|
tmueller@0
|
144 |
closedir(*pdir);
|
tmueller@0
|
145 |
*pdir = NULL;
|
tmueller@0
|
146 |
return 0;
|
tmueller@0
|
147 |
}
|
tmueller@0
|
148 |
|
tmueller@0
|
149 |
|
tmueller@198
|
150 |
static int
|
tmueller@0
|
151 |
posix_readdir(lua_State *L)
|
tmueller@0
|
152 |
{
|
tmueller@0
|
153 |
DIR *dir = *getinstptr(L, 1, DIRCLASSNAME);
|
tmueller@0
|
154 |
struct dirent *de = readdir(dir);
|
tmueller@0
|
155 |
if (de)
|
tmueller@0
|
156 |
lua_pushstring(L, de->d_name);
|
tmueller@0
|
157 |
else
|
tmueller@0
|
158 |
lua_pushnil(L);
|
tmueller@0
|
159 |
return 1;
|
tmueller@0
|
160 |
}
|
tmueller@0
|
161 |
|
tmueller@0
|
162 |
|
tmueller@198
|
163 |
static int
|
tmueller@0
|
164 |
posix_readlink(lua_State *L)
|
tmueller@0
|
165 |
{
|
tmueller@0
|
166 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
167 |
char buf[PATH_MAX + 1];
|
tmueller@0
|
168 |
ssize_t len = readlink(path, buf, sizeof(buf) - 1);
|
tmueller@0
|
169 |
if (len < 0)
|
tmueller@0
|
170 |
{
|
tmueller@0
|
171 |
lua_pushnil(L);
|
tmueller@0
|
172 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
173 |
return 2;
|
tmueller@0
|
174 |
}
|
tmueller@0
|
175 |
buf[len] = 0;
|
tmueller@0
|
176 |
lua_pushstring(L, buf);
|
tmueller@0
|
177 |
return 1;
|
tmueller@0
|
178 |
}
|
tmueller@0
|
179 |
|
tmueller@0
|
180 |
|
tmueller@198
|
181 |
static int
|
tmueller@0
|
182 |
posix_mkdir(lua_State *L)
|
tmueller@0
|
183 |
{
|
tmueller@0
|
184 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
185 |
if (mkdir(path, 0775) == 0)
|
tmueller@0
|
186 |
{
|
tmueller@0
|
187 |
lua_pushboolean(L, 1);
|
tmueller@0
|
188 |
return 1;
|
tmueller@0
|
189 |
}
|
tmueller@0
|
190 |
lua_pushnil(L);
|
tmueller@0
|
191 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
192 |
return 2;
|
tmueller@0
|
193 |
}
|
tmueller@0
|
194 |
|
tmueller@0
|
195 |
|
tmueller@198
|
196 |
static int
|
tmueller@0
|
197 |
posix_symlink(lua_State *L)
|
tmueller@0
|
198 |
{
|
tmueller@0
|
199 |
const char *oldpath = luaL_checkstring(L, 1);
|
tmueller@0
|
200 |
const char *newpath = luaL_checkstring(L, 2);
|
tmueller@0
|
201 |
if (symlink(oldpath, newpath) == 0)
|
tmueller@0
|
202 |
{
|
tmueller@0
|
203 |
lua_pushboolean(L, 1);
|
tmueller@0
|
204 |
return 1;
|
tmueller@0
|
205 |
}
|
tmueller@0
|
206 |
lua_pushnil(L);
|
tmueller@0
|
207 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
208 |
return 2;
|
tmueller@0
|
209 |
}
|
tmueller@0
|
210 |
|
tmueller@0
|
211 |
|
tmueller@0
|
212 |
static int
|
tmueller@0
|
213 |
resolvepath(const char *src, char *dest)
|
tmueller@0
|
214 |
{
|
tmueller@0
|
215 |
int len = strlen(src);
|
tmueller@0
|
216 |
const char *sp = src + len;
|
tmueller@0
|
217 |
char *dp = dest;
|
tmueller@0
|
218 |
int dc = 0, slc = 0, eac = 0, wc = 0;
|
tmueller@0
|
219 |
int i, c;
|
tmueller@198
|
220 |
|
tmueller@198
|
221 |
while (len--)
|
tmueller@0
|
222 |
{
|
tmueller@0
|
223 |
c = *(--sp);
|
tmueller@0
|
224 |
switch (c)
|
tmueller@0
|
225 |
{
|
tmueller@0
|
226 |
case '/':
|
tmueller@0
|
227 |
if (dc == 2)
|
tmueller@0
|
228 |
eac++;
|
tmueller@0
|
229 |
dc = 0;
|
tmueller@0
|
230 |
slc = 1;
|
tmueller@0
|
231 |
wc = 0;
|
tmueller@0
|
232 |
break;
|
tmueller@0
|
233 |
|
tmueller@0
|
234 |
case '.':
|
tmueller@0
|
235 |
if (slc)
|
tmueller@0
|
236 |
{
|
tmueller@0
|
237 |
dc++;
|
tmueller@0
|
238 |
break;
|
tmueller@0
|
239 |
}
|
tmueller@0
|
240 |
/* fallthru: */
|
tmueller@0
|
241 |
|
tmueller@0
|
242 |
default:
|
tmueller@0
|
243 |
if (wc)
|
tmueller@0
|
244 |
break;
|
tmueller@198
|
245 |
|
tmueller@0
|
246 |
if (slc)
|
tmueller@0
|
247 |
{
|
tmueller@0
|
248 |
slc = 0;
|
tmueller@198
|
249 |
|
tmueller@0
|
250 |
if (eac > 0)
|
tmueller@0
|
251 |
{
|
tmueller@0
|
252 |
/* resolve one eatcount */
|
tmueller@0
|
253 |
eac--;
|
tmueller@0
|
254 |
/* now wait for next path part */
|
tmueller@0
|
255 |
wc = 1;
|
tmueller@0
|
256 |
break;
|
tmueller@0
|
257 |
}
|
tmueller@198
|
258 |
|
tmueller@0
|
259 |
*dp++ = '/';
|
tmueller@0
|
260 |
}
|
tmueller@198
|
261 |
|
tmueller@0
|
262 |
while (dc == 2 || dc == 1)
|
tmueller@0
|
263 |
{
|
tmueller@0
|
264 |
*dp++ = '.';
|
tmueller@0
|
265 |
dc--;
|
tmueller@0
|
266 |
}
|
tmueller@0
|
267 |
dc = 0;
|
tmueller@198
|
268 |
|
tmueller@0
|
269 |
*dp++ = c;
|
tmueller@0
|
270 |
break;
|
tmueller@0
|
271 |
}
|
tmueller@0
|
272 |
}
|
tmueller@198
|
273 |
|
tmueller@0
|
274 |
/* unresolved eatcount */
|
tmueller@0
|
275 |
if (eac)
|
tmueller@0
|
276 |
return 0;
|
tmueller@198
|
277 |
|
tmueller@0
|
278 |
/* resolve remaining slash */
|
tmueller@0
|
279 |
if (slc)
|
tmueller@0
|
280 |
*dp++ = '/';
|
tmueller@198
|
281 |
|
tmueller@0
|
282 |
*dp = 0;
|
tmueller@198
|
283 |
|
tmueller@0
|
284 |
len = dp - dest;
|
tmueller@0
|
285 |
for (i = 0; i < len / 2; ++i)
|
tmueller@0
|
286 |
{
|
tmueller@0
|
287 |
char t = dest[i];
|
tmueller@198
|
288 |
dest[i] = dest[len - i - 1];
|
tmueller@0
|
289 |
dest[len - i - 1] = t;
|
tmueller@0
|
290 |
}
|
tmueller@0
|
291 |
|
tmueller@0
|
292 |
return 1;
|
tmueller@0
|
293 |
}
|
tmueller@0
|
294 |
|
tmueller@0
|
295 |
|
tmueller@198
|
296 |
static int
|
tmueller@0
|
297 |
posix_abspath(lua_State *L)
|
tmueller@0
|
298 |
{
|
tmueller@0
|
299 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@0
|
300 |
char *pwd = NULL;
|
tmueller@0
|
301 |
size_t pwdsize = 16;
|
tmueller@198
|
302 |
size_t len1, len2;
|
tmueller@198
|
303 |
char *srcpath, *dstpath;
|
tmueller@198
|
304 |
|
tmueller@0
|
305 |
for (;;)
|
tmueller@0
|
306 |
{
|
tmueller@198
|
307 |
if (path[0] == '/')
|
tmueller@0
|
308 |
{
|
tmueller@198
|
309 |
pwd = malloc(2);
|
tmueller@198
|
310 |
if (pwd == NULL)
|
tmueller@198
|
311 |
break;
|
tmueller@198
|
312 |
pwd[0] = '/';
|
tmueller@198
|
313 |
pwd[1] = 0;
|
tmueller@198
|
314 |
}
|
tmueller@198
|
315 |
else
|
tmueller@198
|
316 |
{
|
tmueller@198
|
317 |
char *newpwd = realloc(pwd, pwdsize);
|
tmueller@198
|
318 |
if (newpwd == NULL)
|
tmueller@198
|
319 |
break;
|
tmueller@198
|
320 |
pwd = newpwd;
|
tmueller@198
|
321 |
|
tmueller@198
|
322 |
if (!getcwd(pwd, pwdsize))
|
tmueller@0
|
323 |
{
|
tmueller@198
|
324 |
if (errno == ERANGE)
|
tmueller@0
|
325 |
{
|
tmueller@198
|
326 |
pwdsize <<= 1;
|
tmueller@198
|
327 |
continue;
|
tmueller@0
|
328 |
}
|
tmueller@0
|
329 |
lua_pushnil(L);
|
tmueller@198
|
330 |
lua_pushstring(L, strerror(errno));
|
tmueller@0
|
331 |
return 2;
|
tmueller@0
|
332 |
}
|
tmueller@198
|
333 |
}
|
tmueller@198
|
334 |
|
tmueller@198
|
335 |
len1 = strlen(pwd);
|
tmueller@198
|
336 |
len2 = strlen(path);
|
tmueller@198
|
337 |
srcpath = malloc(len1 + 1 + len2 + 1);
|
tmueller@198
|
338 |
dstpath = malloc(len1 + 1 + len2 + 1);
|
tmueller@198
|
339 |
|
tmueller@198
|
340 |
if (srcpath && dstpath)
|
tmueller@198
|
341 |
{
|
tmueller@198
|
342 |
int res;
|
tmueller@198
|
343 |
|
tmueller@198
|
344 |
strcpy(srcpath, pwd);
|
tmueller@198
|
345 |
free(pwd);
|
tmueller@198
|
346 |
srcpath[len1] = '/';
|
tmueller@198
|
347 |
strcpy(srcpath + len1 + 1, path);
|
tmueller@198
|
348 |
res = resolvepath(srcpath, dstpath);
|
tmueller@0
|
349 |
free(srcpath);
|
tmueller@198
|
350 |
|
tmueller@198
|
351 |
if (res)
|
tmueller@198
|
352 |
{
|
tmueller@198
|
353 |
lua_pushstring(L, dstpath);
|
tmueller@198
|
354 |
free(dstpath);
|
tmueller@198
|
355 |
return 1;
|
tmueller@198
|
356 |
}
|
tmueller@198
|
357 |
|
tmueller@0
|
358 |
free(dstpath);
|
tmueller@198
|
359 |
lua_pushnil(L);
|
tmueller@198
|
360 |
lua_pushstring(L, "Not a valid path");
|
tmueller@198
|
361 |
return 2;
|
tmueller@0
|
362 |
}
|
tmueller@198
|
363 |
|
tmueller@198
|
364 |
free(srcpath);
|
tmueller@198
|
365 |
free(dstpath);
|
tmueller@198
|
366 |
break;
|
tmueller@0
|
367 |
}
|
tmueller@198
|
368 |
|
tmueller@0
|
369 |
free(pwd);
|
tmueller@0
|
370 |
luaL_error(L, "Out of memory");
|
tmueller@0
|
371 |
return 0;
|
tmueller@0
|
372 |
}
|
tmueller@0
|
373 |
|
tmueller@0
|
374 |
|
tmueller@198
|
375 |
static int
|
tmueller@172
|
376 |
posix_nanosleep(lua_State *L)
|
tmueller@172
|
377 |
{
|
tmueller@172
|
378 |
struct timespec req;
|
tmueller@172
|
379 |
lua_Number d = luaL_checknumber(L, 1);
|
tmueller@198
|
380 |
|
tmueller@172
|
381 |
req.tv_sec = (time_t) d;
|
tmueller@172
|
382 |
req.tv_nsec = (long) ((d - req.tv_sec) * 1000000000);
|
tmueller@198
|
383 |
|
tmueller@172
|
384 |
if (nanosleep(&req, NULL) == 0)
|
tmueller@172
|
385 |
{
|
tmueller@172
|
386 |
lua_pushboolean(L, 1);
|
tmueller@172
|
387 |
return 1;
|
tmueller@172
|
388 |
}
|
tmueller@198
|
389 |
|
tmueller@172
|
390 |
lua_pushnil(L);
|
tmueller@172
|
391 |
lua_pushstring(L, strerror(errno));
|
tmueller@172
|
392 |
return 2;
|
tmueller@172
|
393 |
}
|
tmueller@172
|
394 |
|
tmueller@251
|
395 |
#if defined(__linux)
|
tmueller@251
|
396 |
#include <sys/statvfs.h>
|
tmueller@172
|
397 |
|
tmueller@251
|
398 |
static int
|
tmueller@251
|
399 |
posix_statvfs(lua_State *L)
|
tmueller@251
|
400 |
{
|
tmueller@251
|
401 |
struct statvfs buf;
|
tmueller@251
|
402 |
const char *path = luaL_checkstring(L, 1);
|
tmueller@251
|
403 |
if (statvfs(path, &buf) == 0)
|
tmueller@251
|
404 |
{
|
tmueller@251
|
405 |
lua_newtable(L);
|
tmueller@251
|
406 |
lua_pushinteger(L, buf.f_bsize);
|
tmueller@251
|
407 |
lua_setfield(L, -2, "bsize");
|
tmueller@251
|
408 |
lua_pushinteger(L, buf.f_blocks);
|
tmueller@251
|
409 |
lua_setfield(L, -2, "blocks");
|
tmueller@251
|
410 |
lua_pushinteger(L, buf.f_bfree);
|
tmueller@251
|
411 |
lua_setfield(L, -2, "bfree");
|
tmueller@251
|
412 |
lua_pushinteger(L, buf.f_fsid);
|
tmueller@251
|
413 |
lua_setfield(L, -2, "fsid");
|
tmueller@251
|
414 |
lua_pushinteger(L, buf.f_namemax);
|
tmueller@251
|
415 |
lua_setfield(L, -2, "namemax");
|
tmueller@251
|
416 |
return 1;
|
tmueller@251
|
417 |
}
|
tmueller@251
|
418 |
lua_pushnil(L);
|
tmueller@251
|
419 |
lua_pushstring(L, strerror(errno));
|
tmueller@251
|
420 |
return 2;
|
tmueller@251
|
421 |
}
|
tmueller@251
|
422 |
|
tmueller@251
|
423 |
#endif
|
tmueller@251
|
424 |
|
tmueller@251
|
425 |
static const luaL_Reg libfuncs[] =
|
tmueller@0
|
426 |
{
|
tmueller@0
|
427 |
{ "stat", posix_stat },
|
tmueller@0
|
428 |
{ "lstat", posix_lstat },
|
tmueller@0
|
429 |
{ "opendir", posix_opendir },
|
tmueller@0
|
430 |
{ "closedir", posix_closedir },
|
tmueller@0
|
431 |
{ "readdir", posix_readdir },
|
tmueller@0
|
432 |
{ "mkdir", posix_mkdir },
|
tmueller@0
|
433 |
{ "readlink", posix_readlink },
|
tmueller@0
|
434 |
{ "symlink", posix_symlink },
|
tmueller@0
|
435 |
{ "abspath", posix_abspath },
|
tmueller@172
|
436 |
{ "nanosleep", posix_nanosleep },
|
tmueller@251
|
437 |
#if defined(__linux)
|
tmueller@251
|
438 |
{ "statvfs", posix_statvfs },
|
tmueller@251
|
439 |
#endif
|
tmueller@0
|
440 |
{ NULL, NULL }
|
tmueller@0
|
441 |
};
|
tmueller@0
|
442 |
|
tmueller@0
|
443 |
|
tmueller@251
|
444 |
static const luaL_Reg dirmethods[] =
|
tmueller@0
|
445 |
{
|
tmueller@0
|
446 |
{"read", posix_readdir },
|
tmueller@0
|
447 |
{"close", posix_closedir },
|
tmueller@0
|
448 |
{"__gc", posix_closedir },
|
tmueller@0
|
449 |
{NULL, NULL}
|
tmueller@0
|
450 |
};
|
tmueller@0
|
451 |
|
tmueller@251
|
452 |
int luaopen_tek_lib_posix(lua_State *L)
|
tmueller@0
|
453 |
{
|
tmueller@251
|
454 |
luaL_register(L, "tek.lib.posix", libfuncs);
|
tmueller@251
|
455 |
/* s: lib */
|
tmueller@251
|
456 |
luaL_newmetatable(L, DIRCLASSNAME);
|
tmueller@251
|
457 |
/* s: lib, meta */
|
tmueller@251
|
458 |
lua_pushvalue(L, -1);
|
tmueller@251
|
459 |
/* s: lib, meta, meta */
|
tmueller@251
|
460 |
lua_setfield(L, -2, "__index");
|
tmueller@251
|
461 |
/* s: lib, meta */
|
tmueller@251
|
462 |
luaL_register(L, NULL, dirmethods);
|
tmueller@251
|
463 |
/* s: lib, meta */
|
tmueller@198
|
464 |
lua_pop(L, 2);
|
tmueller@251
|
465 |
/* s: */
|
tmueller@0
|
466 |
return 0;
|
tmueller@0
|
467 |
}
|