chiark / gitweb /
basic: add readdir_no_dot and safe_glob functions
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 26 Apr 2017 03:44:34 +0000 (23:44 -0400)
committerSven Eden <yamakuzure@gmx.net>
Tue, 25 Jul 2017 07:46:52 +0000 (09:46 +0200)
safe_glob filters out "." and "..".

This converts all users of glob_extend() and glob_exists() to safe_glob.

src/basic/dirent-util.c
src/basic/dirent-util.h

index 6b9d26773ea78868a50fdb662bb6ba8ea559bd64..5bf58bcdc36d442022e1ef6a82965e06aecfcdf2 100644 (file)
@@ -75,3 +75,14 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
 
         return endswith(de->d_name, suffix);
 }
+
+struct dirent* readdir_no_dot(DIR *dirp) {
+        struct dirent* d;
+
+        for (;;) {
+                d = readdir(dirp);
+                if (d && dot_or_dot_dot(d->d_name))
+                        continue;
+                return d;
+        }
+}
index b91d04908f9a01545ed7f68e4f32fbc91323288d..18b9db9b28c6c3d80bd3a5d422149f5b4c3b6cee 100644 (file)
@@ -31,6 +31,8 @@ int dirent_ensure_type(DIR *d, struct dirent *de);
 bool dirent_is_file(const struct dirent *de) _pure_;
 bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
 
+struct dirent* readdir_no_dot(DIR *dirp);
+
 #define FOREACH_DIRENT(de, d, on_error)                                 \
         for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d))   \
                 if (!de) {                                              \