LINUX - Writing a Simple ShellInstructionsFor 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 Linuxcommand with any number of command line argumentsThe shell should repeatedly prompt the user for a command then execute that command, unless theuser types exit in which case the shell should exit sucessfully (using exit(0)), or the user enters onlywhitespace in which case the shell should prompt the user againIf the user does not type exit or a blank line the shell should break up the line of text the user enteredinto an array of C-stringsHere is a sample of what running your shell might look like:[mann@apmrc32 -JS /nutshell> IsmanAssign2.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> foardnutshell: 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 stringiSblank(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 stringscontaining the command and the arguments, then stores the strings in the array providedI 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 sizewhen you declare a character array to represent the string to pass to cead ineMAX_ARGS - the maximum number of arguments (including the command) that may beused in a command line. Use this as the size when you declare an array of C-strings torepresent the array of arguments to pass to paseargs 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 andreturns a value of type pidt that can be treated like an integer value. WARNING: be verycareful with using fork in a loop. If you are not careful you will fork too many processesfor your shell to handle.Check to see if the current process is the child or the parent by checking the return value offork. If the return value is 0, the current process is the child process. Otherwise, it is theparent process.The parent process should call the wait function with a single argument of NULL to wait forthe child to completeThe 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 nextine is executed only when exesRfails. If this is the case, display an error message and terminatethe child process by calling exit 1)Displaying Output to the ConsoleIn 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 formatcode %s and provide the string as an extra argumentprint"Hello %sln*", name);We also used the fputs function with the stream stderr to write to standard erroriRuts/stderr, "Something went wrong.In)If you want to insert a C-string into your error message you can use frintfpUnt'stderr, "Something went wrong with %s.In", str);String ComparisonWhen you are checking to see if the user has entered the exit command it may be tempting to dosomething like thisifline "exit')Il display message and exit programThis will not work because the operator compares the addresses of the strings, not the contentsof the stringsInstead you will need to use the strcMN function. This function takes two strings as arguments andreturns zero if they are the sameif(strcmp(str1str2) == 0)putsThe strings are the same");Compiling Your CodeYour source file should be called yourlastnameAssign2.c except with your actual last name. Tocompile this code you should use the gcc compiler. To create an executable file called nutshell usethe following command:Cgc-onutshell yourlastnameAssign2.c

Question
Asked Sep 20, 2019
51 views

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 <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#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 < MAX_LINE)
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 < MAX_ARGS)
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
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
help_outline

Image Transcriptionclose

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

fullscreen
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
help_outline

Image Transcriptionclose

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

fullscreen
check_circle

Expert Answer

Step 1

C shell is a type of command processor which enables user to write and modify commands .It can also read from a file which is known as script .Here in this program we have defined some functions –

is_blank and isspace – to check whether string contains white space or not.

MAX_LINE 256 – to find the maximum number of characters

MAX_ARGS 64 – to find the maximum number of arguments

read_line(line) – reads a line from text from input and stores it in a string.

Parse_args(cmd,args) – takes a command line an segregate it into sequence of strings that contains command and arguments.

...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in
EngineeringComputer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Develop (separate) truth tables for the following equations: 1)  (A+B)(B+C) 2) (X*Y)*~(Z)3) (A+B) ⊕ ...

A: Truth table is used to simplify the Boolean expression in digital electronics. Number of gates used ...

question_answer

Q: use java Develop a program which allows the user to enter numbers into an array. Input will be as fo...

A: Program plan:Import the required packages.Define a class named “ArraySort”.Define a “main()” method....

question_answer

Q: In C++ Create a function that takes in an array and outputs the min, max and average of the array to...

A: A function is to be created in C++ that will take in an array and will results with minimum, maximum...

question_answer

Q: In a server, the processor accounts for 50% of total server power, the memory system accounts for 30...

A: For a server,Power consumption of processor = 50 % of total powerPower consumption of memory system ...

question_answer

Q: Construct npda that accept the following languages on Σ = {a, b, c}: L = {anb3n : n ≥ 0}.

A: To construct the NPDA of the language L = {anb3n : n ≥ 0} on Σ = {a, b, c}, we will draw a machine l...

question_answer

Q: How to write a function in python where it takes a list of string s and finds the index of the longe...

A: The program has the following algorithm:Intitialize the counter counting string list position number...

question_answer

Q: Locate and list three examples of image map button bars on the Web, or of particularly creative uses...

A: Image map button are the hyperlink areas on an image and is created using specific image coordinates...

question_answer

Q: Write a program which does the following (name=xxx_Chapter81):1) Asks the user for the number of row...

A: Description:The following program is to print the two dimensional array.

question_answer

Q: This program should compound interest continuously. It asks the user for the number of times they wa...

A: Formula for calculating compound interest=P(1+R/N)NTP – Principle amountR – Rate of the interest, an...