Week 09 Laboratory Exercises
Objectives
- practicing file operations
- learning how to access file metadata via stat
- learning how to work with UTF-8
- learning how to retrieve environment variables
- learning how to recursively search directories
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
lab09 and changing to this directory.
mkdir lab09 cd lab09
There are some provided files for this lab which you can fetch with this command:
1521 fetch lab09
If you're not working at CSE, you can download the provided files as a zip file or a tar file.
Check Off
While these exercises are not conducted under exam conditions and lab assistants will be available to provide guidance, they are a mandatory requirement designed to ensure you are consistently engaging with the core concepts in preparation for the final exam.
To complete this activity each week, you will be required to enter an access code provided to you by your tutor during the lab session.
If you cannot attend your scheduled lab session, you must email the course account cs1521@cse.unsw.edu.au.
You can access the check-off exercise for this week here: Check-Off Exercise.
Exercise — individual:
Print File Modes
Write a C program, file_modes.c, which is given one or more pathnames as command line arguments.
It should print one line for each pathnames which gives the permissions of the file or directory.
Follow the output format below.
dcc file_modes.c -o file_modes ls -ld file_modes.c file_modes diary.c diary -rwxr-xr-x 1 z5555555 z5555555 116744 Nov 2 13:00 diary -rw-r--r-- 1 z5555555 z5555555 604 Nov 2 12:58 diary.c -rwxr-xr-x 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw-r--r-- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c ./file_modes file_modes file_modes.c diary diary.c -rwxr-xr-x file_modes -rw-r--r-- file_modes.c -rwxr-xr-x diary -rw-r--r-- diary.c chmod 700 file_modes chmod 640 diary.c chmod 600 file_modes.c ls -ld file_modes.c file_modes diary.c diary -rwxr-xr-x 1 z5555555 z5555555 116744 Nov 2 13:00 diary -rw-r----- 1 z5555555 z5555555 604 Nov 2 12:58 diary.c -rwx------ 1 z5555555 z5555555 222672 Nov 2 13:00 file_modes -rw------- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c ./file_modes file_modes file_modes.c diary diary.c -rwx------ file_modes -rw------- file_modes.c -rwxr-xr-x diary -rw-r----- diary.cThe first character on each line should be '-' for ordinary files and 'd' for directories. For example:
./file_modes /tmp /web/cs1521/index.html drwxrwxrwx /tmp -rw-r--r-- /web/cs1521/index.html
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest file_modes
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_file_modes file_modes.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Find the First Invalid UTF-8 Byte
You have been given invalid_utf8_byte.c,
which contains a C function invalid_utf8_byte,
that takes a string and returns 42.
Add code to the function invalid_utf8_byte so that,
given a string containing one or more invalid UTF-8 sequences,
it returns the index of the first unexpected byte in the string,
and -1 if the string if the string contains no invalid UTF-8 sequences.
For example:
unzip invalid_utf8_byte_examples.zip (several lines of output) xxd invalid_utf8_byte_examples/hello_world.txt 00000000: 6865 6c6c 6f20 776f 726c 64 hello world ./invalid_utf8_byte < invalid_utf8_byte_examples/hello_world.txt No invalid bytes found. xxd invalid_utf8_byte_examples/bad_hello_world.txt 00000000: 6865 6c6c 6fa1 776f 726c 64 hello.world ./invalid_utf8_byte < invalid_utf8_byte_examples/bad_hello_world.txt Invalid byte found at index 5. xxd invalid_utf8_byte_examples/too_few_continuation_bytes.txt 00000000: f09f 98 ... ./invalid_utf8_byte < invalid_utf8_byte_examples/too_few_continuation_bytes.txt Invalid byte found at index 3. xxd invalid_utf8_byte_examples/too_many_continuation_bytes.txt 00000000: f09f 98b3 98 ..... ./invalid_utf8_byte < invalid_utf8_byte_examples/too_many_continuation_bytes.txt Invalid byte found at index 4. xxd invalid_utf8_byte_examples/valid_with_emoji.txt 00000000: 796f 7520 6172 6520 646f 696e 6720 6772 you are doing gr 00000010: 6561 7420 f09f 918d f09f 918d f09f 918d eat ............ ./invalid_utf8_byte < invalid_utf8_byte_examples/valid_with_emoji.txt No invalid bytes found. xxd invalid_utf8_byte_examples/invalid_with_emoji.txt 00000000: 7468 6973 f0a1 3923 7374 7269 6e67 a269 this..9#string.i 00000010: 73e7 8936 6e6f 74a0 646f 696e 67c2 c273 s..6not.doing..s 00000020: 6fa0 6772 6561 74f1 3242 a068 6f77 6576 o.great.2B.howev 00000030: 6572 20f0 9f98 ad91 er ..... ./invalid_utf8_byte < invalid_utf8_byte_examples/invalid_with_emoji.txt Invalid byte found at index 6.Further example inputs are provided in the
invalid_utf8_byte_examples.zip archive.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest invalid_utf8_byte
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_invalid_utf8_byte invalid_utf8_byte.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Append to a Diary File
$HOME/.diary
Write a C program, diary.c, which appends 1 line to $HOME/.diary.
The line should be its command-line arguments separated by a space followed by a '\n'.
$HOME represents the HOME environment variable. This means you will need to retrieve
the value of the environment variable, join it with the name of the file we wish to open and then use the resulting
pathname in order to open it.
diary.c should print nothing on stdout. It should only append to $HOME/.diary.
dcc diary.c -o diary ./diary Lisa cat $HOME/.diary Lisa ./diary in this house ./diary we obey the laws of thermodynamics cat $HOME/.diary Lisa in this house we obey the laws of thermodynamics
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest diary
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_diary diary.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
Recursively print directory contents
Write a C program, find,
which takes the name of a directory as an argument,
and recursively finds and prints the name of all files
and subdirectories inside it.
The program should also print the name of the provided directory.
unzip -oqq find_examples.zip ./find find_examples/a | sort find_examples/a find_examples/a/b find_examples/a/b/c find_examples/a/d find_examples/a/e find_examples/a/e/f find_examples/a/e/g
You can unzip the provided archive find_examples.zip to test with the examples used by autotest.
You can assume that the provided pathname refers to a valid directory. You can assume that this directory contains only regular files and more directories. You can assume the maximum length of the pathname for a nested file is 1023 characters.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest find
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_find find.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Challenge Exercise — individual:
ls -ld
Write a C program, lsld.c, which is given zero or more pathnames as command line arguments
produces exactly the same output as ls -ld given the same pathnames as arguments.
Except ls -ld sorts its output lines. You do not have to match this.
You should exactly match the output format of ls -ld. An example follows, although your output will depend on the metadata associated with the arguments that your provide.
dcc lsld.c -o lsld mkdir -m 751 -p test_dir ls -ld test_dir invalid_utf8_byte_examples/hello_world.txt invalid_utf8_byte_examples lsld.c /home/cs1521/public_html drwxr-x--x 38 cs1521 cs1521 4096 May 30 19:26 /home/cs1521/public_html drwxr-xr-x 2 z5555555 z5555555 4096 Apr 10 2023 invalid_utf8_byte_examples -rw-r--r-- 1 z5555555 z5555555 11 Apr 10 2023 invalid_utf8_byte_examples/hello_world.txt -rw-r----- 1 z5555555 z5555555 1521 Jul 10 11:51 lsld.c drwxr-x--x 2 z5555555 z5555555 4096 Jul 10 11:44 test_dir ./lsld test_dir invalid_utf8_byte_examples/hello_world.txt invalid_utf8_byte_examples lsld.c /home/cs1521/public_html drwxr-x--x 38 cs1521 cs1521 4096 May 30 19:26 /home/cs1521/public_html drwxr-xr-x 2 z5555555 z5555555 4096 Apr 10 2023 invalid_utf8_byte_examples -rw-r--r-- 1 z5555555 z5555555 11 Apr 10 2023 invalid_utf8_byte_examples/hello_world.txt -rw-r----- 1 z5555555 z5555555 1521 Jul 10 11:51 lsld.c drwxr-x--x 2 z5555555 z5555555 4096 Jul 10 11:44 test_dir
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest lsld
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_lsld lsld.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Challenge Exercise — individual:
Print The Last line of Huge Files
Write a C program, last_line,
which takes one argument, a filename,
and which should print the last line of that file.
For example:
dcc last_line.c -o last_line echo -e 'hello\ngood bye' >hello_goodbye.txt cat hello_goodbye.txt hello good bye ./last_line hello_goodbye.txt good bye
You program should not assume the last byte of the file is a newline character.
echo -n -e 'hello\ngoodbye' >no_last_newline.txt hello goodbye./last_line no_last_newline.txt goodbye
Your program should handle extremely large files. It should not read the entire file. As this is a challenge exercise, marks will not be awarded for programs which read the entire file.
For example, it should be able to print the last line of a one-terabyte file:
echo -e 'Hello\nGood Bye'|dd status=none seek=1T bs=1 of=/tmp/gigantic_file$$ ls -l /tmp/gigantic_file$$ -rw-r--r-- 1 z5555555 z5555555 1099511627791 Oct 26 17:27 gigantic_file12345 ./last_line /tmp/gigantic_file$$ Good Bye
The gigantic file created above is a sparse file, consisting almost entirely of zero bytes: it uses little actual disk space, but, to be safe, remove it when you finish the exercise.
The commands above create the sparse file in /tmp to avoid it accidentally being backed up or otherwise copied.
Sparse files can create problems if they are accidentally copied by a program which doesn't handle them specially — and most programs don't.
BTW the $$ in the above command is replaced by the shell process id. This is because /tmp is shared so we'd like to use a filename that is (more or less) unique.
When you think your program is working,
you can use autotest
to run some simple automated tests:
1521 autotest last_line
When you are finished working on this exercise,
you must
submit your work by running give:
give cs1521 lab09_last_line last_line.c
You must run give
before Monday 03 August 2026 12:00 (midday) (2026-08-03 12:00:00)
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Submission
give.
You can run give multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 10 Monday 12:00:00 (midday) to submit your work without receiving a late penalty.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest runs for you.
(Hint: do your own testing as well as running autotest.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1521 classrun -sturec