0

this is my code, theoretically once taken from the terminal the directory where search and the file extension should look for these files contained in it and print their last modification and absolute path ... the problem is in the path printing.... 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..so I think I should pass the absolute path of the directory to solve the problem ... or suggest me how I should do it pls

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

}
}
  • 2
    Your question is unclear. Do you mean whether it is possible to pass a command line argument to a program as an argument to a function? In that case: yes. How: this is in every basic C tutorial ever. Please provide a description of the actual problem you're trying to solve and include the code you wrote so we understand where you are and what you need. – Cheatah May 16 '20 at 21:12
  • ok I try to explain myself better ... I have to pass to a function the command that returns the path of a directory, is it possible? – checcasio May 16 '20 at 21:35
  • You are not being any clearer. – Cheatah May 16 '20 at 21:48

1 Answers1

-2

Like Cheatah said,you need to provide us with more details,like,we don't even know what this function of yours does,but generally speaking in C an C++,you need to use the regular slash "/" as opposed to the back slash when specifying file paths,you also need to decide whether you're using an absolute path (Example:"D:/images/img.png") or a relative one,if the directory you're accessing is inside the project folder itself (Example:"images/img.png") as opposed to ("D:/Projects/c_project/images/img.png") with "D:/Projects/c_project" being your project folder and "images" being the directory you're trying to access.

So in short,your code with looks something like this:

function f(string s){

int main(){

f("D:/images/img.png");//Absolute path
f("images/img.png");//Relative path

}

function f(string s){...}

I hope I understood your question correctly,if not then you need to provide more details.

The Riser
  • 309
  • 2
  • 4
  • 12
  • This is C++, but the question is tagged C – Chris Loonam May 16 '20 at 21:59
  • Declaring and calling functions was the same in both c and c++ last time I checked. – The Riser May 16 '20 at 22:05
  • 2
    `function` and `string` would both lead to compilation errors in C. – Chris Loonam May 16 '20 at 22:06
  • Of course it would,because this is pseudo code,I'm assuming OP knows enough to translate it into C,since in C functions are supposed to have a type like "int f" or "string f" (or "void f" if it doesn't have a return type),also you're supposed to call the header file,that's why 'string' gave you an error. – The Riser May 16 '20 at 22:10
  • 1
    The `` header doesn't exist in C; that's part of the C++ standard library. ``, which is a C header, has no such `string` type declared in it. Also, assuming the OP knows not to copy/paste the code you provide is a bit naive, as many people on this site are absolute beginners and may not know as much as you do, especially when it comes to translating between two languages. – Chris Loonam May 16 '20 at 22:12
  • Ok my bad,it's ,it's been a while since I worked with C,but when it comes to me assuming OP's knowledge,that's frankly their fault,since they barely provided any details about what they're doing,I'm assuming if they're trying to make a function return a value,that they know how to declare and call one. – The Riser May 16 '20 at 22:17