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: 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

LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
5th Edition
ISBN:9781337569798
Author:ECKERT
Publisher:ECKERT
Chapter7: Working With The Bash Shell
Section: Chapter Questions
Problem 6DE
icon
Related questions
Question

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
Transcribed Image 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:
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L