Skip to content

Instantly share code, notes, and snippets.

@jrozner
Created January 1, 2011 21:38
Show Gist options
  • Save jrozner/762026 to your computer and use it in GitHub Desktop.
Save jrozner/762026 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
void find_dir(char *path, char *dir);
int main(int argc, char *argv[])
{
char dir[100];
if (argc != 2)
{
getcwd(dir, sizeof(dir));
} else {
strncpy(dir, argv[1], sizeof(dir) - 1);
}
find_dir(dir, "");
return 0;
}
void find_dir(char *path, char *dir)
{
char full_path[150];
struct dirent *entry;
DIR *current_dir;
snprintf(full_path, sizeof(full_path) - 1, "%s%s/", path, dir);
current_dir = opendir(full_path);
while ((entry = readdir(current_dir)) != NULL)
{
if ((entry->d_type == 4) && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
{
printf("%s%s\n", full_path, entry->d_name);
find_dir(full_path, entry->d_name);
}
}
closedir(current_dir);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment