From 0976864358ad2fa493a1085b4f882a55da4328e8 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 8 Jul 2020 21:01:09 -0700 Subject: readdir: skip . and .. entries * sysif.c (readdir_wrap): If d_name is "." or ".." loop around and get another directory entry. * txr.1: Documented. --- sysif.c | 33 +++++++++++++++++++-------------- txr.1 | 9 +++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/sysif.c b/sysif.c index 12509c85..4d1a8b6d 100644 --- a/sysif.c +++ b/sysif.c @@ -2267,23 +2267,28 @@ static val readdir_wrap(val dirobj, val dirent_in) struct dir *d = coerce(struct dir *, cobj_handle(self, dirobj, dir_s)); struct dirent *dent = readdir(d->dir); - if (dent == 0) { - return nil; - } else { - args_decl(args, ARGS_MIN); - val dirent = default_arg(dirent_in, make_struct(dirent_st, nil, args)); - slotset(dirent, name_s, - if3(d->path, - path_cat(d->path, string_utf8(dent->d_name)), - string_utf8(dent->d_name))); - slotset(dirent, ino_s, num(dent->d_ino)); + for (;;) { + if (dent == 0) { + return nil; + } else if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) { + dent = readdir(d->dir); + continue; + } else { + args_decl(args, ARGS_MIN); + val dirent = default_arg(dirent_in, make_struct(dirent_st, nil, args)); + slotset(dirent, name_s, + if3(d->path, + path_cat(d->path, string_utf8(dent->d_name)), + string_utf8(dent->d_name))); + slotset(dirent, ino_s, num(dent->d_ino)); #ifdef _DIRENT_HAVE_D_TYPE - slotset(dirent, type_s, num(dent->d_type)); + slotset(dirent, type_s, num(dent->d_type)); #else - if (dirent_in == dirent) - slotset(dirent, type_s, nil); + if (dirent_in == dirent) + slotset(dirent, type_s, nil); #endif - return dirent; + return dirent; + } } } diff --git a/txr.1 b/txr.1 index 8478934f..a86d5e6c 100644 --- a/txr.1 +++ b/txr.1 @@ -62867,6 +62867,15 @@ Otherwise, the next available directory entry is returned as a structure object of type .codn dirent . +The +.code readdir +function intenally skips and does not report the +.str . +(dot) +and +.str .. +(dotdot) directory entries. + If the .meta dirent-struct argument is specified, then it must be a -- cgit v1.2.3