Find the answer to your Linux question:
Results 1 to 2 of 2
I am trying to learn Unix C programming and doing some exercises for practice. I am currently working on writing my own shell that works similar to the linux bash ...
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    1

    implement pipe in unix shell

    I am trying to learn Unix C programming and doing some exercises for practice. I am currently working on writing my own shell that works similar to the linux bash shell.

    The code I have below provides for a fairly basic shell. It supports I/O redirection. I am trying to add support for piping. Initially, I just want to add support for a single pipe.

    I have tried to go through some tutorials online but can't quite figure out where I am going wrong. I am missing something but not sure what.

    Here is what I have so far.

    Code:
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    
    #define TRUE 1
    
    int main(void)
    {
      char *arg_list[10];
    
      int status;
    
      //counter for tokenizing/parsing
      int x = 0;
      
      //counter for while loop to find input file, output file, etc
      int y = 0;
    
      pid_t pid;
      char buf[100];
    
      char inFile[10];
      char outFile[10];
    
      int fdIn, fdOut;
    
      int fd[2];
    
      pipe(fd);
    
      while(TRUE)
      {
         printf("> ");
         if (!fgets(buf, 100, stdin))
           return 0;
    
        pid = fork();
        switch(pid)
        {
          case -1:
          return 1;
       
          case 0:
          {
            arg_list[x] = strtok(buf, " \n");
                
            x = 0;
             
             while(arg_list[x] != NULL)
             {
               counter++;
               arg_list[x] = strtok(NULL, " \n");
             }
                
             y = 0;
             
             while(arg_list[y] != NULL)
             {
               if(!strcmp(arg_list[y], "<"))
               {
                 if(arg_list[y + 1] != NULL)
                 {
                   fdIn = open(arg_list[y + 1], O_RDONLY);
                   dup2(fdIn, STDIN_FILENO);
                 }
                 else
                 {
                   printf("No input file specified");
                 }
                 arg_list[y] = 0;
               }
               else if(!strcmp(arg_list[y], ">"))
               {
                 if(arg_list[y + 1] != NULL)
                 {
     fdOut = open(arg_list[y+1], O_CREAT | O_WRONLY | O_TRUNC, 0666);
                   dup2(fdOut, STDOUT_FILENO);
                 }
                 else
                 {
                   printf("No output file specified");
                 }
                 arg_list[y] = 0;
               }
               else if(!strcmp(arg_list[y], "|"))
               {
                 //is the code in this "if else" correct?
    				 //or should something else be done when the
    				 //pipe character is seen?
    				 if(arg_list[y + 1] != NULL)
                 {
                   close(fd[1]);
    
                   if(fd[0] != STDIN_FILENO)
                   {
                     if(dup2(fd[0], STDIN_FILENO) == -1)
                     {
                       perror("dup2");
                       exit(1);
                     }
                     close(fd[0]);
                   }
    
                 }
                 arg_list[y] = 0;
               }
    
               y++;
             }
    
            execvp(arg_list[0], arg_list);
            break;
          }
    
          default:
          waitpid(-1, &status, 0);
          close(fd[0]);
    
          if(fd[1] != STDOUT_FILENO)
          {
            if(dup2(fd[1], STDOUT_FILENO) == -1)
            {
              perror("dup2");
              exit(2);
            }
            close(fd[1]);
          }
    
          break;
        }
      }
      
      return 0;
    }
    If somebody could help with this, it would be greatly appreciated.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Just a thought , W. Richard Stevens Unix book (don't remember the exact book name) has complete code for shell implementation . That might be helpful.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...