Machine Problem 4, due Apr 7

Part of the homework for 22C:112, Spring 2008
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Background: The shell command cat f copies file f to standard output by opening the file and reading it using a sequence of calls to the read() system service.

Linux (and several other Unix variants) allow a second mechanism to read a file. The mmap() system service can be used to map a file into the virtual address space of a running program. (use the shell command man to get documentation.)

In Linux and other Unix variants, you can get the size of an open file by calling lseek(fd,0,SEEK_END).

The Assignment:

  1. Write a C program that behaves like cat with a single parameter (don't worry about cat for copying standard input to standard output, nor command line arguments of cat nor the use of cat to concatenate multiple files). Your program must use mmap() to map the file indicated by the command line argument into memory, and then it must copy this file to standard output by copying bytes from memory.

  2. Your program must terminate with an appropriate error indication if someone tries to open a file that is not readable or is too big to map into memory. Error messages should begin with the program name (argv[0]) followed by a colon followed by the filename followed by an indication of the problem. At minimum, distinguish between "cannot open for reading" and "cannot map into memory". Error messages should be directed to standard error (stderr); you may need to read the man page on fprintf().

  3. Submit your tested work using the icon submit mechanism.