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:
-
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.
-
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().
-
Submit your tested work using the
icon
submit mechanism.