1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
int tree(const char* const rep) {
DIR *fd=opendir(rep);
if (fd == NULL) {
fprintf(stderr, "Erreur tree [%s] - %s\n", rep, strerror(errno));
return -1;
}
int cpt;
struct dirent *d;
for (cpt=0; (d=readdir(fd)) != NULL; cpt++) {
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) continue;
char nom[FILENAME_MAX + 1];
sprintf(nom, "%s/%s", strcmp(rep, "/") != 0 ?rep :"", d->d_name);
printf("%s\n", nom);
if (d->d_type == DT_DIR) tree(nom);
}
closedir(fd);
return cpt;
}
int main(int argc, char *argv[]) {
tree(argv[1]);
} |
Partager