Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Handling File Redirection ( < input, > output, >> output-append). Design and implement C/C++ program (myfile.c) in a loop: (1) to prompt for a command,

Handling File Redirection (< input, > output, >> output-append).

Design and implement C/C++ program (myfile.c) in a loop: (1) to prompt for a command, (2) to read a command (from keyboard as you type), (3) to echo the command, (4) to fork a child process to process command while the parent process is waiting for child process to complete this part, and then (5) the parent is back to the command prompt for next command.

The commands are: read, write, and append, and exit. The following tasks (that is, test cases) are to be implemented.

Task1. When the command is "exit", then the program terminates with exit(0).

Task2. The "read" command will do a file-redirection for a file to be read and to be written to a temporary file. For example, "read < hello.c" will read a file "hello.c" and write it to a temporary file ("temp.txt").

Task3. The "write" command will do a file-redirection for a file ("temp.txt") to be read and written to a file. For example, "write > out1.c" will read the save temp file "temp.txt" and write it to the file ("out1.c").

Task4. The "append" command has a file-redirection for a file (temp.txt) to be appended to a file. For example, "append >> out2.c" will read the save temp file "temp.txt" and write it to the file ("out2.c").

Task5. You may have two commands with a pipe. For example, "read < hello.c | write > hello2.c" will read hello.c file and write it to hello2.c via temp.txt file.

Task6. Create a Makefile to build the executable for the codes in this part.

Task7. For version control, create a code repository using git, github, or SVN (or any version control tool that you are familiar with). If this is your first time trial, you may consider github tool (with GUI). Please check Korn Lecture08 (Week04 Activity).

** Copy and paste your program code here, Makefile to compile, and the program-run results (screenshots, session log, or the part of MobaXterm "save terminal text" file to show your program runs for each test case). Submit (1) this document and (2) a zip file containing all the files including source, executable, Makefile, etc. For Version Control (e.g., github), you may provide here a few screenshots to show that you have done the task.

Hint. You may use the following code as your basis for this part.

#include

#include

#include

#include

#include

#include

#include

#include

char *getinput(char *buffer, size_t buflen) {

printf("$$ ");

return fgets(buffer, buflen, stdin);

}

int main(int argc, char **argv) {

char buf[1024];

pid_t pid;

int status;

while (getinput(buf, sizeof(buf))) {

buf[strlen(buf) - 1] = '\0';

/* Place your code to check "exit". If so, then exit */

if((pid=fork()) == -1) {

fprintf(stderr, "shell: can't fork: %s ", strerror(errno));

continue;

} else if (pid == 0) {

/* child process to do each command

place your code here to handle read, write, append */

exit(EX_OK);

}

if ((pid=waitpid(pid, &status, 0)) < 0)

fprintf(stderr, "shell: waitpid error: %s ",

strerror(errno));

}

exit(EX_OK);

}

You may read more on fork() in APUE 8.3, on system() call in APUE 10.18, on shell in APUE 9.9

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

in java a subclass can only extend one class

Answered: 1 week ago

Question

A Sample for Coffee shop Business Plan in Nepal

Answered: 1 week ago

Question

2. Identify the employees who are included in the plan.

Answered: 1 week ago

Question

7. Discuss the implications of a skill-based pay plan for training.

Answered: 1 week ago