Assignment 0: Introduction to Unix and C

Due: Mon Jan 14 11:59 pm
No late submissions accepted.

Assignment by Michael Chang, Julie Zelenski, and Chris Gregg, with modifications by Nick Troccoli

No late submissions are accepted on this assignment. The deadline is firm without exception. There is no on-time bonus for this assignment.

Learning Goals

The goals for this assignment are for you to get familiar with working in the Unix environment, and editing/building/running C programs.

Unix and C Resources

The Resources page linked to in the top bar of the course website has a collection of guides, including videos, for various Unix commands, tools, and more that we'll be using this quarter. We highly recommend reading through the materials, and trying out various commands and tools yourself, to get practice. In particular, some of the activities in this assignment will rely on commands you will need to read up on from that page. Note that you don't need to memorize all the information on the Resources page, but you will start to memorize lots of commands just by repetition. That being said, familiarization with the command line will make you more productive!

Before starting the assignment, double-check that you are comfortable with these Unix fundamentals - the guides for each of these can be found on the Resources page:

  • Log in to Myth
  • Manage files, navigate the filesystem
    • Did you create a CS107 directory to hold and organize your work?
    • Can you find the code being demoed in lecture?
  • Edit files, configure Emacs
    • You should be able to create a new file, edit its contents, save, and exit.
  • Working with the shell and Unix commands
    • If you encounter a command that is new to you, how can you get more information about it?
    • Have you tried using tab-completion to avoid manually typing long commands/paths?

Additionally, take a look at the working on assignments page linked to from the assignments page as you are working on this assignment; it outlines everything you need to know about working through a CS107 assignment, from getting the starter code to testing to submitting.

Cloning The Assignment

To get started on the assignment, you must "clone" the starter code to get a copy you can work on. Check out the working on assignments page for how to do this.

If you attempt to clone and receive an error that the "repository does not exist":

  • double-check for typos in the path. The path needs to be typed exactly as specified. This includes the odd-looking $USER at end, which is a environment variable that expands into your username automatically.
  • be sure you are logged into myth

If you confirm you are on a Myth system and your correctly-typed path is not available, this indicates that you were not on the Axess enrollment list at the time we created the student starter code projects. Please send an email to cs107@cs.stanford.edu and tell us your username and we can manually set up the starter code for you. Please make sure to enroll in Axess as soon as possible so that the starter code is automatically generated for you in the future.

Provided Files

The starter project contains the following:

  • readme.txt: a text file where you will answer questions for the assignment
  • triangle.c , Makefile and custom_tests: used to build and test the triangle program
  • samples: a symbolic link to the shared directory /afs/ir/class/cs107/samples/assign0. It contains:
    • SANITY.ini: the configuration for Sanity Check. You can ignore this file.
    • server_files: a folder that pertains to the first part of the assignment.
    • triangle_soln: an executable solution for the Triangle program.
  • tools: contains symbolic links to the sanitycheck and submit programs for testing and submitting your work.

Symbolic links mean that the files actually live in the CS107 class directory, but appear just like normal files in your starter code folder. Note that the shared directories are not editable by you, so you will not be able to create, edit, or delete files with these directories, since they actually live in the CS107 class directory.

1. Enrollment Confirmation

In order to complete your enrollment in CS107, you must confirm your availability to attend the exams and accept the course Honor Code policy.

Open the readme.txt file in your editor now and edit as appropriate. This file also contains reminders to join our course Piazza forum and sign up for lab.

2. Intruder Detection

For this activity, you will investigate a simulated break-in and answer the questions below. Type your answers into the readme.txt file. For each, briefly describe (in 1-2 sentences) how you arrived at that answer and what Unix commands you used.

Situation: An intruder had broken into your dorm's unix-based server and deleted most of the files! Fortunately, there is a backup of the server contents, but before restoring the files, you'd like to know who the intruder was and what they did. With your newly-developed Unix skills, you are just the expert to help investigate.

The key files are available in the directory samples/server_files, which you can access within your assign0 directory.

The first thing you want to determine is the username of the intruder. The server is used by many different users. Each user has a home directory under home/. For example, the home directory for the user bob would be the path home/bob. The file users.list contains a list of all the authorized users. In an uncompromised system, each home directory would correspond to a user on the authorized list and vice versa. The intruder is not an authorized user and they gained illicit access by inserting their own home directory onto the system. This means there is one home directory that doesn't belong, and your job is to find it.

Manually cross-comparing the users.list to the directory contents would be time-consuming. Instead, what Unix commands can you use to help? Check out the Resources page for some ideas. Hint: If you need to create a temporary file, you won't be able to create it inside samples/server_files; you can create them directly inside your assign0 directory instead.

  1. What is the username of the intruder? Include the details on how you figured out the answer, and what Unix command(s) you used.

Now that you know the intruder's username, you can examine the files in intruder's home directory to learn what they were up to. Though the intruder tried to delete all the home directory files as part of covering their tracks, you can see that this supposedly empty directory is still taking up space. Perhaps something interesting was overlooked?

Take a closer look to find out what files have been left behind. Open each of the files in the intruder's home directory to see their contents.

  1. There is one file in the intruder's home directory that provides critical information about their activities. What file is that and what does it contain? Include the details on how you figured out the answer, and what Unix command(s) you used.

You believe that the intruder used sudo to execute some commands as a privileged user. You want to identify those commands, but the file is rather long to comb through by hand. What Unix command can you use to extract the information you seek?

  1. Which commands did the intruder execute using sudo? Include the details on how you figured out the answer, and what Unix command(s) you used.

