Shell Unix in C

During 42 school, I realized operating system projects including the creation of a Unix system Command Line Interpreter in C language. This project, 42sh, is the accomplishment of a dozen other projects carried out in a logical sequence which is validated in several weeks to a few months each.

The use of C libraries was limited, and the compilation rules and programming standards were strict. Our team of 3 students chose Bash 5.0 as the reference shell and developed the following functionalities:

  • Execution of commands with their parameters and management of $PATH
  • Error handling and return value
  • Complete command line editing
  • Dynamic and contextual completion of commands, built-ins, files and variables
  • Inhibitors :
    • single quote
    • double quote
    • \ backslash
  • Logical operators :
    • &&
    • ||
  • Redirect operators :
    • >
    • >>
    • <
    • <<
    • <<<
    • >&
    • <&
    • |
  • The separator ;
  • Management of all signals
  • Program suspension by the operator & as well as the built-ins :
    • jobs
    • fg
    • bg
  • Integration of internal variables with :
    • creation with syntax var=value
    • display with buit-in set
    • revocation with the built-in unset (internal and environment variable)
    • export with built-in export
  • Alias support with built-ins alias and unalias
  • Optimizing the search for binaries by a hash table accessible by the built-in hash
  • As well as the following built-ins :
    • cd
    • echo
    • type
    • test
    • exit

All the buit-ins respect the standard POSIX except set, unset and test.

Only the following functions were imported from the C libraries:
_access _chdir _close _closedir _dup _dup2 _environ _execve _exit _fork _free _getcwd _getenv _getpgrp _getpid _getpwnam _getpwuid _getuid _ioctl _isatty _kill _lstat _malloc _memcpy _nanosleep _open _opendir _pipe _read _readdir _setpgid _signal _stat _tcgetattr _tcgetpgrp _tcsetattr _tcsetpgrp _tgetent _tgetstr _tgoto _tputs _uname _unlink _waitpid _write

[sujet] [source]


Creation of a Unix shell in C with the minimum of libraries and a maximum of functionalities. Result similar to bash 5.0