Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me implement this promt of imput and output in the code I have so far, please? Unix shell environmnet- program is written

Can someone help me implement this promt of imput and output in the code I have so far, please?

Unix shell environmnet- program is written in C

Q:

Your shell should then be modified to support the > and < redirection operators, where > redirects the output of a command to a file and < redirects the input to a command from a file. For example, if a user enters osh>ls > out.txt the output from the ls command will be redirected to the file out.txt. Similarly, input can be redirected as well. For example, if the user enters osh>sort < in.txt the file in.txt will serve as input to the sort command.

Managing the redirection of both input and output will involve using the dup2() function, which duplicates an existing file descriptor to another file descriptor. For example, if fd is a file descriptor to the file out.txt, the call dup2(fd, STDOUT_FILENO); duplicates fd to standard output (the terminal). This means that any writes to standard output will in fact be sent to the out.txt file.

You can assume that commands will contain either one input or one output redirection and will not contain both. In other words, you do not have to be concerned with command sequences such as sort < in.txt > out.txt.

My code:

#include #include #include #include #include

#define MAX_LINE 80 /*The maximum length command*/

int main(void) { char *args[MAX_LINE/2+1];/*command line arguments*/ int should_run=1;/*flag todetermine when to exit program*/ while(should_run) { /*Shell promt*/ printf("osh>"); fflush(stdout); char command[MAX_LINE+1]; fgets(command, MAX_LINE+1 , stdin); // read and store command line input if (!strcmp(command,"exit")) should_run = 0; else { pid_t pid; pid = fork(); if (pid == 0) { command[strlen(command)-1] = 0; char *token = strtok(command, " "); int i = 0; while(token!=NULL) { args[i] = token; token = strtok(NULL, " "); i++; } args[i] = NULL; execvp(args[0], args); exit(0); } else { if (command[strlen(command)-2] == '&') continue; else wait(NULL); } } } return 0; }

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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

What is the difference between law and ethics?

Answered: 1 week ago

Question

What is disclosure of cost method investment?

Answered: 1 week ago