Computer Club MNNIT
Would you like to react to this message? Create an account in a few clicks or log in to continue.

write your own shell

2 posters

Go down

write your own shell Empty write your own shell

Post  Admin Tue Sep 23, 2008 1:56 pm

you must have work on bash ksh ,sh shells on linux
now it's time for to write your own shell are you ready ??
You are required to write a C program which accepts a UNIX/Linux
shell command as an input and deliver output of that command on
the same x-terminal. Your program must support the following:
1. The internal command ‘exit ’which terminates the shell.
2. A command with no arguments (Example: ls)
3. A command with arguments (Example: ls -l)
4. A command, with or without arguments, whose output is redi-
rected to a file (Example: ls -l > some file)
5. A command, with or without arguments, whose input is redi-
rected from a file (Example: sort < testfile)
Question

Admin
Admin

Posts : 39
Join date : 2008-08-03

https://computerclub09mnnit.forumotion.net

Back to top Go down

write your own shell Empty Re: write your own shell

Post  ankurgutpa Tue Sep 30, 2008 4:46 pm

Code:

#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>


int main(){
   char buf[1000],ifile[10],ofile[10],cmd[20];
   char *myargv[10];
   int i,j,k,ifd,ofd,pfd1[2],pfd[2],len;
   pid_t pid;
   int status;
   
   printf("[MY-SHELL]$ ");
   fflush(stdout);
   
   while(  fgets(buf,1000,stdin) != NULL ) {
      if( buf[ strlen(buf) - 1 ] == '\n' )
         buf[ strlen(buf) - 1 ] ='\0';
      
      if(strcmp( buf , "exit" ) == 0)
         break;
   
      if( ( pid = fork() ) < 0 ) {
         perror( "fork" );
         exit (1);
      }
      
      //FORK SUCCESS ,CHILD PROCESS
      else if( pid == 0) {
         i=0;j=0,k=0;
         len=strlen(buf);
         
         //PARSING THE COMMAND
         while( i < len ) {
            if( ' ' == buf[i] )
               i++;
            
            if( '<' == buf[i] || '>' == buf[i] )
               break;
            
            if( '-' == buf[i] || isalpha(buf[i]) ) {
               myargv[k++]=&buf[i];
               i++;
               while( ' ' !=  buf[i]  && '<' != buf[i] && '>' != buf[i] && i < len)
                  i++;
               if( i == len )
                  break;
               else {
                  i+=1;
                  buf[i-1]='\0';
                  continue;
               }
            }
         }
         
         myargv[k]=NULL;
         
         //REDIRECTION OF I/P OR O/P
         if(i<len){
         
         
         //FOR I/P REDIRECTED FILE
            if( '<' == buf[i] ) {
               i++;
               j=0;
               if( ' ' == buf[i] )
                  i++;
               while( ' ' != buf[i] && i < len )
                  ifile[j++]=buf[i++];
               ifile[j]='\0';
            
               if ( ( ifd = open(ifile,0) ) < 0 ){
                  perror("input redirected file");
                  exit( 1 );
               }
            
               close(0);
               dup(ifd);
            }
            //FOR O/P REDIRECTED FILE
            else if( '>' ==  buf[i] ){
               i++;
               j=0;
               
               if( ' ' == buf[i] )
                  i++;
               
               while( ' ' != buf[i] && i < len )
                  ofile[j++]=buf[i++];
               ofile[j]='\0';
            
               
               if ( ( ofd = open(ofile,1) ) < 0 ){
                  perror("output redirected file");
                  exit( 1 );
               }
            
               close(1);
               dup(ofd);
            }
         }
               //myargv[0]='\0';
               
         //EXECUTE THE COMMAND
         execvp( myargv[0], myargv);
         fprintf(stderr,"execvp error\n" );
         exit(127);
      }
      
      //PARENT PROCESS
      else {
         if( ( pid = waitpid(pid,&status,0) ) < 0 ){
            perror("waitpid");
            exit(1);
         }
         printf("[MY-SHELL]$ ");
         fflush(stdout);
      }
   }
   return 0;
}

I'm hoping this code will meet all given requirements.In case not , tell me.
@ Admin
two questions:
1.How can we take care of pipe. 1 pipe ,i can handle but what if user use more than 1 pipe
2.How to execute internal shell commands like "cd" ? cheers
ankurgutpa
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 36
Location : Tandon 72

Back to top Go down

write your own shell Empty Re: write your own shell

Post  Admin Wed Oct 01, 2008 1:54 am

internal commands are written in shell itself for eg cd can be done using chdir(2) system call ........
just use dis system call for cd and many more are available for other internal commands .......

Admin
Admin

Posts : 39
Join date : 2008-08-03

https://computerclub09mnnit.forumotion.net

Back to top Go down

write your own shell Empty Re: write your own shell

Post  ankurgutpa Wed Oct 01, 2008 2:17 am

ok thnx
and what about multiple pipes?
ankurgutpa
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 36
Location : Tandon 72

Back to top Go down

write your own shell Empty Re: write your own shell

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum