0

I must write a command interpreter in C. It must:

  • handle command options and parameters
  • support commands without options and parameters
  • allow redirecting one command to another (for example: ls -a | wc) with a max of 3 redirects

We can also assume a fixed maximum of arguments (say MAXARG).

At this moment I have written this code, but I do not know why some commands do not work (e.g. cd). Please help me.

#include <stdio.h>
#include <string.h>
#include <unistd.h>

void  parse(char *line, char **argv)
{
 while (*line != '\0') {       
      while (*line == ' ' || *line == '\t' || *line == '\n' || *line=='|')
           *line++ = '\0';     
      *argv++ = line;         
      while (*line != '\0' && *line != ' ' && 
             *line != '\t' && *line != '\n') 
           line++;            
 }
 *argv = '\0';               
 }


 int main(int argc,char *argv[])
  {
  int child_pid;
  char * arg[10];       
   int child_status;
  char str[10];
  do
  {
  printf(">> ");
  gets(str);
  parse(str,arg);
  if(!strcmp(arg[0],"end"))
  {
    break;
   }
  child_pid = fork();
  if(child_pid == 0)
  {
    execvp(*arg,arg);
    printf("Unknown command\n");


   }
   else 
  {
    wait(&child_status);
    printf("koniec\n");
  }
 }
  while(1==1);
    return 0;
 }
1
  • I must write Command Interpreter in C I thought Pinochet was dead Commented Dec 14, 2013 at 20:40

2 Answers 2

2

you need a parser/lexer to inspect your input, such tools are already available, a few names: YACC, Bison, Flex .

Regarding your <command> | <command> situation that is a pipe and is a functionality provided by the linux kernel space and if you want that you need to code using the linux kernel api for the pipes.

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

2 Comments

I am absolutly green in Linux and C. Can you give any example?
@user2900036 you just have to search, those are among the most popular topics when programming under linux/unix, you will find anything with any search engine, even on SO stackoverflow.com/questions/5823838/… !
2

For cd, you should not do a fork/exec because then, you will only change the working directory of the child which will not be the same as changing the working directory of the parent. For a command like cd, you must use chdir(2) after parsing it without doing a fork.

2 Comments

Where the program will know when to use and when not to fork execvp
That is for you to decide when you analyze the problem. It may help to study how the current shells are implemented. The shells have a lot of builtin commands and as the programmer, you will have to decide which ones to do without going through fork/exec. For example, the arithmetic functions, control functions (if/then/for/while/case) are not to be implemented using the fork/exec mechanism. Any user command for which you have binary executable will typically be implemented with fork/exec.

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.