Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in UNIX Design and implement a C/C++ program (a3part3.c) to implement a simple s

ID: 3740293 • Letter: I

Question

in UNIX

Design and implement a C/C++ program (a3part3.c) to implement a simple shell.

Warning: you should not use any "system" call .

Your program will run in a loop: (1) get the input from the user, (2) parse it, (3) for each command in pipe, set it for pipe, set each file-redirection specified for the command, fork a process to run it (via exec), and (4) back to the loop for next command.

If the command is "exit", then the program terminates.

Task1. Three or More Commands in Pipe

You may use the sample code provided to handle two commands in pipe, to make a shell.

Your shell handles one command, or two or three commands in pipe, for example,

            ls /etc | grep "passwd" | sort

            ls /etc | grep "*.c" | sort > a3out.txt

            ls | grep ".c" | sort | wc –l > a3out.txt

            or more commands in pipe

For example, if there are three commands in pipe, the parent gets the command (for example, "ls /etc | grep passwd | sort") to recognize that there are three commands piped together. Thus the parent forks a child (to do sort – the last command). The child will create a pipe to be shared, and then fork a child process (to do grep – the command in the middle of the command-pipe) which will fork its child process (to do ls – the first command) with the pipe shared (so that the output of one process doing "ls" will be the input to the process doing grep command). Meanwhile the parent waits for the child processes (doing the last command in the pipe sequence) to be terminated, and then back to the loop for the next command from the user.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>

static char* args[512];
pid_t pid;
int command_pipe[2];
static void waiting(int n);

static int command(int input, int first, int last)
{
   int fd[2];
    int flag=0;
   pipe( fd );  
   pid = fork();

    if (pid == 0) {
       for(int i=0;args[i]!=0;i++)
       {
          if(args[i][0]=='>')
                {
                   fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644);
                   flag=1;
                }
            if(args[i]=='>>' )
                {
                   fd[1]=open(args[i+1], O_APPEND|O_WRONLY, 0644);
                   flag=1;
                }
       }
       if (first == 1 && last == 0 && input == 0) {
           //fprintf(stderr, "here ");
           dup2( fd[1], 1 );
       } else if (first == 0 && last == 0 && input != 0) {
           dup2(input, 0);
           dup2(fd[1], 1);
       } else {
           if(flag==1)
            {
               dup2(fd[1],1);
            }
           dup2( input, 0 );
       }
         if(flag==1)
       {
                if(strcmp(args[1],">>")==0)
                   execlp(args[0], args[0], 0, (char *)0);
                else if(strcmp(args[1],">")==0)
                   execlp(args[0], args[0], 0, (char *)0);
                else
                   execlp(args[0], args[0], args[1], (char *)0);
       }
       else if (execvp( args[0], args) == -1)
           exit(1);
   }
  

   if (input != 0)
       close(input);
    close(fd[1]);
    if (last == 1)
       close(fd[0]);
   return fd[0];
}

static int run(char* cmd, int input, int first, int last);
static char line[1024];
static int n = 0; /* number of calls to 'command' */

int main()
{
   printf("SIMPLE SHELL: Type 'exit' to exit. ");
   while (1) {
       /* Print the command prompt */
       printf("$$$ ");
       fflush(NULL);

       /* 0 a command line */
       if (!fgets(line, 1024, stdin))
           return 0;

       int input = 0;
       int first = 1;

       char* cmd = line;
       char* next = strchr(cmd, '|'); /* Find first '|' */

       while (next != NULL) {
           /* 'next' points to '|' */
           *next = '';
           input = run(cmd, input, first, 0);

           cmd = next + 1;
           next = strchr(cmd, '|'); /* Find next '|' */
           first = 0;
       }
       input = run(cmd, input, first, 1);
       waiting(n);
       n = 0;
   }
   return 0;
}

static void tokenize(char* cmd);

static int run(char* cmd, int input, int first, int last)
{
   tokenize(cmd);
   if (args[0] != NULL) {
       if (strcmp(args[0], "exit") == 0)
           exit(0);
       n += 1;
       return command(input, first, last);
   }
   return 0;
}

static char* skipspace(char* s)
{
   while (isspace(*s)) ++s;
   return s;
}

static void tokenize(char* cmd)
{
   cmd = skipspace(cmd);
   char* next = strchr(cmd, ' ');
   int i = 0;

   while(next != NULL) {
       next[0] = '';
       args[i] = cmd;
       ++i;
       cmd = skipspace(next + 1);
       next = strchr(cmd, ' ');
   }

   if (cmd[0] != '') {
       args[i] = cmd;
       next = strchr(cmd, ' ');
       next[0] = '';
       ++i;
   }

   args[i] = NULL;
}

static void waiting(int n)
{
   int i;
   for (i = 0; i < n; ++i)
       wait(NULL);
}