0

It looks like I cannot change the working directory of the parent process, but is there any way to simulate this? I am learning C by writing a command-line tool x that allows the user to navigate the filesystem with custom commands. Would it be possible to write something like x enter [directory] and then x look and get something equivalent to cd [directory] and then ls?

My only thought is reading/writing to some .config file before/after every command, a way of managing state across multiple x executions.

I realize this is a problem of my own making, but I've had a lot of fun playing with C and don't want to give up on my idea for a tool that wraps the basic filesystem traversal of the Unix builtins.

Community
  • 1
  • 1
jds
  • 7,910
  • 11
  • 63
  • 101
  • IMO persisting some state sounds like the way to go. – NPE Jan 04 '15 at 22:13
  • 1
    You are on the right track. The purpose of the filesystem is to provide storage for data that needs to persist between program invocations. Your remembered current directory is a good example of such. – user4815162342 Jan 04 '15 at 22:13
  • Have you considered writing your own "shell"? cd is embedded into shells, which is why it works. You could have considerably more fun trying to write something like that, since you also get to execute subprograms and such. You can start with a simple read-eval-print loop with exit and then build on top of this. – lared Jan 04 '15 at 22:15
  • @lared, that sounds fun but I'm not sure I would know where to start. I haven't done much low-level programming. Could you recommend something to read or search or a tutorial? – jds Jan 04 '15 at 22:22
  • It doesn't have to be low level at all. It could be just a usual program which reads a line from the user and, depending on what the user wrote, reacts accordingly. After that it just waits for another instruction. That's roughly what bash and other shells are, but they are obviously complicated. A simple version can be a `switch` in a `while(1)` loop. You can use variables to keep track of your directory and whatnot. If you absolutely need the shell in which you are running your shell (like bash) to do something, you can use `system` method. It's simpler than it sounds. – lared Jan 04 '15 at 22:26

1 Answers1

1

I worked on a design for something like this years ago (I called it mouse for some reason). The first time it is started, it spawns a background process that reads from a fifo. All further commands get passed in via the fifo and executed from the background process.

There was some difficulty with keeping the fifo open. tail -f on a regular file might be easier. These days, I'd design it with some kind of socket but that's a whole 'notehr can of worms.

Joshua
  • 40,822
  • 8
  • 72
  • 132