0

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.

Stefan
  • 55
  • 5
  • 6
    that's not possible, and it's a mistake. you have to use the system call chdir(2) in order to change the pwd. whoever said you have to build your own doesn't know what they're talking about. – Ahmed Masud Apr 26 '17 at 14:23
  • I figured so, thank you for confirming this :) – Stefan Apr 26 '17 at 14:26
  • 5
    The current working directory of a process is controlled by the kernel. To truly "build your own" you'd have to modify the kernel. As @AhmedMasud said, whoever said you have to build your own doesn't know what they're talking about. One thing to be aware of, though, is that maybe you *shouldn't* change the working directory because it is a process-wide thing and can affect other parts of the program. In general, `chdir()` is something to be used very carefully. – Andrew Henle Apr 26 '17 at 14:27
  • 1
    If this works: http://stackoverflow.com/questions/2375003/how-do-i-set-the-working-directory-of-the-parent-process you could in theory use it - I think it could create a funny reaction. The real answer is, as already mentioned, yes - it's the only way. – sknt Apr 26 '17 at 14:30
  • @JohnBollinger I asked my lecturer and he specifically said I have to implement my own chdir function, rather than use the one ready at hand, which is why I found it so confusing. – Stefan Apr 26 '17 at 14:39
  • @Skynet I'd do that, but wouldn't that be a cheeky way to use chdir without formally calling it? :) – Stefan Apr 26 '17 at 14:43
  • @JohnBollinger chdir(1) is not a utility of any sort (nature of changing present working directories being what they are cannot be a sub process), the shell command in bourne shell compatible is the built-in command cd – Ahmed Masud Apr 26 '17 at 14:53
  • 2
    @Stefan, your assignment already seems weird, as the working directory ordinarily is not directly relevant to the terminal, but rather to what ever is running *in it*, e.g. a shell, which will typically have its own provisions for such things. I continue to suspect a misunderstanding here, possibly related to imprecise word usage. If the lecturer insists that you implement `chdir`, then I suggest you seek their help or advice on how to implement it. Feel free also to point them to this discussion. – John Bollinger Apr 26 '17 at 14:56
  • @AhmedMasud, I've seen too many operating systems in too many years. You are correct that `chdir` is not the name of a standard utility, but I note that many shells from both major Unix families provide it as a builtin (though Bash and the original Bourne shell are not among those). – John Bollinger Apr 26 '17 at 15:03
  • @JohnBollinger actually i stand corrected bourne shell did provide chdir; korn didn't – Ahmed Masud Apr 26 '17 at 15:27

1 Answers1

3

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.

Johan
  • 3,667
  • 6
  • 20
  • 25
  • 3
    You can open the directory to obtain a file descriptor and use [fchdir()](http://pubs.opengroup.org/onlinepubs/009695399/functions/fchdir.html). That conforms to the letter of the rule but not the spirit. – JeremyP Apr 26 '17 at 14:39