The .
and ..
represent the current and parent directory and are present in all directories (see footnote below). readdir()
does not filter them out as they are valid entries within a directory. You can do the following to filter them out yourself.
while((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
continue;
}
printf("%s \n", dir->d_name);
}
Note: Technically, SUSv3 does not require that .
and ..
actually be present in all directories, but does require that the OS implementation correctly interpret them when encountered within a path.