hi guys this script recursively searches for files with a certain extension and possibly in a directory passed by the terminal ....(In case no directory has been passed, search from the current one) I believe it works correctly but I wanted to ask you if you noticed any flaws and report them to me or just tell me that everything is ok ...thanksss
#include<stdio.h>
#include<sys/stat.h>
#include<errno.h>
#include<stdlib.h>
#include<dirent.h>
#include<stdarg.h>
#include<limits.h>
#include<string.h>
#include<time.h>
char * scrivi(const char * a, char * B) {
char *targetdir = malloc(2048);
strcpy(targetdir,a);
strcat(targetdir,"/");
strcat(targetdir,B);
return targetdir;
}
void ricor1(const char estensione[],const char nomedirectory[]){
struct stat attr;
char dbuf[PATH_MAX+1];
DIR * fh ;
struct dirent *fdata;
struct stat buf;
if((fh=opendir(nomedirectory))==NULL){
perror("ERRORE 1");
exit(errno);
}
while((fdata = readdir (fh))!=NULL){
if(strcmp(fdata->d_name,".")==0){
continue;
}
if(strcmp(fdata->d_name,"..")==0){
continue;
}
char *percorso;
percorso = scrivi(nomedirectory,fdata->d_name);
if (fdata->d_type==DT_DIR){
ricor1(estensione,percorso);
}
if (fdata->d_type == DT_REG && strstr(fdata->d_name, estensione)) {
realpath(percorso, dbuf);
printf("[%s]", dbuf);
stat(percorso, &attr);
printf("%s\n", ctime(&attr.st_mtime));
}
free(percorso);
}
closedir(fh);
}
int main(int argc, char *argv[]) {
if(argc==3){
printf("Checking existence directory.. \n");
DIR* dir=opendir(argv[2]);
if(dir){
closedir(dir);
ricor1(argv[1],argv[2]);
}
else if (ENOENT==errno){
printf("Directory doesn't exist\n");
}
else{
printf("error");
}
}
else if(argc==2){
ricor1(argv[1],"./");
}
}