Thread: the subtree rooted in the directory,C language,linux

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    9

    the subtree rooted in the directory,C language,linux

    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]);
    
      }
    }
    Last edited by Salem; 05-15-2020 at 11:10 PM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,167
    Accessing a directory and all the files within it - Cprogramming.com
    Scroll down a bit.

    Or if you're not against using things which already exist.
    ftw(3): file tree walk - Linux man page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2019, 12:31 PM
  2. Display Multi Rooted tree
    By ricky9 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2011, 11:35 AM
  3. know the only file name in a directory(Linux)
    By kapil1089thekin in forum C Programming
    Replies: 5
    Last Post: 10-15-2010, 08:56 AM
  4. directory operations, linux
    By MK27 in forum C Programming
    Replies: 10
    Last Post: 08-01-2008, 07:15 PM
  5. Is there a way to get a linux directory size in c++?
    By meili100 in forum Linux Programming
    Replies: 5
    Last Post: 04-19-2008, 02:01 AM

Tags for this Thread