I am doing an Unix programming exercise to find out the i-node number of a directory and its parent directory, I know that command "ls -ldi" satisfies my requirement but as I said I am doing Unix programming exercise and just want to further understand it so I write the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
fprintf(stderr, "usage: ex1 directory_name");
if((dp = opendir(argv[1])) == NULL)
fprintf(stderr, "can't open %s", argv[1]);
while((dirp = readdir(dp)) != NULL)
if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
printf("inode number of %s is %llu\n", dirp->d_name, dirp->d_ino);
closedir(dp);
exit(0);
}
I just check all the entries of a given path and compare if the name is dot or dot-dot, if it is, then print out its d_ino.
Here is what I am confused by: if I run "./a.out .", this acts as what "ls -ldi .", meaning they both give the i-node number of the current directory and its parent directory, but if I run "./a.out /" expecting to find out the i-node number of the root directory and its parent directory, then my program print the i-node number 2 for "." and 1 for ".." but command "ls -ldi /." and "ls -ldi /.." both print 2.
So I am wondering why this occurs.
Thanks!