summaryrefslogtreecommitdiff
path: root/luxio.c
diff options
context:
space:
mode:
Diffstat (limited to 'luxio.c')
-rw-r--r--luxio.c50
1 files changed, 29 insertions, 21 deletions
diff --git a/luxio.c b/luxio.c
index 4ba1a9b..739a055 100644
--- a/luxio.c
+++ b/luxio.c
@@ -61,6 +61,11 @@ Not all systems will provide all the functions described here.
#include <syslog.h>
#include <iconv.h>
+#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 24)
+/* readdir is deprecated as of glibc 2.24, use readdir instead */
+#define LUXIO_USE_READDIR
+#endif
+
#ifdef HAVE_SENDFILE
# include <sys/sendfile.h>
#endif
@@ -1077,34 +1082,37 @@ static int
luxio_readdir(lua_State *L) /* 5.1.2 */
{
luxio_readdir_state *s = luaL_checkudata(L, 1, LUXIO_READDIR_METATABLE);
- int err;
- err = readdir_r(s->dirp, s->buf, &s->ent);
-
- if (err == 0 && s->ent != NULL) {
- lua_pushinteger(L, 0);
- lua_createtable(L, 0, 3);
- lua_pushinteger(L, s->ent->d_ino);
- lua_setfield(L, -2, "d_ino");
- lua_pushstring(L, s->ent->d_name);
- lua_setfield(L, -2, "d_name");
-#ifdef HAVE_D_TYPE
- lua_pushinteger(L, s->ent->d_type);
- lua_setfield(L, -2, "d_type");
+ errno = 0;
+#ifdef LUXIO_USE_READDIR
+ struct dirent *ent = readdir(s->dirp);
+#else
+ errno = readdir_r(s->dirp, s->buf, &s->ent);
+ struct dirent *ent = errno == 0 ? s->ent : NULL;
#endif
- return 2;
- }
- if (s->ent == NULL) {
- /* end of directory */
- lua_pushnil(L);
+ if (ent == NULL) {
+ if (errno != 0) {
+ lua_pushinteger(L, errno);
+ } else {
+ /* end of directory */
+ lua_pushnil(L);
+ }
return 1;
}
- lua_pushinteger(L, err);
-
- return 1;
+ lua_pushinteger(L, 0);
+ lua_createtable(L, 0, 3);
+ lua_pushinteger(L, ent->d_ino);
+ lua_setfield(L, -2, "d_ino");
+ lua_pushstring(L, ent->d_name);
+ lua_setfield(L, -2, "d_name");
+#ifdef HAVE_D_TYPE
+ lua_pushinteger(L, ent->d_type);
+ lua_setfield(L, -2, "d_type");
+#endif
+ return 2;
}
/*** Reset directory stream