CITS2002 Systems Programming  
 

Unit home

Final exam

help2002

Lecture & Workshop
recordings on LMS

Schedule

FAQ

C textbooks

OS textbooks

Information resources


Extra reading

Past projects

Recent feedback


Working effectively

Look after yourself!

Workshop 7: Using structures and reading directories

In recent lectures we introduced a number of important concepts - C structures, ways that an operating system can represent time, and how to read the contents of a directory. All of these concepts are used frequently in systems programming, and their use is seen in utility programs such as ls.

For this workshop we'll combine these concepts in a very simple program named myls.

Before the workshop session you're strongly encouraged to think how you would do it. Experiment with the true ls program, if necessary. You are not expected to have developed and tested a perfect solution before the workshop, but you should at least scratch out your approach on paper. Don't forget, the online manuals for the necessary system-calls and functions describe how they are called, and (often) the fields of the necessary C structures.


Consider the problem in this sequence:

  1. Firstly, consider a program that accepts a directory name, as its only command-line argument, and simply lists the names of each entry (the files and subdirectories) in that directory.

    The program will require the opendir(), readdir(), and closedir() functions.

  2. Extend the program to print (just as an integer) the modification-time of each directory entry.

    We need to understand the use of struct stat, and to call the stat() system-call.

  3. Extend the program to print (now as a more useful string) the modification-time of each directory entry.

    We now need to call the ctime() function.

  4. 🌶 Extend your program to list the directory entries sorted by their the modification-time.
    Remember that a file's modification-time is represented by an integer in its struct stat, so sorting these integers will be equivalent to sorting the files' times.

    We now need to save a copy of each file's name and modification-time, and we can best "keep these together" in a new structure. We'll need an array of these structures, but we won't know how many elements it requires until the program runs - we can use the realloc() function to allocate/grow the required space.

    Now, this is quite difficult - we need to sort the array of structures, using the modification-time field as the key for the sorting.
    Here is a simple tutorial on how to do it, using the standard qsort() function.

 

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]