Week 08 Laboratory Exercises
Objectives
- 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
Create a new directory for this lab called lab08
,
change to this directory, and fetch the provided code for this week
by running these commands:
mkdir lab08 cd lab08 1521 fetch lab08
Or, if you're not working on CSE, you can download the provided code as a zip file or a tar file.
Exercise — in pairs:
Print Files Sizes
Write a C program, file_sizes.c
, which is given one or
more filenames as command line arguments. It should print one line
for each filename which gives the size in bytes of the file. It
should also print a line giving the combined number of bytes in the
files.
Follow the output format below.
Do not read the file - obtain the file size from the function stat.
dcc file_sizes.c -o file_sizes ./file_sizes bubblesort.c print_bigger.c swap_numbers.c unordered.c bubblesort.c: 667 bytes print_bigger.c: 461 bytes swap_numbers.c: 565 bytes unordered.c: 486 bytes Total: 2179 bytes ./file_sizes bubblesort.s print_bigger.s swap_numbers.s unordered.s numbers1.txt numbers2.txt sorted.txt bubblesort.s: 1142 bytes print_bigger.s: 1140 bytes swap_numbers.s: 1173 bytes unordered.s: 791 bytes numbers1.txt: 59 bytes numbers2.txt: 55 bytes sorted.txt: 21 bytes Total: 4381 bytes
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest file_sizes
When you are finished working on this exercise, you and your lab
partner must both submit your work by running give
:
give cs1521 lab08_file_sizes file_sizes.c
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
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 file_sizes.c file_sizes -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 file_modes -rw-r--r-- 1 z5555555 z5555555 2934 Nov 2 12:59 file_modes.c ./file_modes file_modes file_modes.c file_sizes file_sizes.c -rwxr-xr-x file_modes -rw-r--r-- file_modes.c -rwxr-xr-x file_sizes -rw-r--r-- file_sizes.c chmod 700 file_modes chmod 640 file_sizes.c chmod 600 file_modes.c ls -ld file_modes.c file_modes file_sizes.c file_sizes -rwxr-xr-x 1 z5555555 z5555555 116744 Nov 2 13:00 file_sizes -rw-r----- 1 z5555555 z5555555 604 Nov 2 12:58 file_sizes.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 file_sizes file_sizes.c -rwx------ file_modes -rw------- file_modes.c -rwxr-xr-x file_sizes -rw-r----- file_sizes.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 and your lab
partner must both submit your work by running give
:
give cs1521 lab08_file_modes file_modes.c
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
Exercise — in pairs:
Compile C Files If Needed
compile_if_needed.c
, a C program that given the pathname of single file C programs
compiles them. For example:
dcc compile_if_needed.c -o compile_if_needed ./compile_if_needed file_sizes.c file_modes.c /usr/local/bin/dcc file_modes.c -o file_modes /usr/local/bin/dcc file_sizes.c -o file_sizesUnfortunately the code you have been given is inefficient. It recompiles the C file even if this is not necessary.
Modify compile_if_needed.c
so that it only compiles C
files if necessary.
A compilation is necessary if the binary doesn't exist.
A compilation is also necessary if the C file has been changed since the last compilation. In other words, if the modification time of the C file is more recent than the binary.
Follow the output format below.
./compile_if_needed file_sizes.c file_modes.c /usr/local/bin/dcc file_modes.c -o file_modes /usr/local/bin/dcc file_sizes.c -o file_sizes ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling rm file_sizes ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling /usr/local/bin/dcc file_sizes.c -o file_sizes echo >> file_sizes.c # add a new-line to file_sizes.c ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling /usr/local/bin/dcc file_sizes.c -o file_sizes ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling ./compile_if_needed file_sizes.c file_modes.c file_modes.c does not need compiling file_sizes.c does not need compiling
When you think your program is working, you can use
autotest
to run some simple automated tests:
1521 autotest compile_if_needed
When you are finished working on this exercise, you and your lab
partner must both submit your work by running give
:
give cs1521 lab08_compile_if_needed compile_if_needed.c
Note, even though this is a pair exercise, you both must run
give
from your own account before
Wednesday 01 January 00:00
to obtain the marks for this lab exercise.
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.
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 lab08_lsld lsld.c
You must run give
before Wednesday 01 January 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 Wednesday 01 January 00:00 to submit your work.
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 runningautotest
.)
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