Printing all files within a directory and its subdirectories
<p>My goal is to print all files within a directory and its subdirectories. If the path is a file, I print out the path. However, if the path is a directory, then I recursively call <code>pathInfo(pathnm)</code>, where <code>pathnm</code> is the path. My reasoning for this is that eventually it will reach the last directory, in which case it will print all of the files in that directory. As it progresses towards the last directory, it will successively print out the files it encounters. Eventually, all of the files in a given path directory and its subdirectories will be printed.</p>
<p>The problem is that at runtime, the program fails to print file names. Instead, it prints continuous lines of rubbish, such as <code>/Users/User1//././././././././././././</code>. This occurs until the program exits with the error <code>progname: Too many open files</code>.</p>
<p>How can I determine what I am doing incorrectly, and how I can fix it so that the program behaves in the way I have described? I am new to programming.</p>
<p><strong>pathInfo function</strong></p>
<pre><code>#include &quot;cfind.h&quot;

void getInfo(char *pathnm, char *argv[]) 
{
    DIR *dirStream; // pointer to a directory stream
    struct dirent *dp; // pointer to a dirent structure
    char *dirContent = NULL; // the contents of the directory
    dirStream = opendir(pathnm); // assign to dirStream the address of pathnm
    if (dirStream == NULL)
    {
        perror(argv[0]);
        exit(EXIT_FAILURE);
    }
    while ((dp = readdir(dirStream)) != NULL) // while readdir() has not reached the end of the directory stream
    {
        struct stat statInfo; // variable to contain information about the path
        asprintf(&amp;dirContent, &quot;%s/%s&quot;, pathnm, dp-&gt;d_name); // writes the content of the directory to dirContent // asprintf() dynamically allocates memory
        fprintf(stdout, &quot;%s\n&quot;, dirContent);
        if (stat(dirContent, &amp;statInfo) != 0) // if getting file or directory information failed
        {
            perror(pathnm);
            exit(EXIT_FAILURE);
        }
        else if (aflag == true) // if the option -a was given
        {
            if (S_ISDIR(statInfo.st_mode)) // if the path is a directory, recursively call getInfo()  
            {
                getInfo(dirContent, &amp;argv[0]);
            }
            else if(S_ISREG(statInfo.st_mode)) // if the path is a file, print all contents
            {
                fprintf(stdout, &quot;%s\n&quot;, dirContent);
            }
            else continue;
        }
        free(dirContent);
    }
    closedir(dirStream);
}
</code></pre>