Operating Systems 2230 Computer Science & Software Engineering Labsheet 3 By the end of Week 4, all students should have completed Questions 1–3 from this labsheet. This labsheet also provides a few additional questions for students needing a challenge. They are intended to both increase your understanding of C, and to test your design and programming skills. (Do not feel that you are behind in 2230 if you can’t complete all of these questions!) 1. Complete all questions from Labsheet 2. 2. [no true programming required] The program in /cslinux/examples/CITS2230/Lab3/queue.c is designed to demonstrate the use of a dynamic queue data structure. The queue is characterised as having a head, a tail, and a count of the number of elements in the queue. The elements could be anything: here they are just ’generic’ blocks of data and a pointer to the next element. The file consists of 3 main parts (appearing top-to-bottom in the file): the definition/declaration of the queue data structure, actual code to implement/define the queue data structure, and a main() function which demonstrates the use of the queue. Identify each of the three parts of the program. Break the single source file into three source files: a queue.h C header file, containing the declaration of the queue data structure; a queue.c C source file, containing the implementation of the queue data structure; and a testqueue.c C source file which calls the queue’s creation and manipulation functions. Write a Linux Makefile which compiles and links each of the C source files as necessary. Ensure that Makefile builds the whole “project” without any compilation or warning messages. 3. The Linux utility wc (short for “word count”) reports the numbers of characters, words, and lines in text files passed as command line arguments. It uses a simple definition of a word: a word is any sequence of characters separated from other words by one of the standard “whitespace” characters (space, tab, or a newline). Write your own simple version named mywc which reports the counts of all characters, words, and lines. It does not need to accept nor process any command-line flags. 4. The previous question introduced a simple way for you to detect “words” in a file. Can you write a C program which accepts one or more files on the command line, and guesses whether the file contains C or Java source code? Your program should not simply use the filename’s extension, but make its judgement based on the file’s contents. 1 5. The Linux utility grep locates text strings on lines in a file or its standard input stream. (For more information, run man grep). Write your own simple version of grep, named mygrep. grep permits the matching string to be a general pattern termed a regular expression. Using regular expressions we can look for general patterns, including wildcards, in files, e.g. grep “ˆ[A-Z]” file. searches for lines beginning with an uppercase character in file. Read the manual entry for regular expression support (man re comp) and add regular expression support to mygrep. 6. [hard] One thing grep does not do is provide some textual context in which the matching pattern occurs. It would be helpful if a matching line was displayed with a couple of lines displayed before and after it so that we can determine where the match was found. Add some argument processing to mygrep to indicate that some textual context should also be displayed. A simple invocation of this new form would be mygrep -2 +3 string file, which displays any matching lines in file, together with the two previous lines and the three following lines. 7. [hard] Consider the following (fictitious) ASCII file storing email aliases: lecturers: chris,lyndon,gordon,john,peter help2230: chris units: lecturers, students 2230tuteA: 2230tuteB,cs1,cs2 2230tuteB: 2230tuteA,cs3,cs4 students: cs3,cs4,cs1,cs2,cs5,cs1 Write a C program, whoto, which first reads the aliases and usernames from the file and stores them in appropriate data structures. Then, for each command line argument, the program should print a sorted list of usernames requested by that argument. For example, whoto units should print a sorted list of all “real” usernames from the alias file. Test your program with a number of different alias files, in particular one with some invalid entries. 2