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;
 }
reuben
  • 3,360
  • 23
  • 28
Damian
  • 63
  • 2
  • 9

2 Answers2

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.

user2485710
  • 9,451
  • 13
  • 58
  • 102
  • I am absolutly green in Linux and C. Can you give any example? – Damian Dec 14 '13 at 20:46
  • @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 http://stackoverflow.com/questions/5823838/flex-bison-and-c-looking-for-a-very-basic-introduction ! – user2485710 Dec 14 '13 at 20:48
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.

unxnut
  • 8,509
  • 3
  • 27
  • 41
  • Where the program will know when to use and when not to fork execvp – Damian Dec 14 '13 at 22:16
  • 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. – unxnut Dec 15 '13 at 00:08