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!

Labsheet 5 - for the week commencing 25th September 2023

Tasks

  1. Write a C program, named listints, which will print the integers requested via a single command-line argument. The list of integers is to appear in strictly increasing order, with each requested integer appearing once and only once (even if duplicated in the request).

    Simple examples of its use are:

    prompt> listints 8 will print: 8 prompt> listints 3,5,9 will print: 3 5 9 prompt> listints 1-10 will print: 1 2 3 4 5 6 7 8 9 10 prompt> listints 1-10,6 will print: 1 2 3 4 5 6 7 8 9 10

    🌶 🌶 And some much more difficult examples:

    prompt> listints 2000-2020,40-50 prompt> listints 1-10,2010-2020,3000000-3000010

  2. Locate a single-file program consisting of several functions, such as the one developed in Workshop-3 - and its solution. Or maybe your own 1st project.

    Now, break the single-file program into a multi-file project consisting of:

    • a single C header file (providing the function declarations), and
    • several C source code files (with each C file providing at least one function definition).

    Next, develop a simple Makefile, as introduced in Lecture-17, to compile and link your multi-file project using make.

    Determine how to test that your Makefile correctly represents the dependencies between your source and header files.

    Extend your Makefile by adding a new target to cleanup (remove) any unnecessary files (careful!).

  3. Both Linux and macOS provide a standard command named wc (an abbreviation for wordcount!) which determines the number of lines, words, and characters in a named file. You can read about this command using the online documentation: (man wc).

    For this task, you will develop your own version of the wc program named mywc.

    1. Firstly, write a function named counter() that calculates and prints out the number of lines contained within a file. Your counter() function should take one argument, a character array that provides a filename.

    2. Next, modify your mywc program and the counter() function to either count the lines of a file, named on the command-line, or to count the input "arriving" via the stdin stream. We often described such a program as a filter.

    3. Next, extend your counter() function so that it now also counts, and prints, the number of characters and words found in the file. Be careful with the meaning of a "word" - it's just a sequence of any characters surrounded by whitespace characters. Check your printed results against those of the standard wc program.

    4. Next, observe that our counter() function is printing its three results - as we can only return a single result. Modify the counter() function so that its three calculated results are now "given" back to the calling function through pointers passed as parameters. Now, the calling function will have the results placed in its own local variables, and be able to print (or use) the results itself.

  4. 🌶 Now, using the standard getopt() function introduced in Lecture 18, add support for command-line options to your mywc utility from Task-3.

    Add command-line options to request:

    • -c   to report the number of characters,
    • -l   to report the number of lines,
    • -L   to report the maximum line length (as supported on Linux, but not macOS), and
    • -w   to report the number of words.

    Ensure that your program still works with meaningful defaults if no options are provided on the command-line.

    Ensure that your program checks that the provided command-line arguments are sensible - if they are not, issue an appropriate error message to stderr after all options have been processed.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]