diff options
author | pixel <pixel> | 2004-11-28 01:25:17 +0000 |
---|---|---|
committer | pixel <pixel> | 2004-11-28 01:25:17 +0000 |
commit | 338315a055e9ba12f6e1111995235524cbab6f1a (patch) | |
tree | 2bede1b779084633f9bfae75d0ee54d682d92a48 /lib/lua | |
parent | c93f5466134388afa9e4b1a51ce8840d86494779 (diff) |
Lua's dir iterator fix.
Diffstat (limited to 'lib/lua')
-rw-r--r-- | lib/lua/src/LuaLib/ldirlib.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/lua/src/LuaLib/ldirlib.c b/lib/lua/src/LuaLib/ldirlib.c index 9bf2090..8997eb6 100644 --- a/lib/lua/src/LuaLib/ldirlib.c +++ b/lib/lua/src/LuaLib/ldirlib.c @@ -15,9 +15,15 @@ static int dir_iter (lua_State *L); static int l_dir (lua_State *L) { const char *path = luaL_checkstring(L, 1); + char * d_path, * p; + DIR **d; /* create a userdatum to store a DIR address */ - DIR **d = (DIR **)lua_newuserdata(L, sizeof(DIR *)); + p = lua_newuserdata(L, sizeof(DIR *) + strlen(path) + 1); + d = (DIR **) p; + d_path = p + sizeof(DIR *); + + strcpy(d_path, path); /* set its metatable */ luaL_getmetatable(L, "LuaBook.dir"); @@ -37,7 +43,10 @@ static int l_dir (lua_State *L) { } static int dir_iter (lua_State *L) { - DIR *d = *(DIR **)lua_touserdata(L, lua_upvalueindex(1)); + char * p = (char *) lua_touserdata(L, lua_upvalueindex(1)); + DIR * d = *(DIR **) p; + char * path = p + sizeof(DIR *); + char * fullpath; struct dirent *entry; #ifdef _WIN32 struct _stat file_stat; @@ -49,11 +58,14 @@ static int dir_iter (lua_State *L) { lua_pushstring(L, "name"); lua_pushstring(L, entry->d_name); lua_settable(L, -3); + fullpath = malloc(strlen(path) + strlen(entry->d_name) + 2); + sprintf(fullpath, "%s/%s", path, entry->d_name); #ifdef _WIN32 - _stat(entry->d_name, &file_stat); + _stat(fullpath, &file_stat); #else - stat(entry->d_name, &file_stat); + stat(fullpath, &file_stat); #endif + free(fullpath); lua_pushstring(L, "type"); lua_pushnumber(L, (S_IFREG & file_stat.st_mode) ? 2 : ((S_IFDIR & file_stat.st_mode) ? 1 : 0)); lua_settable(L, -3); |