NOTE: This assignment is for individual work only and not a group project. See my class policies. If I find code that is too similar I will assume it was simply copied and the Draconian rules of my class policies apply. Let's not go there. If you need help send me email, show up during office hours, or at CSAC during hours.

This assignment will give you practice using Linux system and library calls from C++.

The Task

You are to write a Linux command called key. Here is the man page. The test script may answer questions about the command that are not answered by the man page. If you still have questions, ask. I will probably expand the test file as the assignment progresses. Note that the test file tests the exit code after every call.

Coding Requirements

You must:

  1. Use open, lseek, write, close and read system calls using file descriptors. You use these for all I/O to the file.
  2. Use getopt to get all the options, the key, and the file. The later two are what may be left over after the options are removed. I will assume the standard getopt and not the GNU getopt. You may add an option called d if you want to have an option to turn on your own debugging. I won't assume that is an error and will not invoke it in the tests.
  3. Use the malloc command to allocate space for a buffer to hold the key and and value. You cannot use a fixed size buffer for reading and writing of key and value.
  4. Use the strcmp library routine to compare strings.
  5. The file format has a header which is a 4 byte magic number of "KEYZ" followed by 3 integers: maximum key size, maximum value size, and maximum number of save key-value pairs. The numbers are stored as sizeof(int) bytes per number.
  6. The file consists of fixed sized records each of which is 2*sizeof(int) + (maximum key size) + (maximum value size) bytes long. There are up to max number of save pairs of records. The first number is the actual length of the key in bytes. The second number is the actual length of the value in bytes. This means you can store null characters in strings. If the length of the key is zero (0) then that record is unused.
  7. Use this hash function to do a hash with linear probing as described in class to store your data. There is no remove command for this hash so you need not handle the deletion case.

You are NOT allowed to:

  1. Read in the entire file or write out the entire file. You must read and write only the data that you need to using the lseek command and read and write.
  2. You are not allowed to use fprintf or fscanf to read or write from the key file.
  3. Use the C++ new command and delete commands to allocated space. Use the malloc library call instead.
  4. Use I/O commands based on the FILE * type. This includes fread, fwrite, fopen.
  5. Parse your options without using getopt.
  6. Use the C++ string type.
  7. Use cin or scanf.


  1. As we have seen in class, the man pages are your friend. Refer to them as you code. The useful routine strcmp is a library call and so is in section 3. write is a system call and so is in section 2. Just like in class. We intentionally did not cover strcmp in class, but we did see how to use man pages. This should be easy now.
  2. You can write and read an integer by using the address of the integer and giving the size sizeof(int).
  3. When you allocate space with malloc be sure to cast the type:
    char *space;
    space = (char *)malloc(size);
    You can then use the space as the pointer in the read and write.

Error Messages

Your program must generate the following exact error messages to stdout (not stderr) when called for:

"ERROR(key): option -%c is not an option\n"
"ERROR(key): Cannot create file '%s'\n"
"ERROR(key): File '%s' is not of type key file.\n"
"ERROR(key): No key was specified for key file '%s'\n"
"ERROR(key): Attempt to overwrite record for key '%s' when in no-overwrite mode\n"
"ERROR(key): Trying to get value from key file '%s' for nonexistent record for key '%s'\n"
An error code of 1 is given for any of these errors otherwise 0 is returned. If the -h is specified or the wrong option is given then this usage message is also printed:
Usage: key {-s | -g} file key
       key -[hnkvp] file


To test your code and better understand the definition of the functions there is a tar in the sidebar that contains a mock directory structure. Included is also a test script and the expected output when the test script is run in the same directory where the tar is exploded. This will be discussed in class. You can of course learn more about tar right now by accessing the man-page for tar by doing the command: man tar.


Homework will be submitted as an uncompressed tar file via BBLearn. The tar file should contain one file: key.cpp. I will compile that file with: g++ key.cpp -o key on a machine that is configured like wormulon, which you have access to.

You can submit as many times as you like. The LAST file you submit BEFORE the deadline will be the one graded.

Have fun.