Week 09 Laboratory Exercises

Objectives

  • learning how to retrieve environment variables
  • learning how to access file metadata via stat
  • learning how to use file metadata
  • practicing file operations generally
  • understand make's core operation

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started

Set up for the lab by creating a new directory called 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.

Exercise — individual:
Print Select Bytes from a File

Write a C program, print_select_bytes.c, which is given a filename and one or more positions as command line arguments. It should print the byte that is located at each given position within the file. It should print each byte in decimal, two digit hex, and (if possible) the character itself.

Follow the output format below.

Do not read the entire file - use fseek(3) to move around the file.

dcc print_select_bytes.c -o print_select_bytes
./print_select_bytes lorem-ipsum.txt 0 1 2 3 4
76 - 0x4C - 'L'
111 - 0x6F - 'o'
114 - 0x72 - 'r'
101 - 0x65 - 'e'
109 - 0x6D - 'm'
./print_select_bytes lorem-ipsum.txt 542 361 840 232 97 546 1612 2508 96 85 1382
116 - 0x74 - 't'
101 - 0x65 - 'e'
117 - 0x75 - 'u'
10 - 0x0A
101 - 0x65 - 'e'
108 - 0x6C - 'l'
32 - 0x20 - ' '
111 - 0x6F - 'o'
116 - 0x74 - 't'
114 - 0x72 - 'r'
116 - 0x74 - 't'

When you think your program is working, you can use autotest to run some simple automated tests:

1521 autotest print_select_bytes 

When you are finished working on this exercise, you must submit your work by running give:

give cs1521 lab09_print_select_bytes print_select_bytes.c

You must run give before Monday 15 April 12:00 (midday) (2024-04-15 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 15 April 12:00 (midday) (2024-04-15 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

We wish to maintain a simple diary in the 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 15 April 12:00 (midday) (2024-04-15 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

We need clean room implementation of the standard Unix program ls.

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.

Follow the output format below.

dcc lsld.c -o lsld
ls -ld lsld.c lsld file_sizes.c file_sizes /home/cs1521/public_html
drwxr-xr-x 6 cs1511   cs1511      128 Sep 16 08:02 /home/cs1521/public_html
-rwxr-xr-x 1 z5555555 z5555555 116744 Nov  2 13:00 file_sizes
-rw-r--r-- 1 z5555555 z5555555    604 Nov  2 12:58 file_sizes.c
-rwxr-xr-x 1 z5555555 z5555555 222672 Nov  2 13:00 lsld
-rw-r--r-- 1 z5555555 z5555555   2934 Nov  2 12:59 lsld.c
./lsld lsld.c lsld file_sizes.c file_sizes /home/cs1521/public_html
-rw-r--r-- 1 z5555555 z5555555   2934 Nov  2 12:59 lsld.c
-rwxr-xr-x 1 z5555555 z5555555 222672 Nov  2 13:00 lsld
-rw-r--r-- 1 z5555555 z5555555    604 Nov  2 12:58 file_sizes.c
-rwxr-xr-x 1 z5555555 z5555555 116744 Nov  2 13:00 file_sizes
drwxr-xr-x 6 cs1511   cs1511      128 Sep 16 08:02 /home/cs1521/public_html

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 15 April 12:00 (midday) (2024-04-15 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

When you are finished each exercises make sure you submit your work by running 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