/* parcours recursif d'un dir et affiche fichier ou rep ou lien utilisation de strcpy et strcat pour les chemins */ #include #include #include #include #include #include #include int parcours(char* rep){ DIR * repCourant=opendir(rep); // ptr sur obj dynam. répertoire courant if (repCourant==NULL){ printf("%s n'est pas un répertoire lisible\n",rep); return -1; } else{ char *chemCourant=(char *)malloc(1024); struct dirent *entreeRep; struct stat buf; /* pour le lstat */ while ((entreeRep=readdir(repCourant)) != NULL){ // pour chaque entrée chemCourant=strcpy(chemCourant, rep); chemCourant=strcat(chemCourant,"/"); chemCourant=strcat(chemCourant,entreeRep->d_name); // lstat pour ne pas etre embete par les liens if(0==lstat(chemCourant,&buf)){ if (S_ISDIR(buf.st_mode)){ if(strcmp(entreeRep->d_name, ".") && strcmp(entreeRep->d_name, "..")){ printf("dir : %s\n",chemCourant); parcours(chemCourant); } } else if(S_ISREG(buf.st_mode)) printf("fic : %s\n",chemCourant); else if(S_ISLNK(buf.st_mode)) printf("lien symb : %s\n",chemCourant); else printf("Tube ou bloc ou ... : %s\n",chemCourant); }//fin stat OK else{ printf("pb pour %s\n", chemCourant); } } free(chemCourant); closedir(repCourant); return 0; } } int main(int argc, char*argv[]){ if(argc<2){ printf("syntaxe : %s repertoire\n",argv[0]); return 0; } return parcours(argv[1]); }