Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview I need to build on top of the code that I written in C, called smash.c. Here is that code: #include #include #include #include

Overview

I need to build on top of the code that I written in C, called smash.c. Here is that code:

#include #include #include #include

#define MAXLINE 4096

int main() { char *cmd; char buff[MAXLINE]; while (1) { fprintf(stderr, "$"); if (!fgets(buff, MAXLINE, stdin)) { buff[strlen(buff) -1] = '\0'; break; } if ((cmd = strtok(buff, " \t "))) { if (strcmp(cmd, "cd") == 0) { // the cd command char *arg = strtok(0, " \t "); if (chdir(arg) == -1) { fprintf(stderr, "error: %s does not exist ", arg); } getcwd(buff, MAXLINE); printf("%s ", buff); } else if (strcmp(cmd, "exit") == 0) { // the exit command break; } else { system(buff); } } }

return 0; }

Modular design checkpoint

I will need at least two functions not including main. Here are some examples of addional functions I may need. Can you also clean my main and create my cd and exit functions?

smash_get_input : a function which will handle getting the input from a user and split out the tokens.

change_my_dir : a function that handles the cd command directory and checks for errors.

exit_smash : a function that handles the exit command and cleans up any malloc'd data structures you may have used.

Capturing command history

For all commands that a user runs we want to save the string that the user typed. We will leverage the struct data type to accomplish this. Our struct will initially only have one member.

// HINT: You may need to create an array of cmd structs to capture multiple // commands. This struct will only hold 1 command as typed by the user. struct cmd{ char * cmd; // a bit of memory that will store your string } 

In addition to the data structure defined above You will also need to add in a history command that will iterate through the array of cmd structs and print out each string that was input by the user. This new command must be in its own file named history.c with a matching header file history.h. You should have appropriate prototypes defined in your header file as well as header guards.

Here is an example of the history command in bash.

[shane@Darwin-2 projects(master) ]$ history 1 cd foo 2 cd repos/ 3 ls 4 cd bsu/ 5 ls 6 cd CS253 7 ls 8 git status 9 cd exams/ 10 ls 11 cd ../projects/p0/ ... 501 history [shane@Darwin-2 projects(master) ]$ 

Public facing API

As stated above you are free to define and name any internal functions as you see fit. However when it comes to a public facing API you typically don't have such freedoms as header files are consumed by several stake-holders (internal and external). So we are going to partially dictate the contents of history.h. We have defined the names of the functions and we want you (the student) to define the prototypes that make the most sense according to your implementation.

init_history // Make sure all buffers (arrays) are initialized and ready to use.

This is equivalent to a constructor in Java.

add_history // adds command into a ```cmd struct``` and append to your history buffer

This is equivalent to a public method in Java.

clear_history // properly frees all memory and clears the history buffer

This is equivalent to a method in Java. Unlike Java, however, you are responsible for freeing any memory you malloc'ed (there is no garbage collector).

print_history // Prints the history to the command line (See the example above)

This is similar to a toString in Java, except it is not returning a String, it is just printing.

Adding to the number of smash built-in commands

In addition to the cd command and exit command that you implemented, you will need to add a history command that will call the print_history function in your shell. The clear_historyfunction should be called when your shell exits.

Please help me finish this project by adding a history command, and any additional functions needed. such as cleaning my main and creating cd and exit functions?

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

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions