[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lib.sys.listdir Re: C Function returning a table to LUA?
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Mon, 29 Sep 2003 11:39:12 -0300
>Look at lposlib.c you can read :
For Lua 5.0, you should use the version at
http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
Here is the code you need. It's simple and can be made simpler by inlining
storeindex.
--lhf
static void storeindex(lua_State *L, int i, const char *value)
{
lua_pushstring(L, value);
lua_rawseti(L, -2, i);
}
static int Pdir(lua_State *L) /** dir([path]) */
{
const char *path = luaL_optstring(L, 1, ".");
DIR *d = opendir(path);
if (d == NULL)
return pusherror(L, path);
else
{
int i;
struct dirent *entry;
lua_newtable(L);
for (i=1; (entry = readdir(d)) != NULL; i++)
storeindex(L, i, entry->d_name);
closedir(d);
return 1;
}
}