Skip to content
Advertisement

Is using chdir() the only way to change the working directory in Linux C?

I have been given an assignment where I have to build a custom terminal using ncurses and C, and implementation of several commands is requested.

One of the commands happens to be chdir, which is meant to change the working directory, however I could not find anything more relevant in C when it comes to changing the working directory besides the already built in chdir() function in C.

It is requested I do not use the available chdir() function but rather build my own. I am not even sure if this is possible, or if it is a mistake, shouldn’t directory handling be implementation dependent?

Thank you for your time and help.

Advertisement

Answer

chdir() is the only way to change the current working directory of the process. If you are not allowed to use this function you can partially simulate chdir() by keeping track of the current directory in the application, e.g. by using a variable. Note that such a simulation will not be complete, in particular the working directory of child processes will be where your program was started, not then one in your variable.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement