Skip to content

Instantly share code, notes, and snippets.

@kkrolikowski
Last active July 20, 2016 10:37
Show Gist options
  • Save kkrolikowski/ec239bc856dd71b3bc1095600556868e to your computer and use it in GitHub Desktop.
Save kkrolikowski/ec239bc856dd71b3bc1095600556868e to your computer and use it in GitHub Desktop.
Recursive directory listing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
void printdir(char * name);
int main(int argc, char *argv[]) {
printdir(argv[1]);
return 0;
}
void printdir(char * name) {
DIR * d;
struct dirent * entry;
char buff[256];
memset(buff, '\0', 256);
strcpy(buff, name);
d = opendir(name);
while((entry = readdir(d)) != NULL) {
if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
if(entry->d_type == 4) {
strcat(buff, entry->d_name);
strcat(buff, "/");
printf("type: %u, name: %s\n", entry->d_type, buff);
printdir(buff);
}
else {
strcat(buff, entry->d_name);
printf("type: %u, name: %s\n", entry->d_type, buff);
}
memset(buff, '\0', 256);
strcpy(buff, name);
}
closedir(d);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment