simple_shell is a command line interpreter, or shell, in the tradition of the first Unix shell written by Ken Thompson in 1971. This shell is intentionally minimalistic, yet includes the basic functionality of a traditional Unix-like command line user interface.
Standard functions and system calls employed in simple_shell include:
access, execve, exit, fork, free, fstat, getline, malloc, perror, signal, stat, wait, write.
- AUTHORS - List of contributors to this repository
- shell.h - program header file
- _atoi.c - major builtin functions
interactive
- returns true if shell is interactive modeis_delim
- checks if character is a delimeter_isalpha
- checks for alphabetic character_atoi
- converts a string to an integer
- builtin.c - helper functions for the builtins
_myexit
- exits the shell_mycd
- changes the current directory of the process_myhelp
- changes the current directory of the process
- builtin1.c - functions related to the environment
_myhistory
- displays the history list, one command by line, precededunset_alias
- sets alias to stringset_alias
- sets alias to stringprint_alias
- prints an alias string_myalias
- mimics the alias builtin (man alias)
- environ.c - functions related to printing errors
_myenv
- prints the current environment_getenv
- gets the value of an environ variable_mysetenv
- Initialize a new environment variable or modify an existing one_myunsetenv
- Remove an environment variablepopulate_env_list
- populates env linked list
- errors.c - memory allocation functions
_eputs
- prints an input string_eputchar
- writes the character c to stderr_putfd
- writes the character c to given fd_putsfd
- prints an input string
- errors1.c - custom strtok and helper functions
_erratoi
- converts a string to an integerprint_error
- prints an error messageprint_d
- function prints a decimal (integer) number (base 10)convert_number
- converter function, a clone of itoaremove_comments
- function replaces first instance of '#' with '\0'
- exits.c - functions related to executing commands
*_strncpy
- copies a string*_strncat
- concatenates two strings*_strchr
- locates a character in a string
- getLine.c - functions related to executing commands
input_buf
- buffers chained commandsget_input
- gets a line minus the newlineread_buf
- reads a buffer_getline
- gets the next line of input from STDINsigintHandler
- blocks ctrl-C
- getenv.c - functions related to executing commands
get_environ
- returns the string array copy of our environ_unsetenv
- Remove an environment variable_setenv
- Initialize a new environment variable or modify an existing one
- getinfo.c - functions related to executing commands
clear_info
- initializes info_t structset_info
- initializes info_t structfree_info
- frees info_t struct fields
- history.c - functions related to executing commands
get_history_file
- gets the history filewrite_history
- creates a file, or appends to an existing fileread_history
- reads history from filebuild_history_list
- adds entry to a history linked listrenumber_history
- renumbers the history linked list after changes
- lists.c - functions related to executing commands
add_node
- adds a node to the start of the listadd_node_end
- adds a node to the end of the listprint_list_str
- prints only the str element of a list_t linked listdelete_node_at_index
- deletes node at given indexfree_list
- frees all nodes of a list
- lists1.c - functions related to executing commands
list_len
- determines length of linked listlist_to_strings
- returns an array of strings of the list->strprint_list
- prints all elements of a list_t linked listnode_starts_with
- returns node whose string starts with prefixget_node_index
- gets the index of a node
- main.c - entry point
- memory.c - functions related to executing commands
bfree
- frees a pointer and NULLs the address
- parser.c - functions related to executing commands
is_cmd
- determines if a file is an executable commanddup_chars
- duplicates charactersfind_path
- finds this cmd in the PATH string
- realloc.c - functions related to executing commands
*_memset
- fills memory with a constant byteffree
- frees a string of strings_realloc
- reallocates a block of memory
- shell_loop.c - functions related to executing commands
hsh
- main shell loopfind_builtin
- finds a builtin commandfind_cmd
- finds a command in PATHfork_cmd
- forks a an exec thread to run cmd
- string.c - functions related to executing commands
_strlen
- returns the length of a string_strcmp
- performs lexicogarphic comparison of two strangs.starts_with
- checks if needle starts with haystack_strcat
- concatenates two strings
- string1.c - functions related to executing commands
_strcpy
- copies a string_strdup
- duplicates a string_puts
- prints an input string- `_putchar - writes the character c to stdout
- tokenizer.c - functions related to executing commands
**strtow
- splits a string into words. Repeat delimiters are ignored**strtow2
- splits a string into words
- vars.c - functions related to executing commands
is_chain
- test if current char in buffer is a chain delimetercheck_chain
- checks we should continue chaining based on last statusreplace_alias
- replaces an aliases in the tokenized stringreplace_vars
- replaces vars in the tokenized stringreplace_string
- replaces string
simple_shell is designed to run in the Ubuntu 20.04 LTS
linux environment and to be compiled using the GNU compiler collection v. gcc
, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
- Clone this repository:
git clone "https://github.com/AbuhaithemAlthry/simple_shell.git"
- Change directories into the repository:
cd simple_shell
- Compile:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh
- Run the shell in interactive mode:
./hsh
- Or run the shell in non-interactive mode: example
echo "pwd" | ./hsh
The simple_shell is designed to execute commands in a similar manner to sh, however with more limited functionality. The development of this shell is ongoing. The below features will be checked as they become available (see man page for complete information on usage):
- uses the PATH
- implements builtins
- handles command line arguments
- custom strtok function
- uses exit status
- shell continues upon Crtl+C (^C)
- handles comments (#)
- handles ;
- custom getline type function
- handles && and ||
- aliases
- variable replacement
- exit
- env
- setenv
- unsetenv
- cd
- help
- history
Run the executable in your terminal after compiling:
$ ./hsh
$ # This is our rendition of the shell
$ ls -al
total 100
drwxrwxr-x 3 vagrant vagrant 4096 Jul 19 22:49 .
drwxr-xr-x 14 vagrant vagrant 4096 Jul 17 22:37 ..
-rw-rw-r-- 1 vagrant vagrant 144 Jul 19 17:16 AUTHORS
-rw-rw-r-- 1 vagrant vagrant 2367 Jul 19 22:33 builtins2.c
-rw-rw-r-- 1 vagrant vagrant 2764 Jul 19 22:14 builtins.c
-rw-rw-r-- 1 vagrant vagrant 710 Jul 16 01:03 environment.c
-rw-rw-r-- 1 vagrant vagrant 1217 Jul 16 03:24 errors.c
drwxrwxr-x 8 vagrant vagrant 4096 Jul 19 22:34 .git
-rwxrwxr-x 1 vagrant vagrant 32287 Jul 19 22:34 hsh
-rw-rw-r-- 1 vagrant vagrant 1792 Jul 19 22:12 man_1_simple_shell
-rw-rw-r-- 1 vagrant vagrant 484 Jul 15 20:09 memory_allocation.c
-rw-rw-r-- 1 vagrant vagrant 1273 Jul 18 21:00 new_strtok.c
-rw-rw-r-- 1 vagrant vagrant 3427 Jul 19 22:06 path.c
-rw-rw-r-- 1 vagrant vagrant 2347 Jul 19 22:49 README.md
-rw-rw-r-- 1 vagrant vagrant 1769 Jul 19 22:04 shell.h
-rw-rw-r-- 1 vagrant vagrant 1480 Jul 18 21:15 simple_shell.c
-rw-rw-r-- 1 vagrant vagrant 2111 Jul 16 01:10 strfunc.c
-rw-rw-r-- 1 vagrant vagrant 719 Jul 19 21:46 tokenize.c
At this time, there are no known bugs.
Seid Muhammed | GitHub | Twitter
Selehadin Seid | GitHub
simple_shell is open source and therefore free to download and use without permission.