Polo_2 0 Newbie Poster

this is my code that takes from the terminal directory and extension of the file (s) to search ... the problem is in the path .... print for example

/home/ciccio/prova.txt .... instead of ...... / home / ciccio / dir / subdir / prova.txt

This is because I only pass the file name to the "realpath" function contained in ricor1 (), which starts from the working directory and resolves the past path .... can anyone help me solve it?

#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 * percorso;

////////////
char * scrivi(const char * a, char * b)
{

char *targetdir = malloc(2048);
strcpy(targetdir,a);
strcat(targetdir,"/");
strcat(targetdir,b);

//printf("%s \n", targetdir); 
return targetdir;
} 
//////////////

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);
}

while((fdata = readdir (fh))!=NULL){

    // se รจ una directory richiama ricorsivamente

if(strcmp(fdata->d_name,".")==0)
{
  continue;
}
if(strcmp(fdata->d_name,"..") ==0)
{
continue;
}
if(fdata->d_type==DT_DIR)
{
 { percorso=scrivi(nomedirectory,fdata->d_name);
   ricor1(estensione,percorso);
 }
}

//
//

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]);

}
}