Friday, July 20, 2012

My Own Shell for Linux

         Do you want to publish source codes in your blog or web site as follows.
                              Visit Source Code Formatter

 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <string.h>  
 #include <errno.h>  
 #include <sys/types.h>  
   
 /*method to parse thi input to argv and store null  
   point of the white spaces of the input*/  
 void parse(char *input, char **argv){  
   while (*input != '\0') {  //  
        while (*input == ' ' || *input == '\t' || *input == '\n'){  
             *input++ = '\0';  
     }  
        *argv++ = input;  
        while (*input != '\0' && *input != ' ' && *input != '\t' && *input != '\n'){  
             input++;  
     }  
   }  
      *argv = '\0';  
 }  
   
 //check method to check the input  
 int check(char *input){  
   int i = 0;  
   for(i = 0; i < strlen(input); i++){  
      if(input[i] == '|'){  //if input[i] is |(pipe)  
       return i;  //return i  
     }  
   
   }  
   return 0;  //otherwise return 0  
 }  
   
 //execute the command if there is pipe  
 int pipexec(char input[32],int i){    
   char *input2 = &input[i + 1];  //assign the address of input[i+1] to input2 pointer  
   input[i] = '\0';    //assign input[i] as a null point  
   int pid[2];    
   int retvalexec;      
           
   if(pipe(pid) == -1){    //if pipe is not created  
     perror("pipe call error");  
     exit(1);       
   }  
     
   char *arr[32];  
   if(fork()){    //create a new process and if it is parent  
     close(1);  //close the stdin of that process  
     dup(pid[1]);  //duplicate that to pid[1]  
     close(pid[0]);  //close the stdou of pid  
     parse(input, arr); //method calling  
     retvalexec=execvp(arr[0], arr); //execute and return the value to the retvalexec  
     if(retvalexec) {  //if retvalexec is not 0  
           puts(strerror(errno)); //print string of the error number   
           exit(0);    //exit  
         }   
           
   }else{    //if it is child process  
     close(0);  //close the stdou of that process  
     dup(pid[0]);   //duplicate that to pid[0]  
     close(pid[1]);  //close the stdin of pid  
     parse(input2, arr);  //method calling  
     retvalexec=execvp(arr[0], arr);  //execute and return the value to the retvalexec  
       
     if(retvalexec) {  //if retvalexec is not 0  
           puts(strerror(errno)); //print string of the error number   
           exit(0);    //exit  
         }                    
   }          
   return 0;  
 }  
   
 //execute normally if there is not pipe  
 int execute(char input[32],char *argv[32]){  
   parse(input, argv); //method calling  
   if(execvp(argv[0], argv)!=0) {  //if retvalexec is not 0  
         puts(strerror(errno)); //print string of the error number   
         exit(0);    //exit  
      }    
   return 0;  
 }  
   
 //main method starts here  
 int main(){  
   char input[32];  //to store user input  
   char *argv[32];  //to store input and null points  
   pid_t pid;  
   
   printf("\n\n\n");  
     
   while (1) {    
     printf("MOSH $ ");  
     gets(input);  //get the user input to input char array  
   
     if (strcmp(input, "exit") == 0){  //compare the input  
             exit(0);      //if it is "exit" exit from mosh  
     }  
        pid = fork();    //create a new process and return the value to pid  
         
     if (pid){  //if parent process runs  
           wait(0);  //wait until child finished  
       }else{    //if child process runs        
       int i = 0;  
       i = check(input);  //check the input assign return value to i  
       if(i){      //if input has | symbol  
         pipexec(input,i);  
       }else{    //if input has not | symbol  
         execute(input,argv);                
       }  
       }  
     }  
     return 0;  //return 0 to sending program executed correctly  
 }  

0 comments:

Post a Comment