CS 270: Program assignment 1

The purpose of this assignment is to introduce you to systems programming in C, which is the language that we will be using throughout the semester. You must complete this program in C, not C++ or C#. In particular, it must compile with gcc without any -std options. Your program must compile and run on your virtual machine (VM), although you may develop it on any machine you like, such as on the MultiLab.


You will be graded on exact adherence to these specifications.

  1. Your C program is called prog1.c, and the executable program is called prog1.
  2. The program is invoked on the command line like this:
    prog1 FILE
    where FILE can be any valid Unix file name, such as testdata.txt.
  3. Your program is interactive. It accepts commands from standard input (file descriptor 0) and sends output to standard output (file descriptor 1). All output is terminated by a newline (\n). Here are the commands it accepts:
    q  Quit the program.
    e  Print the name by which the program is called (argv[0])
    p n Print the first n bytes of FILE as characters.
    L n Print the last n bytes of FILE as characters.
    i n  Print the first 4n bytes of FILE as n unsigned integers, separated by a space
    x n Print the XOR of the first n bytes of FILE as a 3-digit unsigned octal number.
    There is one space separating operators and parameters. Commands are terminated by a newline.
  4. The program does not print anything other than what is specified above, except if it encounters an error. If there is an error, the program prints a helpful one-line message to standard error (file descriptor 2) and continues to the next command. (If the error is that standard input is closed, the program should print a message and terminate.)
  5. The program treats any input other than a valid command as an error.
  6. You may treat each command independently, opening the file, handling the command, then closing the file.
  7. You must have a Makefile. The Makefile must be able to compile your program when you run
    make prog1
    You should start with the Makefile that you can get this way:
    wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/Makefile
    You may modify this file.
  8. You may also start with the skeletal C program you can get this way:
    wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/prog1.c
  9. Your program must compile without any warnings with this command:
    gcc -Wall prog1.c -o prog1
    The Makefile we provide invokes the compiler exactly this way.
  10. The program must not assume that the data file exists or it is long enough to satisfy the commands you input to it.
  11. You may assume that commands are not longer than 100 characters.
  12. A command that requires more bytes than are in the file is an error.
  13. The program must terminate with exit status 0 if it finishes normally. It must not terminate with a fault.
  14. You can get a working executable program:
    wget http://www.cs.uky.edu/~raphael/courses/CS270/project1/workingProgram
    chmod +x workingProgram 
  15. You might want to use the od ("octal dump") program to inspect a file:
    od -t u4 -t o1 FILE

    Useful information

The man program can tell you about useful programs and functions in the C library. For example, run this program to find out about the printf() function:

man 3 printf
The 3 means "section 3 of the manual", which is where the C library is documented. You can leave off the "3" and find all references to printf().

You might find the following functions useful:

open fopen 
read fread
close fclose 
lseek fseek
printf fprintf
scanf fscanf
It is a good idea to write your entire program using open/read/close/scanf or using fopen/fread/fclose/fscanf; don't use some of the first series and some of the second series.

Writing programs

For programs, correctness is an important criterion, but is by no means the whole story. Here are some suggestions for programming in general.

What to submit

Submit prog1.c, Makefile, and a README file. Do not submit other files. To create your submission, tar and compress all files that you are submitting. There is a recipe in the Makefile that we provide:

make submit

The README file must be a text file, not a file in MS Word, PostScript, PDF, or any other non-text format. README should include at least your name, a list of the files you are submitting, a short description of the project, any known bugs, and a short description of your testing.

Upload your submittal via https://www.cs.uky.edu/csportal/. You can do so from a browser on your VM, or you can copy it to some other machine and submit it with a browser from there. You may upload it as many times as you like; we grade only the last submission. Once we have graded a submission, we ignore later submissions.