3. C Introduction

The final task of the assignment gives you practice using the Unix development tools to edit, build, run, and test a short C program.

In your assign0 folder, type make. This will build the program named triangle. Run the program to see what it does:

$ ./triangle

You should be rewarded with an ascii representation of Sierpinski's triangle - cool! Try to run make again:

$ make
make: Nothing to be done for `all'.

This isn't an error; it simply means that nothing has changed in the program's source, so there isn't anything to re-compile.

Open triangle.c in a text editor and change the value of the variable nlevels in main from 3 to 5. After you have saved the file, you must then use make to re-build the program, and then you can run the newly built program to see the bigger triangle. If you forget to re-run make, you will run the original version of the program that has not been updated!

The starter code uses a fixed constant for the number of levels to print. Your task is to extend the program to take an optional command-line argument that allows the user to dictate the number of levels. With no arguments, ./triangle should default to a level 3 triangle, but the user should also be able to provide a numeric argument, e.g. ./triangle 4 or ./triangle 2, to control the number of levels. If given an unworkable number of levels (anything larger than 8 gets unwieldy and negative would be nonsensical), your program should reject it with a helpful and explanatory message that informs the user how to correct their error, and then terminate early. The best function to do this is the error function; check out the manual pages for more information about this function. One note is that you should specify an errnum of 0, since we don't need to print out an error message corresponding to a specific error code. Try to figure out the values for the remaining parameters. Note: You may assume that the user will enter an integer value, and do not have to worry about handling arguments that are not valid integers.

In order to complete this task, the program will need to convert the user's argument (supplied in string form) into an integer. The C library function atoi can be used to do this. Review the man page (man atoi) or look in your C reference to get acquainted with this function.

Testing

Now let's test the program implementation. The Sanity Check tool is included in the assignment starter project, and acts as a testing aid. Read the guide to working on assignments for more information about how to use it.

The default sanitycheck for assign0 has one test that validates the output of the triangle program when given no argument. The unmodified starter program code should pass this test. After you have extended the triangle program to accept an argument, the program should continue to pass the default sanitycheck, but you will need new tests to validate the argument-handling.

You extend sanitycheck to test additional cases by writing your own custom_tests file. The starter project includes a sample custom_tests. Open this file in your editor to see the format. Now consider what additional test cases are needed to fully vet the output of your new, improved triangle program. You will need at least two additional tests. Add those tests to custom_tests and use these with sanitycheck to validate that your triangle program passes all tests. For more information about how to run your custom tests, check out the guide to working on assignments. For tips on thorough testing, check out our testing guide, linked to from the assignments page.

Submitting

Once you are finished working and have saved all your changes, check out the guide to working on assignments for how to submit your work. We recommend you do a trial submit in advance of the deadline to familiarize yourself with the process and allow time to work through any snags. You may submit as many times as you would like; we will grade the latest submission.

Grading

The assignment is graded out of 20 points. Full credit will be awarded for reasonable answers to the questions in the readme.txt file and a correct modification of triangle.c and custom_tests. This assignment is worth many fewer points than other assignments, but we expect everyone will earn all 20 points; go team!

Post-Assignment Check-in

How did the assignment go for you? We encourage you to take a moment to reflect on how far you've come and what new knowledge and skills you have to take forward. Once you finish this assignment, you should have your environment configured and should be starting to feel comfortable with the command-line interface, navigating the filesystem, using Emacs, and getting around Unix. You're off to a great start!

To help you gauge your progress, for each assignment/lab, we identify some of its takeaways and offer a few thought questions you can use as a self-check on your post-task understanding. If you find the responses don't come easily, it may be a sign a little extra review is warranted. These questions are not to be handed in or graded. You're encouraged to freely discuss these with your peers and course staff to solidify any gaps in you understanding before moving on from a task. They could also be useful as review before the exams.

  • Identify a few different techniques to avoid painstakingly re-typing a long Unix command to execute.
  • How do you copy and paste in Emacs?
  • Explain the purpose and use of the CS107 tools sanitycheck and submit. How do you customize the tests used by sanity check?

We would also appreciate if you filled out this homework survey to tell us what you think. We appreciate your feedback!

Frequently Asked Questions

When I try to run the triangle program in my directory, it responds "command not found". What's wrong?

Unix wants you to instead refer to the program by its full name ./triangle. See our overview of the Unix filesystem on the Resources page for more information.

Must my error messages exactly match the wording of the sample solution?

An error message must be accurate and actionable, giving the user sufficient information to understand the problem and what is needed to resolve it. Our sample executable models appropriate handling and feedback on errors. You are not required to match our wording, but any substitution should be similarly informative. Sanitycheck has no human judgment so it will only accept an exact match. When grading, the TA makes the call on whether the mismatch is an acceptable substitution. A reliable way to ensure your error message is sufficient is to adopt the wording of the sample solution.

How do I use the sample executable? How does it relate to sanity check?

Our provided sample executable can be used a reference implementation during testing. Run the solution and your program on the same input and verify the output is the same:

myth$ samples/triangle_soln 4
                ... solution output is here ...
myth$ ./triangle 4
                ... your output is here  ...

If your program produces the same result as the sample, all is good. You can manually "eyeball" the two results, or run sanitycheck with the provided tests, or your own tests. You can find more information about sanity check in our guide to working on assignments.

Do we have to handle the case where the user provides more than one command line argument?

You can ignore any additional arguments if the user passes more than one. For example, if the user types ./triangle 2 3, then you should draw a level 2 triangle. This is the behavior of the sample solution as well.