blackout
blackout

Reputation: 84

scandir filter by type S_ISDIR

I am trying to create a scandir filter so I can get the list of directories. This way regular fyles and links are obviated.

int directoryFilter(const struct dirent *entry){
    struct stat st;
    stat(entry->d_name,&st);
    if(S_ISDIR(st.st_mode))
        return 1;
    return 0;
}

It works, compiles and run fine. But if I do the same execution twice I just get the . and .. directories.

Any clue about why is this happening?

Upvotes: 0

Views: 394

Answers (1)

lvella
lvella

Reputation: 13491

The directories . and .. are special entries always present in every directory. Since they start with dot, they are deemed hidden by most tools, but you still can see them if you type in the terminal ls -a.

You probably already know what they do:

  • .. is a link to the previous directory (except it /, where it points to / itself), so that opening the file ../file_in_parent_directory works.
  • . is a link to current directory, so that executing a script in current directory can be done by calling it via ./script.sh.

If you don't want for hidden directories like these to show up in your list, you must also filter out every directory that starts with dot (.). If don't want just these two, you must explicitly filter them out.

Upvotes: 1

Related Questions