This project implements a basic shell in C, which supports built-in commands like cd
, echo
, pwd
, history
and exit
.
-
The shell displays a prompt with the format
<username@system_name:curr_dir>
, and handles commands like a regular shell. -
The directory from which the shell is invoked will be the home directory of the shell and will be indicated by
~
.
To compile the project, use the provided makefile
by running the command make
in the terminal. Then, you can run the shell using ./cshell
.
Run:
make && ./cshell
main.c
andmain.h
: Contains the main logic of the shell, including the command execution loop.display.c
anddisplay.h
: Implement the display functions for the shell prompt.execute.c
andexecute.h
: Handles the execution of commands likecd
,echo
,pwd
,history
andexit
.input.c
andinput.h
: Handle reading user input and tokenizing it.history.c
andhistory.h
: Provide a command history feature that can store up to 20 commands across sessions.builtin-commands/
: Directory containing the implementation of built-in commands (cd.c , echo.c, pwd.c , and func.h for header files).history.txt
: File used to store command history across sessions (automatically created if not present).makefile
: Makefile for building the project.
The cd
command allows the user to change the current working directory. It supports the following options:
cd
: Go to the home directory.cd ..
: Move up one level in the directory hierarchy.cd -
: Move to the previous directory.cd ~
: Go to the home directory.cd .
: Stays in the current directory.cd [directory]
: Attempt to change to the specified directory.
The echo
command displays lines of text or strings passed as arguments. It handles spaces and tabs between words.
The pwd
command prints the absolute pathname of the current working directory.
The history
command displays the command history. By default, it shows the last 10 commands, but you can specify a different count. It also stores up to 20 commands across sessions, overwriting the oldest commands if the limit is exceeded. Identical consecutive commands are not stored.
Handling Repeated Commands and Persistence of History
The code uses perror.h
to handle errors appropriately. If a command cannot be executed or returns an error, it is handled accordingly.
Also has customised error messages for invalid commands in RED color.
The code is organized into separate modules, each responsible for a specific functionality. This ensures a well-structured and modular codebase.