By Marcelo R-M, and Flavio
** This project is our implementation of a simple shell. **
-
-
** The shell prompts for user input **
Using the
write()function to display (stdout(1))$ -
Get user input.
Using the
char *user_input(void);function, which usesgetline()function to get and store user input into a buffer, which getline mallocs for, if getline succeds, the function returns the buffer that holds the user input. -
Parse the input.
Using the
char **tokens(char *buffer);function, tokanizes the user input and saves each token into a double pointer erray(**hold_tokens). The double pointer erray is then returned. -
Run the command.
-
Using the
int _execute(char **args);function, checks if the user input is a builtin or not(this is called a filler). For example if the commandexitis the user input. the function will make sure the command is in the array list of builtin commands."exit" or "env". -
If it's not then then the
int run(char **args);function is returned. -
Using the
int run(char **args);function, creates a child Proccess, In the child process we check if the user input is a path command (/bin/ls) or a regular command (ls). If it is a regular command (ls) we tokanize the path variable whos value is equall to PATH env variable, and cp = strcat("/", ls); =/ls, we then appendcpto every path's token deliminated by:. This is saved into a variable called new. And then usingaccesswe check if thenewvariable is correct(lsis in the dir) or if we can executels. If so thenexecvethe new varible. But if the user input is is a path then we skip all the above steps and just assigned the path toexecve. AND finally if neither is true thenperror.(print error).
-
-
Starts again at number 1 until terminated(exit, or with kill signals).
-
-
** PATH ** is an environmental ** variable in Linux ** and other Unix-like operating systems that tells the shell which directories to search for executable files. Understanding the path, is really important to buiding a shell. Its important to know how to access the PATH (working with environmental variables).
-
A builtin command is a Linux/Unix command which is "* built into a shell interpreter * such as sh, ksh, bash, etc.
#include "simpleshell.h" /** * main - main shell function, where the magic happens * takes in no arg * Return: return (0) on success */ int main(void) { /* the dollar :)*/ char *prompt = "$ "; char *buffer; char **cmd; while (1) { write(1, prompt, (sizeof(char) * 2)); buffer = user_input(); cmd = tokens(buffer); _execute(cmd); /* free(buffer);*/ /* free(cmd);*/ } }
Function (name) |
File (name) |
|---|---|
char *user_input(void); |
user_input.c |
char **tokens(char *buffer); |
tokens.c |
int main(void); |
shell_main.c |
char *_getenv(char *var_name); |
shell_getenv.c |
int run(char **args); |
run.c |
void *_realloc(void *ptr, size_t old_size, size_t new_size); |
100-realloc.c |
int _execute(char **args);int _exit(char **argv) |
execute.c |
** String functions **int _strlen(char *s);char *_strchr(char *s, char c);char *_strcat(char *dest, char *src);int _strcmp(char *s1, char *s2);char *str_concat(char *s1, char *s2); |
_strlen.c _strchr.c _strcat.c _strcmp.c 2-str_concat.c |
man_1_simple_shell |
man_1_simple_shell |
- PATH being passed as a command ex.(
/bin/ls). - single commands ex.(
ls) - muiltiple commands : parsed user input : (
ls -l)
- Exit - doesn't work first time it's typed in.
- exit and env don't work togther, either or works.(only one can be typed in at the time the shell is running)
- memory leak
- Etc...
⠀
⠀
⠀ ⠀ ⠀ Author: Marcelo Martins
⠀ ⠀ ⠀ GitHub: @matxa
⠀ ⠀ ⠀ Email: [email protected]
⠀
