while loop and if not getting along with readdir() in C
<p>While trying to write a program that will search a directory and list out contents matching a command line argument, I've come across a problem I can't seem to figure out.</p>

<p>I've put an if statement within a while loop to check whether the strings match, but the issue is that I only get the last entry in the directory back. If I comment out the if statement, it prints the entire directory just fine, and it matches strings just fine, but it won't do both.</p>

<p>A friend suggested it has something to do with the stack but as it is printing after each read, I don't see why that should be.</p>

<pre><code>DIR *dirPos;
struct dirent * entry;
struct stat st;
char *pattern = argv[argc-1];

//----------------------
//a few error checks for command line and file opening
//----------------------

//Open directory
if ((dirPos = opendir(".")) == NULL){
    //error message if null
}

//Print entry
while ((entry = readdir(dirPos)) != NULL){
    if (!strcmp(entry-&gt;d_name, pattern)){
        stat(entry-&gt;d_name, &amp;st);
        printf("%s\t%d\n", entry-&gt;d_name, st.st_size);
    }
}
</code></pre>