0

Can someone please suggest what is wrong with this program.I am trying to implement shell like feature by creating a child process here. On giving a command having single word like ls or pwd it works but command with multiple words like ls -lrt or who am i is not working. There is something silly mistake that I am doing but unable to debug.

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <iostream>
    #include <wait.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <cstdlib>
    #define BUFSIZE  200
    #define ARGVSIZE 40
    #define DELIM    "\n\t\r"

    int main ()
    {
      int i,n;
      char buf[BUFSIZE + 1] ;
      char * str = "Shell > ";
      char * clargs[ARGVSIZE] ;
      int returnstatus;
      for(;;)
      {
          n = 1;
          write(STDOUT_FILENO,str,strlen(str));

          read(STDIN_FILENO,buf,BUFSIZE);
          if(!strcmp(buf,"exit\n"))
          {
              perror("exit");
              exit(20);
          }

          clargs[0] = strtok(buf,DELIM);

          while((clargs[n] = strtok(NULL,DELIM)) != NULL)
              n++;

          clargs[n] = NULL;

          switch(fork())
          {
          case 0:
          if((execvp(clargs[0],&clargs[0])) < 0)
              exit(200);

          default:
          wait(&returnstatus);
          printf("Exit status of command : %d\n",WEXITSTATUS(returnstatus));
          for(int i =0; i <= n;i++)
              clargs[i] = "\0";

          for(int i =0; i < BUFSIZE+1;i++)
              buf[i] = '\0';
      }
  }

  return 0;

}

6
  • What do you mean by "is not working"? Commented May 25, 2012 at 18:58
  • @Thomas...its not working for commands having multiple words like "ls -lrt" or say "who am i" Commented May 25, 2012 at 18:59
  • "not working" in what way? What happens? Commented May 25, 2012 at 19:08
  • And what do you mean by "unable to debug"? Commented May 25, 2012 at 19:09
  • @OliCharlesworth...I was expecting that this should give the output when i run the command like ls -lrt or any other command. But its giving exit statis 200 in those cases where i give the command like "ps -aux" or any other command which has more than one word. I am unable to understand why this is not working Commented May 25, 2012 at 19:12

1 Answer 1

4

You don't have a space in DELIM.
When trying to run ls -lrt, you want to run the ls executable, with two arguments - ls and -lrt.
But your strtok wouldn't break ls -lrt in two. So you're actually trying to run a program called ls -lrt, and there's no such program.

Adding a space to DELIM should solve it.

Not that it won't be good enough for some cases. E.g. when running echo "a b", you want "a b" to be one parameter, because of the parenthesis. strtok would break it into two. A real shell does more complicated parsing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.