1

I wrote a program that print the directory name or file name. It's easy but I got something trouble. It couldn't distinguish directory and file type. I know and I used stat.st_mode to finish it. But something is wrong:

enter image description here

When I use gdb to check the st_mode value, I found it was 0, except "." and "..", so here is the question: why st_mode is 0?

and that is my code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

int main(void)
{
    DIR *pDir = opendir("MyDirectory");
    struct dirent *pDirent;
    struct stat vStat;

    if (pDir == NULL)
    {
        printf("Can't open the directory \"MyDirectory\"");
        exit(1);
    }

    while ((pDirent = readdir(pDir)) != NULL)
    {
        stat(pDirent->d_name, &vStat);
        if (S_ISDIR(vStat.st_mode))
            printf("Directory: %s\n", pDirent->d_name);
        else
            printf("File: %s\n", pDirent->d_name);
    }

    closedir(pDir);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Smith John
  • 1,035
  • 1
  • 10
  • 19

1 Answers1

8

Classic readdir mistake: pDirent->d_name is the name of the directory entry, not a path to the file. It's "1", "4-5.c", etc. So your stat calls are looking for a file with that name in the current directory, not under MyDirectory.

Check the return value of stat. You'll see that it's ENOENT — except for . and .., which exist in the current directory as well. When stat fails, the content of the stat structure is undefined.

If you're calling opendir in a directory other than ., then to do pretty much anything useful with the returned names, you need to build a full path. Copy the path you passed to opendir to a buffer with enough room for a slash and file name in addition, and copy each file name to that buffer. Proof-of-concept code (error checking omitted, etc.):

char *directory = "MyDirectory";
size_t directory_length = strlen(directory);
char *path = malloc(directory_length + 1 + NAME_MAX);
strcpy(path, directory);
path[directory_length] = '/';
while ((pDirent = readdir(pDir)) != NULL) {
    strcpy(path + directory_length + 1, pDirent->d_name);
    if (stat(path, &vStat) == -1) {
        perror(path);
        continue;
    }
    …
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • thank you for your answer! Now I know the reason. Yes, You're right, and it's the best way I think. And I also got the another way, that's use chdir(pathname), and that's also worked. – Smith John Dec 09 '15 at 00:10
  • @SmithJohn: note that using `chdir()` works, but can leave you with problems if you need to process several directories in one command. How do you get back to where you started? (One answer is [`fchdir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fchdir.html) — others are considerably more complex.) – Jonathan Leffler May 22 '19 at 19:09