Skip to content

Commit c658c9a

Browse files
committed
fix iterator and cleanups
1 parent e27f694 commit c658c9a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ldir.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ int eli_read_dir(lua_State *L)
103103
#else
104104
struct dirent *entry;
105105
#endif
106-
107106
const char *path = luaL_checkstring(L, 1);
108107
const int asDirEntries = lua_toboolean(L, 2);
109108
lua_newtable(L);
@@ -203,8 +202,8 @@ static int dir_iter(lua_State *L)
203202
struct dirent *entry;
204203
#endif
205204
dir_data *d = (dir_data *)luaL_checkudata(L, 1, DIR_METATABLE);
206-
luaL_argcheck(L, d->closed == 0, 1, "closed " DIR_METATABLE);
207-
const int asDirEntries = asDirEntries == -1 ? lua_toboolean(L, 2) : d->asDirEntries;
205+
luaL_argcheck(L, d->closed == 0, 1, "can not iterate over closed " DIR_METATABLE);
206+
const int asDirEntries = d->asDirEntries == -1 ? lua_toboolean(L, 2) : d->asDirEntries;
208207
#ifdef _WIN32
209208
if (d->hFile == 0L)
210209
{ /* first entry */
@@ -271,7 +270,6 @@ static int dir_iter(lua_State *L)
271270

272271
if (entry != NULL)
273272
{
274-
275273
if (asDirEntries)
276274
{
277275
struct dir_entry_data *result = lua_newuserdata(L, sizeof(struct dir_entry_data));
@@ -349,29 +347,31 @@ int eli_iter_dir(lua_State *L)
349347
if (d->dir == NULL)
350348
return luaL_error(L, "cannot open %s: %s", path, strerror(errno));
351349
#endif
352-
return 2;
350+
lua_pushnil(L);
351+
lua_pushvalue(L, -2);
352+
return 4;
353353
}
354354

355355
/*
356356
** Closes directory iterators
357357
*/
358358
static int lclosedir(lua_State *L)
359359
{
360-
dir_data *d = (dir_data *)lua_touserdata(L, 1);
360+
dir_data *d = (dir_data *)luaL_checkudata(L, 1, DIR_METATABLE);
361361
#ifdef _WIN32
362362
if (!d->closed && d->hFile)
363363
{
364364
_findclose(d->hFile);
365+
free(d->path);
365366
}
366367
#else
367368
if (!d->closed && d->dir)
368369
{
369370
closedir(d->dir);
371+
free(d->path);
370372
}
371373
#endif
372374
d->closed = 1;
373-
374-
free(d->path);
375375
return 0;
376376
}
377377

0 commit comments

Comments
 (0)