Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does fchdir work?

Tags:

c

chdir

In it's main page it says:

fchdir() is identical to chdir(); the only difference is that the directory is given as an open file descriptor.

And the prototype is given as following:

int chdir(const char *path);
int fchdir(int fd);

My question is how can a directory be passed as a file descriptor? Do directories also have a corresponding descriptor like files?

like image 607
UtkarshPramodGupta Avatar asked Oct 21 '25 11:10

UtkarshPramodGupta


1 Answers

Do directories also have a corresponding descriptor like files?

Yes. The Unix philosophy(and Linux) is to treat everything as a stream of bytes. So yes, you can do open(2) on a directory and get its file descriptor. Not only directories but sockets, pipes and devices can also be opened using open(2) system call and do operations on it as though it's a file.

like image 62
P.P Avatar answered Oct 23 '25 03:10

P.P