Directory Commands

The skeleton code for your shell has a dispatcher for “built-in” commands. Every shell needs to support a number of built-in commands, which are functions in the shell itself, not external programs. For example, the exit command needs to be implemented as a built-in command, because it exits the shell itself. So far, the only two built-ins supported are ?, which brings up the help menu, and exit, which exits the shell. Please have a look at the cmd_table data in src/shell.c to understand how this is implemented.

Add a new built-in pwd that prints the current working directory to standard output. Then, add a new built-in cd that takes one argument, a directory path, and changes the current working directory to that directory.

You may find the syscalls chdir and getcwd helpful.

Please note, that you should properly report errors received from any of the system calls used (for this task those are chdir and getcwd). System calls normally return -1 in case of errors and set the global variable errno to a value identifying the cause of the error. Please refer to the documentation of the system calls for details.

In case of chdir used with an non-existent directory your shell should for instance print cd: does_not_exist: No such file or directory, where does_not_exist is the name of the non-existing directory. For the actual error message you can rely on the strerror helper function.

All error messages should be printed to stderr.

Next up: read about Program Execution.