//C list directory and files recursively
#include <dirent.h>
#include <fstream>
#include <string.h>
#include <unistd.h>
void Util::listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
{
return;
}
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR)
{
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
{
continue;
}
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
}
else
{
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}
int main(int args,char **argv)
{
try
{
listdir(argv[1],atoi(argv[2]));
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
}