Skip to content

sleepyheadmp3/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Minishell

Overview

This is an executable minishell that runs a basic shell program !! The shell implements cd and exit as built-ins, includes signal handling, and uses fork/exec for all other commands.

Usage

The program is called from the command line and takes no arguments. When called, the program prints the current working directory and waits for user input. Directory names are printed in BRIGHTBLUE \x1b[34;1m.

$ ./minishell

[/home/user/minishell]$

Commands

cd and exit are manually implemented commands in this shell. All others use exec.

  1. cd

    If cd is called with no arguments or the one argument ~, it changes the working directory to the user's home directory (not hard coded). This also works with ~ followed by trailing directory names, like cd ~/Downloads.

    If cd is called with one argument that isn't ~, it attempts to open the directory specified in that argument.

    Additionally, cd supports changing into a directory whose name contains spaces. These directory names must be enclosed in double spaces, such as:

      cd "some folder name"
      cd "some"" folder ""name"
    

    If a quote is missing (i.e. cd "Downloads), an error message is displayed and the program exits in failure.

  2. exit

    The exit command causes the shell to terminate and return EXIT_SUCCESS. Using exit is the only way to stop shell's execution normally.

  3. exec

    All other commands are executed using exec. When an unknown command is entered, the program forks. The child program will exec the given command and the parent process will wait for the child process to finish before returning to the prompt.

    Commands are bounded at 4096 characters including '\0', and line tokens 2048 (including trailing NULL).

Error handling

Errors for the system/function calls are handled using strerror(errno). The shell exits gracefully varying error scenarios with a detailed error message.

Signal handling

When the minishell captures the SIGINT signal, it returns the user to the prompt. Interrupt signals generated in the terminal are delivered to the active process group, which includes both parent and child processes. The child will then receive the SIGINT and handle it accordingly.

Sample executions

$ ./minishell

[/home/user/minishell]$ echo HI

HI

[/home/user/minishell]$ cd ..

[/home/user]$ cd minishell

[/home/user/minishell]$ cd /tmp

[/tmp]$ cd ~

[/home/user]$ cd minishell

[/home/user/minishell]$ ls

Makefile minishell minishell.c

[/home/user/minishell]$ pwd

/home/user/minishell

[/home/user/minishell]$ cd

[/home/user]$ pwd

/home/user

[/home/user]$ nocommand

Error: exec() failed. No such file or directory.

[/home/user]$ cd minishell

[/home/user/minishell]$ ^C

[/home/user/minishell]$ sleep 10

^C

[/home/user/minishell]$ exit

$

About

Basic shell program in C!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors