Project 0x16 - Simple Shell - A simple UNIX command interpreter that executes commands read from the standard input or from a file. Based on the Bourne Shell.
- access (man 2 access)
- chdir (man 2 chdir)
- close (man 2 close)
- closedir (man 3 closedir)
- execve (man 2 excve)
- exit (man 3 exit)
- _exit (man 2 _exit)
- fflush (man 3 fflush)
- fork (man 2 fork)
- free (man 3 free)
- getcwd (man 3 getcwd)
- getline (man 3 getline) - Bonus if not used
- isatty (man 3 isatty)
- kill (man 2 kill)
- malloc(man 3 malloc)
- open (man 2 open)
- opendir (man 3 opendir)
- perror (man 3 perror)
- read (man 2 read)
- readdir (man 3 readdir)
- signal (man 2 signal)
- stat (__xstat)(man 2 stat)
- lstat (__lxstat)(man 2 lstat)
- fstat (__fxstat)(man 2 fstat)
- strtok (man 3 strtok) - Bonus if not used
- wait (man 2 wait)
- waitpid (man 2 waitpid)
- wait3 (man 2 wait3)
- wait4 (man 2 wait4)
- write (man 2 write)
Compile:
gcc -Wall -Wextra -Werror -pedantic *.c -o hsh
./hsh
$ echo Hello World
cat testfile
echo Hello World
./hsh testfile
Hello World
cd:
$ cd directory/to/change/to
env:
$ env
setenv:
$ setenv VARIABLENAME VALUE
unsetenv:
$ unsetenv VARIABLENAME
exit:
$ exit 127
- my_shell.c - The main function for simple_shell
int main(int argc, char **argv, char **envp)
- main function, where program startsvoid my_error(char *command, int status, char *extra)
- Custom error printingchar *get_prog_name(char *name)
- Gets the program nameint linum(int add)
- Gets current line number and/or add to it
- env_ops0.c - Environmental variable functions
char *get_env_val(char **, char*)
- Gets the value of an env variablechar **get_path(char **env)
- Gets the path in a double char pointerchar *find_path(char **path, char *command)
- finds if a command exist in a path and returns the pathchar **get_env(void)
- Get current environment as a malloc'd NULLchar *get_full_command(char *path, char *command)
- Gets the command with the correct path prepended
- env_ops1.c - More environment variable functions
char **do_env(char *add, char *delete)
- Get the env, or add a variable, or delete a variable
- ll_ops0.c - Linked list functions
size_s list_len(list_s *h)
- Gets size of a list_s linked listlist_s *add_node(list_s **head, void *ptr)
- Adds a new node at the beginning of a list_s linked listlist_s *add_node_end(list_s **head, void *ptr)
- Adds a new node at the end of a list_s linked listvoid free_list(list_s *head)
- Frees list_s linked list
- ll_ops1.c - More linked list functions
void free_list_full(list_s *head)
- Frees list_s linked list and all contained pointerslist_s *get_node_at_index(list_s *head, unsigned int index)
- Returns the nth node of a linked listlist_s *insert_node_at_index(list_s **head, unsigned int idx, void *ptr)
- Inserts a new node at a given positionint delete_node_at_index(list_s **head, unsigned int index)
- Deletes a node at a given position
- ll_ops2.c - More linked list functions
char **arrayify(list_s *head)
- copy a char * linked list into a char **list_s listify(char **arr)
- copy a char ** array into a malloc'd char * linked listvoid free_double_array(char **list)
- Free a double char pointer
- __ll_ops0.c - Linked List functions
size_t __list_len(list_t *h)
- Get size of a list_t linked listlist_t *__add_node(list_t **head, void *ptr)
- Adds a new node at the beginning of a list_t listlist_t *__add_node_end(list_t **head, void *ptr)
- Adds a new node at the end of a list_t listvoid __free_list(list_t *head)
- Frees list_t linked list
- __ll_ops1.c - More linked List functions
void __free_list_full(list_t *head)
- Frees list_t and all contained pointerslist_t *__get_node_at_index(list_t *head, unsigned int index)
- Returns nth node of a linked listlist_t *__insert_node_at_index(list_t **head, unsigned int idx, void *ptr)
- Inserts a new node at a given positionint __delete_node_at_index(list_t **head, unsigned int index)
- Deletes a node at a given position
- str_ops0.c - String functions
int _strcmp(char *s1, char *s2)
- Compares two stringsint _strlen(char *s)
- Gets the length of a stringint word_count(char *str, char *delim)
- counts the number of words in a string separated by a delimchar *_strcat(char *dest, char *src)
- Concats two stringschar *_strcpy(char *dest, char *src)
- Copy src into dest
- str_ops1.c - More string functions
int _atoi(char *s)
- Converts a string to an integerint sizeof_command(char **tokens, int place)
- Gets the size a command separated by ;, &&, ||int _isdigit(int c)
- Check for a digit (0 through 9)int has_newline(char *input)
- checks for \n and returns the indexvoid shiftbuffer(char *input, int newline_index, int filled)
- shifts the buffer to the next command after
- str_ops2.c - More string functions
char *_reverse(char *str, int n)
- Reverses the content of a stringchar *_itoa(int num)
- Converts an integer base 10 to a stringchar *_memset(char *s, char b, int n)
- memset function
- my_exit.c - Memory and exit functions
void *do_mem(size_t size, void *ptr)
- Malloc, free or free all with a static recordvoid do_exit(int fd, char *msg, int code)
- Custo exit with error message, code and automatic memory cleanup
- strtok_to_double_pointer.c - strtok function that returns double pointer
void _free(char **list, int count)
- Frees a double char pointer in case of errorchar **_strtok(char *str, char *delim)
- Splits a string into an array of strings based on delimiters
- _getline.c - getline function
char *_getline2()
- Reads 1024 characters from stdin and returns the string readssize_t _getline(char **lineptr, size_t n, int stream)
- Reads a number of chars from stdin and returns size readssize_t else_handle_input(char *lineptr, int stream, char *input, int filled)
- Handle buffer if it does not include \n or EOF
- builtin_ops.c - functions for builtin commands
char **get_builtins()
- Create list of builtin functionsvoid cd_builtin(char **tokens)
- Executes cd function, changes directoryint env_builtin(void)
- lists environmentint setenv_builtin(char **tokens)
- Set an environment variableint unsetenv_builtin(char **tokens)
- Unset an environment variable
- file_ops.c - Prep input file to execute list of commands
char *read_textfile(char *filename)
- read text file and return as string
- exec_ops0.c - Execute functions
int exec_builtin(char **tokens, int bcase)
- Execute function for builtinsint check_access(char *comm, char *token)
- Checks if path exists or if permission exists for commandchar *prep_execve(char *token)
- preps command by checking current path and then the PATH for commandint exec_nb(char **tokens)
- Execute function for non builtinsint search_ops(char **tokens)
- search for ;, &&, || operators
- exec_ops1.c - More execute functions
int exec_single(char **tokens)
- execute a single commandchar **get_next_commands(char **tokens)
- Gets the tokens after the first &&, || or ;char **get_current_command(char **tokens)
- Gets the command before && || or ;int execute(char **tokens)
- Main execute funtion
- Jose Alvarez de Lugo
- William Dyrland-Marquis