Assignment 2

Due date

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


This assignment begins the process of turning the shell program of assignment 1 into a C program. It replaces part of the shell program that processes the output from the xselect program. That is, it reads a set of filenames and checks if they are in the cache. If not, it fetches the file and stores it in the cache. Then it writes it to the sound device.

The concepts that are used in this assignment are

Supplied functions

This assignment uses a function that is not in the standard C or Unix libraries. The function is char *fetch_sound(char *filename) You will need to add this function at link time. The file is stored in the library ~ostutes/libA2.a. If your source is a2.c that you are compiling to a2, use this command line: gcc -g -o a2 a2.c -L/student1/os/ostutes -lA2

The function takes a filename as parameter, that hopefully is one that came from the program list_sounds. It returns an array of characters (bytes), NULL terminated (i.e. a string) if the file can be fetched. If not, it returns NULL. This array is dynamically allocated by fetch_sound. Your program is responsible for freeing the memory when it is no longer needed.


Write a program in ANSI C. When your program starts it should create the directory ``.sounds'' in the current directory.

The program reads lines from standard input. Each line is expected to be the name of a file. It terminates on EOF from stdin.

If the file does not exist in the directory .sounds then a file of that name must be created in the cache directory. The contents of the file should be from a call to fetch_sound.

The file should be copied to /dev/audio.

When EOF is reached on input, the cache directory and all its contents should be removed and the program should terminate normally.


All function calls should be checked for errors. If an error occurs a message should be posted using fprintf() to stderr. The program should terminate if the error is fatal.


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.

Other comments

If you want to use dialin access to work then some programs will need to be replaced. See here for more information.

If you are running Linux, then you will need binary copies of some programs. See here for more information.

Creative Commons License Copyright © Jan Newmarch under the terms of the Creative Commons Attribution 2.1 Australia License.