Operating Systems and Computer Structures G2
Assignment 2, 1996

Due date

This assignment is due on Sunday, 23 September at midnight.


In assignment one, you had to take a list of image files (in GIF format) and feed them into xview. A question raised by some students was "how can one ensure that a file is really a GIF file?"

The command file will give this kind of information:

    file bart.gif
    GIF picture - version 87a 43 x 58, interlaced, 2 colors

This assignment is to write a specialised version of the file command that could be used in assignment one (or in other problems) to test and handle GIF files appropriately.

GIF file format

Most file types start with a special set of bytes to label the type of file. This information for many file types is held in the file /etc/magic under Unix (see man magic). Examination of the byte structure of files can be done using the command od (see man od). For GIF files, the first three bytes hold the characters "GIF" (od -c file.gif | more shows this). The next three bytes give the GIF version number as "87a" or "89a" and the next byte shows how many colours are possibly present in the image.


You must write a program in ANSI C or C++ to determine if a file is a GIF file or not. The program must be able to be used in a variety of ways and give a variety of outputs. If you call your executable giftest, the command would be used as
    giftest [-v] [-s] [-h] [file]
meaning that it can be called with an option -v, an option -s, an option -h (or any combination in any order), and an optional filename. The normal Unix convention that the filename must be after any options will be followed.

If called with no options, but with a filename, then the program should determine if the file is a GIF file. If it is, then it should write the name of the file to standard output. If it is not, then it should write nothing to standard output, but just terminate. In addition, the exit code should be zero if it is a GIF file, non-zero otherwise. An example use could be

    giftest filename | xview -p
(An extended version of xview will be made available which has an option to be permanent, and not disappear when its input terminates.)

If no filename is given, then the command should read a filename from standard input. If the file is a GIF file the filename should be copied to standard output, otherwise no output should be written. The exit code should be zero for a GIF file, non-zero otherwise. An example use could be

    echo filename | giftest | xview -p

If the -s option is given, no output should be written to standard output. Only the exit code should be given An example use could be

    if giftest filename
       echo filename | xview -p

If the -v option is given, then information about the file should be written to standard error. This should include whether or not it is a GIF file, and if so what version of GIF it is.

If the -h option is given, a help message telling the way to use the command should be written to standard error.

Exit codes

If the file is a GIF file, the exit code should be zero. If the file is readable, but is not a GIF file, then the exit code should be one. If any error occurs in file access, then the exit code should be larger than one, and an appropriate error message should be printed to standard error. You may wish to examine the variable errno or use the library function strerror() to phrase these error messages and exit codes.


The assignment may be written in ANSI C or C++. Students who studied C++ last semester are recommended to use C++, but you can use C. Students who have not studied C++ are recommended to use C, but you can use C++. If you use C++, it would help to signal this in your source code. A convention used by emacs is to look for lines ``-*- language -*-'' as a clue. So if you include a line
// -*- C++ -*-
in your source, it would help me to invoke the right compiler.


Do not hand in a copy of the program on paper. Instead, hand it in electronically using the command give. This command runs with two parameters. The first parameter should be the string ``a2'' and the second should be the name of the file containing the source of your shell program. For example,

    give a2 myass2.c

You may run give any number of times. Each time you run give it replaces any earlier submission of the assignment.

Submit only the source code. Do NOT give the executable file!!! I can't extract the source code from an executable, so it can't be marked. If you give an executable, then it will not be tested or marked and you will have to resubmit it.