-1
#include <dirent.h> 
#include <stdio.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int program(char* file) {
  DIR *d;
  struct dirent *dir;
  d = opendir(file);
  if (d) {
     while ((dir = readdir(d)) != NULL) {
       if(dir->d_type == DT_DIR) {
         if(opendir(dir->d_name)!=NULL) {
          printf("Open successful!  ");
         }
         printf("Is a dir! ");
       }

       printf("%s\n", dir->d_name);
     }

     closedir(d);
  }

 return(0);

}

Lets say i access a directory in a c program like this and i find that one of its entries is also a directory? How do i open this directory entry? I don't think my code is doing it correctly

savram
  • 500
  • 4
  • 18
Leedle
  • 31
  • 1
  • 2
  • The name is "recursion". – Eugene Sh. Oct 25 '17 at 14:59
  • And as a side notice: For portable programs you cannot rely on the contents of `d_type`; it might contain `DT_UNKNOWN` for some filesystems. In this case you need to call `stat()` to find out if it is a directory (see also `man 3 readdir`). – Ctx Oct 25 '17 at 15:02
  • @xing would i have to strcat the name to the path and open that? – Leedle Oct 25 '17 at 15:14
  • Another aside: good practice to declare `const char* file` (rather than non-const) unless you intend to change it. – cdarke Oct 25 '17 at 15:18
  • You can `sprintf` another path name so you don't lose the name of the present directory - there may be more than one directory in the present one. – Weather Vane Oct 25 '17 at 15:19
  • You're leaking directory streams each time the nested `opendir()` succeeds. You must capture the return and `closedir()` it if it is not null. – Jonathan Leffler Oct 25 '17 at 16:27
  • 1
    You should also note [`stat()` error ENOENT with `readdir()`](http://stackoverflow.com/questions/5125919/stat-error-no-such-file-or-directory-when-file-name-is-returned-by-readdir) and also review the use of POSIX function [`nftw()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nftw.html). – Jonathan Leffler Oct 25 '17 at 17:02
  • missing POSIX or Linux flag – Basile Starynkevitch Oct 25 '17 at 17:03

1 Answers1

1

This Code calls function recursively to list the files and directories.

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    listdir(".", 0);       //Call the function for the current path
}

/* Function to list files and directories
 * @parameter Name : Name is the path
 * @parameter indent: indentation for printing the files and directories 
                      in the hierarchical structure
 */
void listdir(const char *name, int indent)
{
    DIR *dir;
    struct dirent *entry;

    /* Base Case, return if the current file is not directory */
    if (!(dir = opendir(name)))
        return;

    /*Loop for all the files in the current directory */
    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];

            /* If the path is . (current path) or .. (parent of the current 
              directory) then continue (do not call  lisdir()), 
              we don't want to stuck in infinite situation!
            */
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            printf("%*s[%s]\n", indent, "", entry->d_name);

            /* Call the function again for the current path
               Add 2 to indentation, that is to leave 2 more spaces in the 
               hierarchical structure 
            */
            listdir(path, indent + 2);
        } else {
            printf("%*s- %s\n", indent, "", entry->d_name);
        }
    }
    closedir(dir);
}
Rathan Naik
  • 993
  • 12
  • 25