Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will add support for i/o redirection and piping to the shell code given below. That is to say, >, file.txt cat

For this assignment, you will add support for i/o redirection and piping to the shell code given below. That is to say, >, <, and |.

For example, your shell should now support commands like this :

ls -l > file.txt

cat file.txt | wc > file.txt

sort < file.txt

Your shell does not need to support "chained" piping. That is your shell is not required to do this:

ls | sort | wc | head

CODE:

Main.c (sh.c):

#include #include "parse.h" #include "runCommand.h" //main function int main(void) { //declare char variable for line char l[1024]; //declare char variable for the command line argument char *argv[64]; //ask the command until done while (1) { //print the given string printf("Enter command -> "); //read the command from user fgets(l,sizeof(l),stdin); //call parse function parse(l, argv); //if the command is exit, then program terminate if (strcmp(argv[0], "exit") == 0) return 0; //otherwise call runCommand function //to execute the command runCommand(argv); } }

parse.h:

#ifndef PARSE_H #define PARSE_H #include "string.h" //function for parse static void parse(char* input, char* args[]) { int i = 0; //fgets reads the , so overwrite it input[strlen(input)-1] = '\0'; //get the first token args[i] = strtok( input, " " ); //get the rest of them while((args[++i] = strtok(NULL, " "))); } #endif

runCommand.h:

#ifndef RUNCOMMAND_H #define RUNCOMMAND_H #include #include #include #include #include"parse.h" //function for runCommand void runCommand(char **argv) { pid_t pid; //declare int variable for status int st; //for fork a child process if ((pid = fork()) < 0) { printf("forking child process failed "); return; } else if (pid == 0) { //if the command is invalid, and then displays the given messgae if (execvp(*argv, argv) < 0) { printf("exec failed "); return; } } else { while (wait(&st) != pid); } } #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

3 How supply and demand together determine market equilibrium.

Answered: 1 week ago