Question: Introduction In this assignment you will be writing more complicated wrapper programs around external programs. Due: One week Deliverables: Commit and push to Github. Submit
Introduction
In this assignment you will be writing more complicated wrapper programs around external programs.
Due: One week
Deliverables: Commit and push to Github. Submit to Gradescope.
Assignment Details
There are two parts to this assignment.
Background
In this assignment, you will learn how the shell executes various commands. As part of this, we'll learn some new commands to test our implementation.
The echo command prints each item in the argument list.
The sort command allows you to sort a csv file by columns. For example, the following command sorts the file by using comma as the token to separate columns, and sort using the first column, and using numerical sort.
$ usrbinsort tkn testscitiescsv
The cut command cuts a CSV file using the comma and extract the first column yeah the two commands use t and d k and f for the same things
$ usrbincut df testscitiescsv
The head command extract the first N lines. So this example prints the first lines.
$ usrbinhead n testscitiescsv
The tail command is the opposite. This one gets the last line.
$ usrbintail n testscitiescsv
Part A
For this part, you will write a simple commandline interface. The program will accept several command line arguments. The first argument will be the command to run, and the remainder will be the arguments to the command. For example:
buildsrcpartamain usrbinecho
will run the echo command without any arguments using our program, whereas
buildsrcpartamain usrbinecho hello boston ma usa
will run the echo command with four arguments.
To run the tests for this part, run:
runsh testa
Part B
For this part, you will extend the program from Part A The program has two modes. In the first mode, it will run the same as Part A
The second mode contains a colon character in part of the arguments. This character will split the arguments into two parts. The first part will run the first command, and the second part will run another command. The two commands will be connected via pipe. For example:
$buildsrcpartbmain usrbinhead n testscvscsv : usrbintail n
It's equivalent to the command:
$ usrbinhead n testscvscsv usrbintail n
Notice we're replacing the pipe character with the colon character. This pair of commands will first extract the top lines from the given file, and then extract the last line from that. In other words, it will print the th line.
For this to work, there are a number of steps you need to make. First, you need to check if you're running in mode or mode This can be determined by checking if : exists in as an argument. If it doesn't exist, you're in mode and you can reuse the code from Part A
If not, you're in mode Create a pipe and then fork a child process. The parent process will be the writer, so it should close its read end. It should also redirect STDOUT to the write end of the pipe:
closereadfd;
dupwritefd STDOUTFILENO;
exec;
The child process will be the reader. It should close its write end of the pipe and convert the pipe's read end into the standard input:
closewritefd;
dupreadfd STDINFILENO;
exec;
Helper Functions
To help you on the assignment, there are helper functions in srcpartbc and some unit tests in teststestbcpp
The first helper function is the splitargs function. This function takes in arguments, the argc, and the argv of the program itself. It also accepts other arguments, args and args These are pointers to malloc'd arrays of strings char pointers You task is to skip the first argument argv and look for the colon : Everything before the colon is copied to args and everything after to args For example, if you run:
splitargs "partb", "echo" args args;
args will contain a string array that contains "echo" and NULL, and args will contain NULL.
On the other hand if you run:
splitargs "partb", "echo", : "sort" args args;
args will contain a string array that contains "echo" and NULL, and args will contain "sort" and NULL.
The second helper function runpipe takes in pairs of arguments. Each pair contains the command to run and the arguments, which are then passed to the exec system call. In this function, create the pipe, child process, and rewire the STDINSTDOUT and exec.
To run the tests for this part, run:
runsh testb
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
