I am writing a C program like,
void printdir (char*);
int main () {
printf ("Directory scan of /home: \n");
printdir ("/home/fahad/");
exit (0);
}
void printdir (char *dir) {
struct dirent *entry;
DIR *dp = opendir (dir);
if (dp == NULL) {
fprintf (stderr, "Cannot open dir:%s\n", dir);
return;
}
chdir (dir);
while ((entry = readdir(dp)) != NULL)
printf ("%s\n",entry -> d_name);
closedir (dp);
}
Interestingly, it shows output in an unexpected way.
Considering the fact that whenever a directory is created in UNIX
. First two entries are created inside this directory one is .
and other is ..
. So basically their inode
numbers should be less than the directory entries created through mkdir ()
or open ()
(for directory and file respectively).
My question is, in what order readdir ()
system call reads the directory entries? Because I don't get first who entries .
and ..
.
Why is that so?