We help IT Professionals succeed at work.
Get Started

Unix shell

halt1001
halt1001 asked
on
308 Views
Last Modified: 2010-05-18
Im doing an assignment, programming in C, in Unix.
The task is, stimulating a shell.
It should accept any command entered.

I have done it, and so far it could run commands entered from the prompt.

However, the problems are:

1. No error returning back when an invalid command entered.

2. Another task is, the pipe operator. I dont know how to do this. Any1 know
how to make it accept pipes like in normal shell? For example:

command1 paratemeters | command 2 ....

Thanks for reading.

Alan.




-----------------------------------------
Here's the code at the moment:
-----------------------------------------

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main();
void parent(int pid);
void child();

char command[100];

int main() {
 int ret;

 do {
      printf("alan_ass2$");
      fgets(command,100,stdin);

      if ( strncmp(command,"logout\n",8) == 0 )
       exit(1);
      if ( strncmp(command,"\n",1) == 0 )
       continue;

      if( (ret=fork()) > 0 )
           parent(ret);
      else if ( ret == 0 )
           child();
      else
           perror("fork() failed");
     } while (1);

}

void parent(int pid)
{
    int childs_exit_val;

 /*
 ** The parent only invokes the wait() if the command is a foreground
command,
 ** i.e. there is no '&' at the end of the line
 */
}

void child()
{
     char *argv[100]; /* 100 is arbitrarily long enough for this assignment
*/
     char *tmp;
     int cnt,i;

     cnt = -1;
     printf("\n");
            tmp=strtok(command, "\n");
            tmp=strtok(tmp, " ");
            while ( tmp != NULL )
            {
                    argv[++cnt]= tmp;
                    tmp=strtok(NULL," ");
            }

     if ((strncmp(argv[0],"cd",2)==0)&&(strlen(argv[0])==2))
          argv[0]="chdir";

     for (i=0; i<=cnt; i++) {
          printf("argv[%d] = '%s'\n", i, argv[i]);
     }

     argv[cnt+1]=NULL;

     execvp(argv[0], argv);

 /*
 ** If we arrive here, an error must have occurred otherwise a different
 ** program image would be active.
 */
    perror("execvp");
    exit(1); /* must be careful to exit a child process */
}




Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE