Here is the code I used to test it. It works on ordinary directories, but not those mounted under sshfs. My goal is to use these methods in https://github.com/jlettvin/Greased-Grep which is designed to allow global fuzzy searches for keywords which must be present and keywords which must be absent.
#include <iostream>
#include <string>
#include <functional>
#include <dirent.h>
using std::cout;
using std::endl;
using std::string;
using std::function;
bool neither (const char* path)
{
bool ret = (path != nullptr);
if (ret)
{
if (path[0] == '.')
{
if (path[1] == '\0') ret = false;
if (path[1] == '.' && path[2] == '\0') ret = false;
}
}
return ret;
}
void walk (const string &path, function<void (const string &)> talk)
{
if (auto dir = opendir (path.c_str ())) {
while (auto f = readdir (dir)) {
auto name = f->d_name;
auto type = f->d_type;
if (neither (name))
{
switch (type)
{
case DT_DIR: walk (path + name + "/", talk); break;
case DT_REG: talk (path + name ); break;
}
}
}
closedir(dir);
}
}
int main (int argc, char** argv)
{
walk ("./", [](const string &path) { cout << path << endl; });
return 0;
}