diff --git a/dash-shell.c b/dash-shell.c index f69eea1..83c6476 100644 --- a/dash-shell.c +++ b/dash-shell.c @@ -10,39 +10,39 @@ Basic working of Dash shell: #include #include #include +#include +#include +#include -char* command; -char** argument_list; -char* builtin_functions_name_list[] = {"help","-","cd","about"}; +char **argument_list; +char *builtin_functions_name_list[] = {"help", "-", "cd", "about"}; void exit_shell() { printf("Bye Bye"); exit(0); } + void about() { system("cat banner.txt"); } -void cd(char** cmd) +void cd(char **cmd) { - if(cmd[1] == NULL) + if (cmd[1] == NULL) { printf("Wrong Usage! Use cd /directory"); - return ; - + return; } else { - if(chdir(cmd[1]) != 0) - { - printf("Directory not found!"); - return ; - } - + if (chdir(cmd[1]) != 0) + { + printf("Directory not found!"); + return; + } } - } void help() @@ -53,160 +53,108 @@ void help() printf(" about -> about the program\n"); } -void init()// set up termianl add colour as green,red. +void init() { - printf("\033[1;32m"); printf("\n"); system("cat banner.txt"); printf("\n"); system(" echo HELLO $USER"); } -int check_builtins() //returns 0 for false 1 for true + +int check_builtins(char *command) { int builtin_number = -1; - int size = sizeof(builtin_functions_name_list)/sizeof(char *); - for(int i = 0; i "); - - //printf("\033[1;32;40m");//Green \033[1;32m - command = input(); - printf("\n"); - int status; - argument_list = parse(command); - if(check_builtins()) - { - printf("\n"); - continue; - } - - int pid = fork(); - - if (pid == 0) { - - //printf("\n"); - int status_code = execvp(command, argument_list); - - printf("\033[5;31m ERROR! Command not found.\n"); //Adding error blinker here - - if (status_code == -1) { - printf("\033[25;32m\n"); - return 1; + command = readline("🐚 -> "); + if (strcmp(command, "") == 0) { + continue; } + add_history(command); + argument_list = parse(command); + if (check_builtins(command)) + { + continue; + } + int pid = fork(); + if (pid == 0) + { + int status_code = execvp(command, argument_list); + printf("\033[5;31m ERROR! Command not found.\n"); + if (status_code == -1) + { + printf("\033[25;32m\n"); + return 1; + } + } + else + { + int status; + do + { + int wpid = waitpid(pid, &status, WUNTRACED); + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + } + free(command); } - else { - // Old Parent process. The C program will come here - do { - int wpid = waitpid(pid, &status, WUNTRACED); - } - while (!WIFEXITED(status) && !WIFSIGNALED(status)); - - - } - } - return 0; - } - int main() - { - //printf("\n"); + +int main() +{ init(); execute(); - - - } - + return 0; +}