2 Replies - 10206 Views - Last Post: 18 February 2011 - 12:22 PM Rate Topic: -----

#1 lindokuhlec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-February 11

shell

Posted 18 February 2011 - 11:24 AM

/* am having problem with shell in c but i need to add history nd cd .. dont know how to can any help


#include <sys/dir.h>
#include <sys/param.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>




/*  FUNCTION :                                                                                                   
*    pass_to_token() : its a voind it doesn't return anything ,Its takes an input line and parse it into tokens.
*  It first replaces all white spaces with zeros until it hits a non-white space character which indicates      *                                               
*  the beginning of an  argument.  It saves the address to argv[], and then skips all non-white spaces          
*  which constitute the argument.
                                                                               

      void  pass_to_token(char *take, char **argv){

                  while (*take != '\0') {       				/* if not the end of take . */
                     while (*take == ' ' || *take == '\t' || *take == '\n')
                       *take++ = '\0';     					/* replace white spaces with 0    */
                       *argv++ = take;    					/* save the argument position     */
                      while (*take != '\0' && *take != ' ' &&  
                             *take != '\t' && *take != '\n') 
                              take++; 						/*increment*/

                        }
                    *argv = '\0'; 						 /* mark the end of argument list  */
               }

/*
*  FUNCTION Process:                                                                                  
*  This function receives a command line(panda) argument list with the first one being a file name   
*  followed by its arguments Then,this function forks a child process to execute the command         
*  using system call execvp() method                         
*/
     
void  Process(char **argv){

             pid_t pid=fork();
             int status;
     
         if (pid  < 0) { 				/*fork a child process  if fork return the negetive number it means 								   the is an error */
             fprintf(stderr,"command not found\n");
              exit(1);
  
             }

         else if (pid == 0) {          			/*for the child process: */
                   if (execvp(*argv, argv) < 0) {     	/*execute the command  */
                   fprintf(stderr,"command not found\n");
                   exit(1);

                  }
                 }
                     					/* Parent   */   
            else {					/* else if not error do while*/
                  					/*  while (wait(&status) !=pid)* wait to completion  */

                      waitpid(-1,&status,0);
      
               
            }
       }


/* This is the main programme its calls the function1 the process() that forks a child process to execute *  *the                              
* command using system call execvp() method,And the seond funtion 2: the pass_to_token() that  *takes                                       
* an input line and parse it into tokens. It saves the address to argv[], and then skips all non-white
* spaces                             
* It also decleares an arrays of char  with a cetain boundary,the is a while(1) it repeat until its completes  and the else call Process()
*/
    
 void  main(void)
{
     char take [1000];           		  /* the input line*/
     char  *argv[64];            		  /* the command line argument*/
     
     while (1) {                 		  /* repeat until done*/
          printf("joy> ");     			  /*   display a prompt*/
          gets(take);              		  /* read in the command line */
         pass_to_token(take, argv);               /*parse the line*/
          if (strcmp(argv[0], "exit") == 0)       /* is it an "exit"?*/
               exit(1);            	          /* exit if it is */
           Process(argv);                         /* otherwise, execute the command */
     }
}						   /* End of our programme called shell.c */     



edit by ishkabible: please use code tags when posting code

This post has been edited by ishkabible: 18 February 2011 - 12:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: shell

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: shell

Posted 18 February 2011 - 12:04 PM

you must tell us what is happening, post any errors you are receiving, explain what is actually happening and state what you think should happen in contrast to what is actually happening when the code is run/compiled.
Was This Post Helpful? 0
  • +
  • -

#3 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: shell

Posted 18 February 2011 - 12:22 PM

Please indent your code. It is not at all readable.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1