Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your shell will have a loop that reads command lines and process them. The prompt for your shell will be shell _ jr: .

Your shell will have a loop that reads command lines and process them. The prompt for your shell will be "shell
_
jr:
"
.
The commands your shell must handle are:
1.
exit
-
When the user enters the exit command the shell will stop executing by calling exit
(
)
with the exit code
0
.
Betore executing exit, the shell will print the message "See you".
2.
goodbye
-
Has the same functionality as exit.
3.
cd
-
This command changes the current directory. You can assume the user will always provide a directory as an argument. Assume that a directory name does not include whitespace characters.
4.
pushd
-
This command is used to change the current directory to the directory given as the command
-
line argument and simultaneously push the current directory onto a stack of directories. Shell Jr can keep
16
directories on the stack. If the user try to push more than
16
,
print the message "Directory stack is full" and the current directory won't be changed. Assume that a directory name does not include whitespace characters.
5.
dirs
-
This command will print the directories in the stack.
6.
popd
-
This command is used to retrieve the directory from the top of the stack and change the current directory to the directory. It will remove top directory from the stack. If the user try to pop a directory when the stack is empty, print the message "Directory stack is empty" and the current directory won't
be changed.
7.
A command with a maximum of one argument
(
e
.
g
.
,
wc location.txt
)
.
That means your shell should be able to handle commands like
,
date or wc location.txt
.
Note that these are not shell commands but executable binaries.
Requirements:
1.
You must NOT use an exec
*
function to implement the functionality associated with the commands exit, goodbye, cd
,
pushd, dirs, and popd. For other commands, you must create a child
(
via fork
(
)
)
and use execvp
(
)
to execute the command.
2.
If the user provides an invalid command, the message "Failed to execute
"
followed by the command name should be printed. In this case the child will exit returning the error code EX
_
OSERR. Use printf to display the message and flush the output buffer
(
e
.
g
,
fflush
(
stdout
)
)
.
Note that the shell is not terminated by executing an invalid command.
3.
You don't need to handle the case where the user just types the enter key. That is
,
you can assume the user will always provide a command.
4.
Make sure you use printf to print the shell prompt and that you flush the buffer
6.
Your shell should dynamically create memory space for the directory given for the pushd command and free the space when popd is executed. The directory pushed to the stack should be an absolute path. For example, if the current directory is
/
usr and pushd include is entered,
/
usr
/
include will be pushed to the stack.
7.
You must use execvp
(
and no other exec
*
system call
)
.
8.
Your code must be written in the file shell
_
jr
.
c
.
9.
You may not use dup
2
,
read, write, nor pipes
-
not discussed in class.
10.
You must not use system
(
)
in order to execute commands.
11.
You can assume a line of input will have a maximum of
1024
characters.
12.
Provide a makefile that builds an executable called shell
_
jr
.
Name the target that builds the executable shell
_
jr
.
Feel free to add any additional targets you need.
13.
All your
programs in this course should be written using the compiler gcc
,
with the options defined in the gcc
_
aliases
_
info.txt file. This file can be found in the info folder of the public grace account.
14.
Common error: If you get the submit server message "Execution error, exit code
126
"
,
execute "make clean
"
before submitting your code.
15.
Common error: To forget to return the correct value
(
e
.
g
.
,
0
)
in your code.
16.
Your
program representing your shell does not take command line arguments. That is
,
the main function is: int main
(
)
{
}
17.
When you see that execvp relies on argv that means it uses an array of strings
(
argv is not the parameter associated with command line arguments
)
.
Just initialize argv with an array of strings and pass it to execvp.
18.
Your shell should exit when end of file is seen. This explains why public tests do not have exit nor goodbye as the last command.
19.
ShellJr exercise relies on Standard I
/
O
(
e
.
g
.
,
scanf, fprintf, fgets, and so on
)
,
NOT UNIX I
/
O
(
e
.
g
.
,
write or read function
not yet discussed in class
)
.
20.
ShellJr takes a maximum of two arguments
(
e
.
g
.
,
wc location.txt
)
.
You can ignore any other values provided after the second argument. For example

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_2

Step: 3

blur-text-image_step3

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

More Books

Students also viewed these Databases questions