summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2019-06-23 10:07:49 -0400
committerTavian Barnes <tavianator@tavianator.com>2019-06-25 01:18:47 -0400
commitc8f7eca0eb6327e8b8ea066af55183a135818fe1 (patch)
treecffd779c202fe52cfe0b603372a33b77721214c9 /util.c
parent8d393a4e1a37a78bccb3928379693529f78ff20e (diff)
downloadbfs-c8f7eca0eb6327e8b8ea066af55183a135818fe1.tar.xz
util: Filter out . and .. in xreaddir()
Diffstat (limited to 'util.c')
-rw-r--r--util.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/util.c b/util.c
index 2a01af1..01fb1ae 100644
--- a/util.c
+++ b/util.c
@@ -41,12 +41,20 @@
#endif
int xreaddir(DIR *dir, struct dirent **de) {
- errno = 0;
- *de = readdir(dir);
- if (!*de && errno != 0) {
- return -1;
- } else {
- return 0;
+ while (true) {
+ errno = 0;
+ *de = readdir(dir);
+
+ if (*de) {
+ const char *name = (*de)->d_name;
+ if (name[0] != '.' || (name[1] != '\0' && (name[1] != '.' || name[2] != '\0'))) {
+ return 0;
+ }
+ } else if (errno != 0) {
+ return -1;
+ } else {
+ return 0;
+ }
}
}