Advertisement
Armaritto

Untitled

Mar 6th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/wait.h>
  6.  
  7. void register_child_signal(){
  8.  
  9. }
  10.  
  11. void execute_command(char** args){
  12.     int pid;
  13.     int status;
  14.     pid = fork();
  15.     if(pid == 0){
  16.         execvp(args[0], args);
  17.         printf("Error! Command not found\n");
  18.         exit(0);
  19.     }
  20.     else if(pid < 0){
  21.         printf("Error forking!\n");
  22.     }
  23.     else{
  24.         do{
  25.             waitpid(pid, &status, 0);
  26.         }
  27.         while(!WIFEXITED(status) && !WIFSIGNALED(status));
  28.     }
  29. }
  30.  
  31. void execute_shell_builtin(char** command_array){
  32.     if(!strcmp("cd", command_array[0])){
  33.  
  34.     }
  35.     else if(!strcmp("echo", command_array[0])){
  36.  
  37.     }
  38.     else if(!strcmp("export", command_array[0])){
  39.  
  40.     }
  41. }
  42.  
  43. int evaluate_command(char** command_array){
  44.     if(!strcmp("cd", command_array[0]) || !strcmp("echo", command_array[0]) || !strcmp("export", command_array[0])){
  45.         return 0;
  46.     }
  47.     else if(!strcmp("exit", command_array[0])){
  48.         return -1;
  49.     }
  50.     else{
  51.         return 1;
  52.     }
  53. }
  54.  
  55. char** parse_command(char input[]){
  56.     static char* strings[100];
  57.     int index = 0;
  58.     char* token = strtok(input, " \n"); // split string by space and newline
  59.     while (token != NULL){
  60.         strings[index++] = token ;
  61.         token = strtok(NULL, " \n");
  62.     }
  63.     strings[index] = NULL;
  64.     return strings;
  65. }
  66.  
  67. void shell_loop(){
  68.     int status = 1;
  69.     do {
  70.         printf("%s", "$ ");
  71.         char input_command[100] ;
  72.         fgets(input_command, 100, stdin);
  73.         char** command_array = parse_command(input_command);  // parse array of char to array of strings seperated by space and ended with null
  74.         int type = evaluate_command(command_array); // 0: shell builtin, 1: executable or error, -1: exit
  75.         switch (type) {
  76.             case 0:
  77.                 execute_shell_builtin(command_array);
  78.                 break;
  79.             case 1:
  80.                 execute_command(command_array);
  81.                 break;
  82.             default:
  83.                 status = 0;
  84.                 break;
  85.         }
  86.     }
  87.     while(status);
  88. }
  89.  
  90. void setup_environment(){
  91.     /*cd(Current_Working_Directory)*/
  92. }
  93.  
  94. void on_child_exit(){
  95.     /*reap_child_zombie()
  96.     write_to_log_file("Child terminated")*/
  97. }
  98.  
  99. void parent_main(){
  100.     /*register_child_signal(on_child_exit());*/
  101.     setup_environment();
  102.     shell_loop();
  103. }
  104.  
  105. int main() {
  106.     shell_loop();
  107.     return 0;
  108. }
  109.  
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement