Laboratory Assignment 3 Solution





For this 40 point lab, you will implement the cat command. This means, among other things, that you will not call cat from your program by using system() or exec() or other similar means.

Speci cs

You will write a program which will run correctly on the linux operating system. Your program will output the same results as running the command cat. For example, if you name your executable le kat, then there will be no di erence in output from running cat and running your program, kat.

Your program will also perform correctly with command line arguments b, E, and n. The argument ’E’ means write a ’$’ character at the end of each output line; ’n’ means write the line number before each line of output; and ’b’ is the same as ’n’ except that empty lines (lines containing just the newline character) are not labelled with a number, nor do they increase the line number. Zero, one, two, or all three arguments may be applied to a single run of cat. Whenever ’b’ and ’n’ are used on the same run of cat, the e ects are those of the argument ’b’. The command cat with no le name arguments takes its input from standard input. There can be any number of le names for command line arguments, which cat will concatenate and print on stdout.

Note that a “-” anywhere in the list of les stands for input from stdin, so you can mix stdin with le input. Note also that there can be several “-” characters in the list of les. To send an end of le from the keyboard, use the combination of keys CTRL and D.


Test your program with multiple les. Be sure that you have lines containing only blanks, lines containing no characters (other than the newline character to end the line) and lines that end in blanks, as well as lines that end in printable characters.


The output will be the same as running the command cat with the appropriate command line arguments.


  1. Actually run cat on a linux system before writing your code to see how it behaves. If you create test les le1, le2, and le3 which contain multiple lines, some lines with only white space in them, some lines which are completely empty, then run cat in these ways (and others) to see how it behaves:

cat le1 – -b le2 – le3 cat le1 -bEn le2 –

cat -n le1 – le3 le2 –

  1. the arguments -b,-n, and -E can be given in any order in the command line, and can be combined in any order. For example, all the following give the same result:

cat -b le -E -n cat le -E -b -n cat -bn le -E cat le -bEn


  1. Make sure your program behaves appropriately when supplied with a le which does not exist. (Note in particular which values go to stdout and which go to stderr, you can tell this by redirecting stdout with the ’>’ character, for example: cat a > outFile. //stdout goes to the le outFile, stderr goes to the screen).

  1. Remember that your command line arguments accessed by parameter argv of main are strings, not just arrays of char.


Submit your source code via Blackboard by 11:59:59p.m. on Tuesday April 23. You should comment your program similar to the example prime.c, but do not have your program print information which the linux cat program does not output. (For example, do not print title/author information).


error: Content is protected !!