Skip to content

Instantly share code, notes, and snippets.

@apotheon
Created May 23, 2014 20:52
Show Gist options
  • Select an option

  • Save apotheon/dc9fb3d24820e1776ad4 to your computer and use it in GitHub Desktop.

Select an option

Save apotheon/dc9fb3d24820e1776ad4 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void listdir(const char *name, int level)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s[%s]\n", level*2, "", entry->d_name);
listdir(path, level + 1);
}
else
printf("%*s- %s\n", level*2, "", entry->d_name);
} while (entry = readdir(dir));
closedir(dir);
}
int main ( int argc, char *argv[] )
{
listdir(argv[1], 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment