Create a code to search all the names of the files in a directory, this code is recursive in case I find other directories in which I am reviewing them. The path inside the directory and file search works well for me, since I have tried them by printing what I read on the screen and finding everything and not repeating any file name. The problem I have is that it saves the names I find in a string array of characters, so when I find all the files I will have the array of strings in the main with all the names of the files. The problem is that in the end when you print that arrangement on the screen, not all the files are there but a few repeated files. The final goal of this program is not to print all the file names on the screen, but to save all the names of the files in the array. If you can help me, it has been days without finding the reason.
void findfiles(char *root,char *p[],int *tam){
DIR * dir;
struct dirent *entrada;
struct stat stt;
dir = opendir(root);
char *aux;
char nombre[BUFFER_TAM];
char buf[30];
if (dir == NULL) {
printf("hola4\n");
return;
}
while ((entrada = readdir(dir)) != NULL) {
if (strcmp(entrada->d_name,".")==0 || strcmp(entrada->d_name,"..")==0);
else {
if (entrada->d_type == DT_DIR){
strcpy(nombre,root);
strcat(nombre,"/");
strcat(nombre,entrada->d_name);
findfiles(nombre,p,tam);
}
else {
strcpy(nombre,root);
strcat(nombre,"/");
strcat(nombre,entrada->d_name);
p[*tam]=malloc(strlen(nombre)+1);
p[*tam]=nombre;
*tam = *tam +1;
}
}
}
}
void main(){
char *archivos[BUFFER_TAM];
char root[BUFFER_TAM]="/home/jesusmolina/Documentos";
int i=0,tam=0;
findfiles(root,archivos,&tam);
for (i;i<tam;i++)
printf("%s\n",archivos[i]);
}