Skip to content

yjyongz/simple-shell-implementation-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov

Simple C Shell Implementation Page

Project Demo

please try it!

https://yjyongz.github.io/simple-shell-implementation-c/

Supported lists:

  1. internal command: cd
  2. history support e.g: !! to execute the most recent command.
  3. multiple pipes support
  4. redirect IO support e.g: > >> 2> 2>> 2>&1 1>&2 <
  5. support background running e.g: ampersand support (&)

How To Run It

cd commands and background commands

stdin and stdout IO redirection e.g < , > and >>

stderr and stdout redirection e.g 2> and 2>>

stderr and stdout combine redirection e.g > 2>&1 and 2> 1>&2

pipeline

Design Consideration

key data strucutre

char** args[160];

why 160? you can have your own number. make sure it is big enough to cover most of shell commands.

e.g: cat /usr/share/dict/words | grep main | grep rem | grep der | grep ip > output2.txt

args[0][0] = cat

args[0][1] = /usr/share/dict/words

args[0][2] = NULL

args[1][0] = grep

args[1][1] = main

args[1][2] = NULL

args[2][0] = grep

args[2][1] = rem

args[2][2] = NULL

args[3][0] = grep

args[3][1] = der

args[3][2] = NULL

args[4][0] = grep

args[4][1] = ip

args[4][2] = >

args[4][3] = output2.txt

args[4][4] = NULL

pipeline

I adopted a recursive approach to deal with multiple pipelines.

invoke: func(0, 4)

func(index_of_commands, total_commands):

    if index_of_commands == total_commands: return
    
    pid = fork()
    
    if pid == 0:
    
        execvp();
        
    else:
    
        func(index_of_commands+1, total_commands)
        
        waitpid();

history

it's a circular buffer

char *history[MAX_LENGTH]; >>> number of history to remember

int index = 0 >>> init index

index = (index + 1) % MAX_LENGTH >>>move to next item

signal handler

you need to register SIGINT with shell process.

and then we need to send minus pid value (which will send a SIGTERM to all the child processes in the same process group)

kill(-pid, SIGTERM)

Limitation:

  1. jobs lists.
    • as of now, this shell cannot list background jobs that are running.
  2. fg
    • fg does not work

About

simple shell implementation in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published