hi I'm doing a program that recursively searches for the file with a certain extension in the subtree rooted in the directory possibly passed as an argument. For each file found, print the absolute path of the directory in which it was found and the data of the last modification. The program receives as input the extension string and the name of the directory where to search (if it is not inserted it searches in the current directory). This is what I managed to do .. My problem is that the files inside the directory are examined but not the ones in the subdirectory too ..... i thinik i should examine the directory and if there are others inside it call ricor1 with passing the subdirectory and extension...someone can help me pls?
Code:
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<stdio.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<sys/stat.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<errno.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<stdlib.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<dirent.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<stdarg.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<limits.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<string.h>
[URL = "tg://search_hashtag?hashtag=include"]
#include[/URL]<time.h>
void ricor1(const char estensione[], const char nomedirectory[])
{
struct stat attr;
char dbuf[PATH_MAX + 1];
DIR *fh; //puntatore ad una struttura DIR
struct dirent *fdata;
struct stat buf;
if ((fh = opendir(nomedirectory)) == NULL) {
perror("ERRORE 1");
exit(errno);
}
puts("\nElenco directory:\n");
while ((fdata = readdir(fh)) != NULL) {
if (strstr(fdata->d_name, estensione)) {
realpath(fdata->d_name, dbuf);
printf("[%s]", dbuf);
stat(nomedirectory, &attr);
printf("%s\n", ctime(&attr.st_mtime));
}
}
}
void ricor2(const char estensione[])
{
struct stat attr;
char dbuf[PATH_MAX + 1];
DIR *fh; //puntatore ad una struttura DIR
struct dirent *fdata;
struct stat buf;
if ((fh = opendir("./")) == NULL) {
perror("ERRORE 1");
exit(errno);
}
while ((fdata = readdir(fh)) != NULL) {
if (strstr(fdata->d_name, estensione)) {
realpath(fdata->d_name, dbuf);
printf("[%s]", dbuf);
stat("./", &attr);
printf("%s\n", ctime(&attr.st_mtime));
}
}
}
int main(int argc, char *argv[])
{
if (argc == 3) {
printf("Controllo esistenza directory.. \n");
DIR *dir = opendir(argv[2]);
if (dir) {
ricor1(argv[1], argv[2]);
} else if (ENOENT == errno) {
printf("La directory passata non esiste");
} else {
printf("altro errore");
}
}
else if (argc == 2) {
ricor2(argv[1]);
}
}