1 Introduction
In this assignment, you are going to implement a mini-shell, namely, asg1-shell,usingC.
If one invokes the program asg1-shell,weshallseesomethinglikethis:
Figure 1: Your shell
The prompt has the format of [3150 Shell:]=>.Yourshellshall
support:
• Execution of any Linux built-in command (e.g., ls).
• Five shell-specific commands: gofolder, push, pop, dirs and bye.
• Basic signal handling: i.e., a user cannot exit the shell simply by typing ctrl-c.
• Basic command chaining: and k.
• Basic error handling.
1.1 Execution of any Linux program
Your shell shall allow a user to execute any Linux program, with basic error handling. Note
that the user can input either an absolute path (e.g., /bin/ls)orjustafilename(e.g.,
ls). If an absolute path is not given, your shell should search the program in the following
sequence:
/bin ! /usr/bin ! . (current directory)
In case your shell cannot locate the program, your shell should report an error message
“{command name}: command not found” (see Figure below).
3
*
1.2 Shell-specific commands
1.2.1 gofolder
This command is similar to cd command in Linux, for changing the working directory, like
below:
After a successful operation, your prompt shall be updated with current directory name.
Please note that you are just required to implement basic changing of directory as shown
above.
4
1.2.2 push,pop and dirs
These commands behave similarly to Linux commands pushd, popd,anddirs.
• On push [directory path],theshellshall(1)pushthecurrentdirectorytoastack,
(2) change to the directory specified by [directory path],and(3)printthecontent
of the stack.
• On dirs,theshellshallprintthecontentofthestack.Theformatis:
[item number] [path]
With the most recent item starts with item number0andtheoldestiteminthestack
has the largest item number.
• On pop,theshellshall(1)popanitemfromthestack,(2)changetothatdirectory
and (3) print the content of the stack.
If the end of the stack is reached, the shell should prompt the user:
pop: directory stack empty
Example:
1.2.3 bye
This command is equivalent to exit command in Linux, which lets a user to terminate the
shell (and back to the normal Linux bash shell).
5
1.3 Basic signal handling
Your shell shall handle the following list of signals as follow:
Signal Action
SIGINT (Ctrl + C) Ignore the signal.
SIGTERM(default signal of command“kill”) Ignore the signal.
SIGQUIT (Ctrl + \) Ignore the signal.
SIGTSTP (Ctrl + Z) Ignore the signal.
6
1.4 Basic command chaining
Your shell should handle two logical operations: AND() and OR(k), like below:
7
2 The Assignment Package
You should have opened a Github account and told us your github account by filling up a
Google form. during your assignment 0. So, after logging in your Github account, come here
https://classroom.github.com/a/ttey5Hj5to get a new repo for this assignment. The
new repo should contain the starter package for this assignment.
3 Your assignment
You are given the following files:
Name Description
/asg1-shell.c Source codeofa runnablebutnon-functioningshell
(Work on it).
/asg1-shell Executable of a runnablebut non-functioning shell.
(Try to run it; Type ctrl-d to quit)
/demo-asg1 Executable, serve as the demo of what you shall
implement. Our gradingwill also use this to define
test cases. That is, the behavior. of your shell shall
exactly follow this demo.
/hello Executable, a hello world program.
/testcases/data Test data.
/Makefile Makefile to compile asg1-shell.
/grader.sh We will run this script. to grade your assignment
(Don’t touch).
8
3.1 To begin
Make, then run grader.sh, you shall see something like this:
This shell script. feeds in some test cases to asg1-shell (barely functioning, but you
should make it functioning in this assignment) and demo-asg1(which is functioning,but no
source code is given) to match their outputs, and reports the number of test cases passed.
Initially, it shall report that none of the test cases passes. Your job is to make all test cases
pass:
If you invoke the grader script. like the following:
./grader.sh 2
Then it only runs test case 2 for you.
9
3.2 Submitting your assignment
Follow the procedure in the warm-up assignment or bonus assignment 1.
4 Assumptions
You can assume the following always holds in this assignment:
Input
• An input command line has a maximum length of 255 characters, including the trailing
newline character.
• An input command line ends with a new line character \n
• There would be no leading or trailing space characters in the input command line.
• Atokenisaseriesofcharacterswithoutanyspacecharacter.Eachtokenisseparated
by exactly one space character only.
• There will be no combination of shell commands in command chaining. For example,
no test cases like ls || bye, bye bye, bye || ls are defined.
• Your shell should also be terminated by ‘Ctrl-D’, the end-of-file character (NOT signal)
upon receiving in the prompt.
5 Grading
1. TheTA will fetch and grade your latest version in your repo as of 4 Dec 2017, 11:00AM.
Remember to commit and push before the deadline.
2. Your shell shall output results to standard output stream (stdout). Otherwise, you
will get 0 mark.
3. The objective of this assignment is to let you practice the necessary system program-
ming skills. So you are not allowed to do your assignment in a way that violates that
10
objective. Therefore, you cannot invoke system(3) library call, invoke any existing
shell programs, including but not limited to: “/bin/sh”, “/bin/bash”, “pushd”, call-
ing non built-in linux libraries and etc. Otherwise, you will score 0 mark. If you have
doubts about the legitimacy of your program, you may ask on Piazza.
4. Your shell should not leave any zombies in the system when it is ready to accept a new
user input. Otherwise, you will have 1 test case marks deducted.
5. There are 30 test cases in total. Test case 6 and test case 24 carry more weight – each
carries 6.25 mark. Each of the rest carries 3.125 mark.
6. Maximum assignment score is 100.
6 Change Log
1.2 Section 1.2.2 Screen Capture 2 updated.
1.1 Section 5 Point 3 updated.
1.0 Specification Released
7 Questions
If you have doubts about the assignment, you are encouraged to ask questions on Piazza
usingthecorrespondingtag. Please focus on knowledge. Unhealthy questions/comments
that focus on scores and grades are not encouraged.
If you findany (possible) bugs, send private questions on Piazza to us instead
— otherwise that may cause unnecessary panic among the class if that is not a real bug.
8 Academic Honesty
We follow the University guide on academic honesty against any plagiarism.
11
9 General Notes
• This specification and our grading platform. are both based our given course VM. You
should compile, debug and run the assignment program on that VM. So, if you insist
to develop on another platform. other than our VM and got any question, test it on
our VM before you ask.
• The TA reserves the right to adjust your scores for any request that requires their extra
manual e↵ort.
• While we have already tried our best to prepare this assignment, we reserve all the
rights to update the specification and the grading scheme. If there are any mis-
takes/bugs which are on ours, the TA will step in and do manual grading to ensure
you get what you deserve. Please respect each other. Any irresponsible, unfair, biased
sentiment would regard as a disciplinary case.
• If this is a programming assignment, only C is allowed, not even C++. If this is a
scripting assignment, only bash shell script. is allowed, not even Python. Furthermore,
for C programming assignments, use the “exit” function parsimoniously because it
might influence the grader as well. Therefore, use “return” instead of “exit” whenever
possible.
• Although this is not an algorithm class, you still shouldn’t implement your assignment
with very poor complexity (e.g., O(n
3
)). The TA reserves the right to terminatea test
case and regard that as a fail test case when a program takes unreasonably long time
(e.g., with deadlock) to finish.
• (Frequently Asked) [Output format] If the assignment package includes a demo,
then our grader defines test cases based on the given demo. In that case, your output
shall exactly follow that demo. For example, hypothetically, if our demo outputs a
message like:
command not found
with two spaces between “not”and“found”. Your output shall also match that in
order to pass the test. The good news is that, if our given demo has not implemented
12
something (e.g., missed certain error checking), you also don’t need to do so. No
test cases would be defined based on something that our demo has not
implemented.
• (Frequently Asked) [Scope of error handling] The scope of error checking and
handlingshall refertoboth ourgiven demo (if given) andourgiven test cases.
First, the corresponding output message shall exactly follow our demo. Second, you
are informed that our demo may have implemented more error checking that what our
grader will test. In that case, it is fine that you implement only the error checking that
is tested by our grader. So, one top tip is:
CHECKTHE (SOURCEOF)
TESTCASESBEFOREYOUASK
• (Frequently Asked)[No hidden test case] We are not intended to run any secret/ex-
tra test cases that deliberately break your assignment. That is, WYSIWYG — your
final score shall be generally indicated by what the grader reports when it runs on
the course VM. However, we do reserve the right to run some additional test cases to
avoid any mis-conduct (e.g., hard-coding the results), and/or invite you to explain the
source code to the teaching assistants and adjust the scores accordingly.
• We welcome discussions among classmates. But don’t share your assignment with the
othersin any means. For example,don’tput your sourcecode in any publicvenue (e.g,
publicrepo, your homepage, Facebook). We handleplagiarism strictly. On submitting
this assignment, you are agreed that your code’s copyright belongs to the Chinese
University of Hong Kong. Unless with our written approval, you must not release
your source code and this specification now and forever.Ifyoushareyourcodewith
anyone without our written consent, that would regard as a disciplinarycase as long
as you are still a CUHK student (i.e., even after this course). If you share your code
with anyone without our written consent after your graduation, that would regard as
13
a breach of copyright and we reserve all the rights to take the corresponding legal
actions.
• Google is your friend. We encourage you use Google for help to do the assignment.
However, if you happen to find any source codes related to this assignment, you still
cannot copy it but use your own way to implement it. You need to put down your list
of source code referencesas comments in the top of your source code.