Question

In: Computer Science

You are required to revise function shell_execute() in simple-execute.c, so the program can execute commands with...

You are required to revise function shell_execute() in simple-execute.c, so the program can execute commands with up to two pipes. Basically, your program should be able to handle the case in which there is at least one space before or after “|” such as:


$$$ ls -l | grep D | wc -l
$$$ ls -l | grep D


If there is no space between or after “|”, for example, for the following case:
$$$ ls|

“ls|” is treated as one argument to be executed (rather than “ls” and “|”)

The simple-execute.c code:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

int shell_execute(char ** args, int argc)
{
   int child_pid, wait_return, status;

   if ( strcmp(args[0], "EXIT") == 0 )
       return -1;
  
   if( (child_pid = fork()) < 0 )
   {
       printf("fork() error \n");
   }
   else if (child_pid == 0 )
       {
       if ( execvp(args[0], args) < 0){
           printf("execvp() error \n");
           exit(-1);
       }
   }else
   {
       if ( (wait_return = wait(&status) ) < 0 )
           printf("wait() error \n");
   }
          
   return 0;
}

Solutions

Expert Solution

First create a file named file.sh and execute the command:

chmod 777 file.sh

The code for shell_execute will be as follows:

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

int shell_execute(int argc, char *args[])
{
   int child_pid, wait_return, status,i;
   if ( strcmp(args[0], "EXIT") == 0 )
       return -1;
   if( (child_pid = fork()) < 0 )
   {
       printf("fork() error \n");
   }
   else if (child_pid == 0 )
       {
             FILE *f=fopen("file.sh","w");
   fprintf(f,"#!/bin/sh\n");
             for(i=1;i<argc;i++)
            {
                   fprintf(f,"%s\nexit",args[i]);
            }
             fclose(f);
       if ( execl("file.sh","file.sh",NULL) < 0){
           printf("%s\n",args[1]);
                 exit(-1);
       }
   }else
   {
       if ( (wait_return = wait(&status) ) < 0 )
           printf("wait() error \n");
   }
        
   return 0;
}


Related Solutions

Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
Question: As the senior management team of the company, you are required to revise the budget...
Question: As the senior management team of the company, you are required to revise the budget for 2020 to take into account the impact of recession can I have the answer for this question
1) Write a simple python function program that can calculate the volumes of following shape: cylinder,...
1) Write a simple python function program that can calculate the volumes of following shape: cylinder, circle, cone and sphere. For each of this shape, write a separate function. Let your function take in 2 inputs only. Your program should prompt user to enter these 2 inputs and print out the results of the volumes of the shape separately?
Euler’s Method and Introduction to MATLAB • Start MATLAB • Inline Commands: You can type commands...
Euler’s Method and Introduction to MATLAB • Start MATLAB • Inline Commands: You can type commands directly into the command window: Type and expression and then hit enter to evaluate the expression. For example: >> 2+2 If you want to suppress the output of a command follow it with ‘;”.  For example >>2+2; Practice evaluating a few expressions in the command window. (In MATLAB multiplication is represented by * so 3*2=6). • Variables and Vectors: You can define variables in the...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you...
For this assignment you will be writing a program that evaluates some PostScript® commands. Specifically, you will be using a special version of a linked-list, called a Stack. Stacks are linear structures that are used in many applications. In fact, Java's runtime environment (JRE) uses a stack to evaluate Java's byte-code. You can learn more about Stacks from this URL: Tutorialspoint - Stack (Links to an external site.) The stack description above uses an array implementation for examples. You will...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT