Thread: shell question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    shell question

    Code:
    #include  <stdio.h>
    #include  <sys/types.h>
    
    void  parse(char *line, char **argv)
    {
         while (*line != '\0') {       /* if not the end of line ....... */ 
              while (*line == ' ' || *line == '\t' || *line == '\n')
                   *line++ = '\0';     /* replace white spaces with 0    */
              *argv++ = line;          /* save the argument position     */
              while (*line != '\0' && *line != ' ' && 
                     *line != '\t' && *line != '\n') 
                   line++;             /* skip the argument until ...    */
         }
         *argv = '\0';                 /* mark the end of argument list  */
    }
    
    void  execute(char **argv)
    {
         pid_t  pid;
         int    status;
    
         if ((pid = fork()) < 0) {     /* fork a child process           */
              printf("*** ERROR: forking child process failed\n");
              exit(1);
         }
         else if (pid == 0) {          /* for the child process:         */
              if (execvp(*argv, argv) < 0) {     /* execute the command  */
                   printf("*** ERROR: exec failed\n");
                   exit(1);
              }
         }
         else {                                  /* for the parent:      */
              while (wait(&status) != pid)       /* wait for completion  */
                   ;
         }
    }
    
    void  main(void)
    {
         char  line[1024];             /* the input line                 */
         char  *argv[64];              /* the command line argument      */
    
         while (1) {                   /* repeat until done ....         */
              printf("Shell -> ");     /*   display a prompt             */
              gets(line);              /*   read in the command line     */
              printf("\n");
              parse(line, argv);       /*   parse the line               */
              if (strcmp(argv[0], "exit") == 0)  /* is it an "exit"?     */
                   exit(0);            /*   exit if it is                */
              execute(argv);           /* otherwise, execute the command */
         }
    }
    I wrote that stuff,now I need to add some stuff to it.
    like ';' which seperates different command from each other ex: ls;ls would give you print the result of ls 2 times.
    and '>' as standard output redirection.
    etc...

    NOw I don't know how to fit that stuff in it.
    THe only thing I can think off is to rewrite the parser function.

    Help would be appreciated.
    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Maybe all you need is a function that breaks up the line, obtained by gets(), into commands separated by a semi-colon before feeding it to the parser.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should not be using gets at all: http://cpwiki.sourceforge.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Also,
    Code:
    void main(void)
    is bad.
    here's why
    Sorry to be a bit off topic I know it's not related to your question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #!/bin/bash question
    By elsheepo in forum C Programming
    Replies: 10
    Last Post: 03-08-2009, 11:42 PM
  2. shell execution question
    By Overworked_PhD in forum Linux Programming
    Replies: 0
    Last Post: 10-19-2008, 08:52 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Virtual Folder Question (Shell)
    By The Dog in forum Windows Programming
    Replies: 1
    Last Post: 04-03-2003, 03:18 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM