The code you will need for this assignment is here (in C file) /* YOUR NAME HERE * CSCI 4100 * Programming Assignment 2 * The Nut Shell - a simple shell that executes commands */ #include #include ...



The code you will need for this assignment is here (in C file)


/* YOUR NAME HERE
* CSCI 4100
* Programming Assignment 2
* The Nut Shell - a simple shell that executes commands
*/


#include
#include
#include
#include
#include
#include
#include
#include


#define MAX_LINE 256
#define MAX_ARGS 64


int is_blank(const char *cmd);
void parse_args(const char *cmd, char *argv[]);
void read_line(char *line);


int main()
{

/* YOUR CODE HERE */

}


/* Checks if a string contains only whitespace
* str - the string to check
* returns 1 if str contains whitespace, 0 otherwise
*/
int is_blank(const char *str)
{
int i = 0;
while(str[i] != '\0') {
if(!isspace(str[i++]))
return 0;
}
return 1;
}


/* Reads a line of text from the console
* line - an allocated array of characters of size MAX_LINE
* where the function can store all of the characters read.
*/
void read_line(char *line)
{
char c;
int chars_read = 0;
while((c = getchar()) != '\n' && chars_read <>
line[chars_read++] = c;
line[chars_read] = '\0';
}


/* Breaks a command line into individual strings containing the
* command and all of its arguments
* cmd - a string containing the command line
* argv - an allocated array of strings of size MAX_ARGS where the
* command and its arguments will be stored.
*/
void parse_args(const char *cmd, char *argv[])
{
int argc = 0;
while(*cmd != '\0' && argc < max_args)="">
int i = 0;
while(!isspace(cmd[i]) && cmd[i] != '\0') i++;
argv[argc] = (char *) malloc(i + 1);
strncpy(argv[argc], cmd, i);
while(isspace(cmd[i])) i++;
cmd += i;
argc++;
}
if(argc <>
argv[argc] = NULL;
else
argv[MAX_ARGS - 1] = NULL;
}



Please do not forget to write a brief explanation of what the program does.......


LINUX - Writing a Simple Shell<br>Instructions<br>For this programming assignment you are going to implement a simple shell called nutshell in C.<br>This shell will not be able to implement pipes or redirects, but it will be able to execute any Linux<br>command with any number of command line arguments<br>The shell should repeatedly prompt the user for a command then execute that command, unless the<br>user types exit in which case the shell should exit sucessfully (using exit(0)), or the user enters only<br>whitespace in which case the shell should prompt the user again<br>If the user does not type exit or a blank line the shell should break up the line of text the user entered<br>into an array of C-strings<br>Here is a sample of what running your shell might look like:<br>[mann@apmrc32 -JS /nutshell<br>> Is<br>manAssign2.c nutshell<br>> Is -<br>total 32<br>-W-r- 1 Colemann staff 2146 Sep 14 16:04 colemanAssign2.c<br>-wX-1 colemann staff 9076 Sep 14 16:05 nutshell<br>> foard<br>nutshell: command fnod not found<br>>exit<br>[exiting nutshell]<br>I have provided the following functions to get you started:<br>tead ine(line) reads a line of text from input and stores it in a string<br>iSblank(str) - returns true (1) if a string contains only whitespace, false (0) otherwise.<br>parse agscmd, argv)- takes a command line and breaks it up into a sequence of strings<br>containing the command and the arguments, then stores the strings in the array provided<br>I have also defined the following constants using the #define preprocessor statement:<br>MAX_LINE - the maximum number of characters allowed in a single line. Use this as the size<br>when you declare a character array to represent the string to pass to cead ine<br>MAX_ARGS - the maximum number of arguments (including the command) that may be<br>used in a command line. Use this as the size when you declare an array of C-strings to<br>represent the array of arguments to pass to paseargs<br>

Extracted text: LINUX - Writing a Simple Shell Instructions For this programming assignment you are going to implement a simple shell called nutshell in C. This shell will not be able to implement pipes or redirects, but it will be able to execute any Linux command with any number of command line arguments The shell should repeatedly prompt the user for a command then execute that command, unless the user types exit in which case the shell should exit sucessfully (using exit(0)), or the user enters only whitespace in which case the shell should prompt the user again If the user does not type exit or a blank line the shell should break up the line of text the user entered into an array of C-strings Here is a sample of what running your shell might look like: [mann@apmrc32 -JS /nutshell > Is manAssign2.c nutshell > Is - total 32 -W-r- 1 Colemann staff 2146 Sep 14 16:04 colemanAssign2.c -wX-1 colemann staff 9076 Sep 14 16:05 nutshell > foard nutshell: command fnod not found >exit [exiting nutshell] I have provided the following functions to get you started: tead ine(line) reads a line of text from input and stores it in a string iSblank(str) - returns true (1) if a string contains only whitespace, false (0) otherwise. parse agscmd, argv)- takes a command line and breaks it up into a sequence of strings containing the command and the arguments, then stores the strings in the array provided I have also defined the following constants using the #define preprocessor statement: MAX_LINE - the maximum number of characters allowed in a single line. Use this as the size when you declare a character array to represent the string to pass to cead ine MAX_ARGS - the maximum number of arguments (including the command) that may be used in a command line. Use this as the size when you declare an array of C-strings to represent the array of arguments to pass to paseargs
To execute a command you must do the following:<br>Use the fork function to fork off a child process. The fork function takes no arguments and<br>returns a value of type pidt that can be treated like an integer value. WARNING: be very<br>careful with using fork in a loop. If you are not careful you will fork too many processes<br>for your shell to handle.<br>Check to see if the current process is the child or the parent by checking the return value of<br>fork. If the return value is 0, the current process is the child process. Otherwise, it is the<br>parent process.<br>The parent process should call the wait function with a single argument of NULL to wait for<br>the child to complete<br>The child process should call the execyR function with two arguments:<br>The name of the command being executed (argument 0 of the array.)<br>The entire array of command line arguments.<br>If execxR succeeds, the process will no longer be running the shell code. This means that the next<br>ine is executed only when exesRfails. If this is the case, display an error message and terminate<br>the child process by calling exit 1)<br>Displaying Output to the Console<br>In Assignment 1, I suggested using puts to display output to the console.<br>puts(

Extracted text: To execute a command you must do the following: Use the fork function to fork off a child process. The fork function takes no arguments and returns a value of type pidt that can be treated like an integer value. WARNING: be very careful with using fork in a loop. If you are not careful you will fork too many processes for your shell to handle. Check to see if the current process is the child or the parent by checking the return value of fork. If the return value is 0, the current process is the child process. Otherwise, it is the parent process. The parent process should call the wait function with a single argument of NULL to wait for the child to complete The child process should call the execyR function with two arguments: The name of the command being executed (argument 0 of the array.) The entire array of command line arguments. If execxR succeeds, the process will no longer be running the shell code. This means that the next ine is executed only when exesRfails. If this is the case, display an error message and terminate the child process by calling exit 1) Displaying Output to the Console In Assignment 1, I suggested using puts to display output to the console. puts("Hello World!\n"); If you want to insert a C-string into your output you can use the pint function with the special format code %s and provide the string as an extra argument print"Hello %sln*", name); We also used the fputs function with the stream stderr to write to standard error iRuts/stderr, "Something went wrong.In) If you want to insert a C-string into your error message you can use frintf pUnt'stderr, "Something went wrong with %s.In", str); String Comparison When you are checking to see if the user has entered the exit command it may be tempting to do something like this ifline "exit') Il display message and exit program This will not work because the operator compares the addresses of the strings, not the contents of the strings Instead you will need to use the strcMN function. This function takes two strings as arguments and returns zero if they are the same if(strcmp(str1str2) == 0) putsThe strings are the same"); Compiling Your Code Your source file should be called yourlastnameAssign2.c except with your actual last name. To compile this code you should use the gcc compiler. To create an executable file called nutshell use the following command: C gc-onutshell yourlastnameAssign2.c
Jun 02, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here