What's wrong with my stat() function in C in linux
<p>I'm trying to list file infos(including directories etc) using stat()</p>

<p>It works fine when I give "."(current directory) as argv[1] (ex. $./a.out .)</p>

<p>but has an error when I give other directory such as "..", "/" etc.</p>

<p>stat function returns -1(fail) when after direntp points ".."(parent directory).</p>

<p>here's an example.</p>

<pre><code>$ ls -a ..
. .. sample temple

$ ./a.out ..
Success: .
Success: ..
Fail: sample
Stat Call Error.
Terminate.

$
</code></pre>

<p>So why does it fail when I give other path to argument of stat()?</p>

<p>here's my code below</p>

<pre><code>#include "stdio.h"
#include "dirent.h"
#include "sys/stat.h"
#include "unistd.h"
#include "stdlib.h"

int main(int argc, char const *argv[])
{
    DIR *dirp;
    struct dirent *direntp;
    struct stat statbuf;

    dirp = opendir(argv[1]);
    while((direntp = readdir(dirp)) != NULL)
    {
        if(direntp-&gt;d_ino == 0)
            continue;
        if(direntp-&gt;d_name[0] != '.'); /* doesn't matter, This isn't a mistake */
        {
            if(stat(direntp-&gt;d_name, &amp;statbuf) == -1)
            {
                printf("Fail: %s\n", direntp-&gt;d_name);
                printf("Stat Call Error\n");
                exit(-1);
            }
            printf("Success: %s\n", direntp-&gt;d_name);
        }
    }
    closedir(dirp);
    return 0;
}
</code></pre>