Sunteți pe pagina 1din 3

Out: Feb 27/Mar 1, 2018

Due: Mar 20/22, 2018

CENG251 – Lab 3: File Properties

A time log following the format of the 1st 2 labs is required. (3 marks)

Part I: More Small Functions (15 marks)

In all cases provide a program listing, the command lines used to run the program, a status statement
indication of any problems or how you verified the program worked and the output. All of these
programs are to be written in C – several people have started on the wrong foot by looking at stat in
section 1 of the manual not section 2. Refer to the posted examples on the course web site.

1. Apply the stat function to a list of files on the command line. Print out the file size in bytes, the
total amount of space it takes up on the disk (block size * the number of 512B blocks) and the
%utilization (file size/space taken up on disk) (2 marks)

2. Apply the fstat function to a list of 4 files on the command line where the files are in different
system directories such as /usr/bin, $ORACLE_HOME, one of your directories with 2 files
belonging to different groups. Avoid using a directory as data. Unlike stat, fstat requires that
you open the file first to obtain a file descriptor so choose files that you can read.

Use the getpwuid and getgrgid functions to list each file owner’s userid, real name and group
name. (3 marks)

3. Write a C program that takes 2 arguments on the command line. The first argument is a
directory and the 2nd is a file in that directory.

The main program checks that both exist and that the first argument is a directory. It then calls
a function that first lists up to 5 files in the directory with an inode that is less than the 2nd file,
then up to 5 files with inodes greater than the current file. The output should show the file
names and the inode numbers. (3 marks)

4. Write a C program that implements chmod octalMode file1 file2 file3 ….


Use the trick used in the processNumbers.c example shown in class to convert the octalMode
into a string. Try this with 0000, 0777 and an odd random pattern.
Have the program generate a command to verify that the change has taken place.
(3 marks)
5. Write a C program that implements a version of the touch command. It should take 3
arguments. The first should be a file name, the 2nd and 3rd are both numbers representing
seconds since Jan 1, 1970 (EPOCH time). Any positive value up to 2 billion will do. (4 marks)
a. If the file does not exist, create it. (1)
b. Use C’s utime function to set the access and modification time of the file to the values in
arguments 2 and 3. (Remember to convert them from a string). I will discuss utime in
class but I will not provide a working example – you are to use the man page and the
text as a reference.
c. Have the program generate a command to verify that the change has taken place. (1)

Part II: Copying a Directory Structure (12 marks)

Write a C application that takes 2 command line argument – a source directory and a destination
directory. Your program should make a copy of the structure of the directory so that the file names,
modification and last access times, permissions and file types (directory and regular files only) are the
same. You do not have to copy the file content in all cases.

1. If the file name is . or .. don’t make a copy – skip the entry. Your new directory will have these
anyway. (1)

2. Your program should also detect and skip any symbolic links. (1)

3. If the file is of type directory make a new directory, however you do not have to duplicate the
contents of that directory. (1)

4. Set the modification and access times of the new file to be the same as the old file. (2)

5. Set the permissions of the new file to be the same as the old file. (2)

6. If the size of the old file is less than 40K in size, use fread and fwrite to copy it to the new
directory. This should be done before setting the times. (2)

7. The selection of test data is important. You may have to create a directory to test all features of
the program or perhaps you can find one. Discuss why your chosen test directory is a
reasonable choice. (1)

8. Status statement and proof that your program works. (2)


Part III – Explorations (8 marks)
1. Work your way through the gab sample exercise provided in the Week 5 Supplementary notes.
Write up how far you got and what you learned.

This exercise is best done in the lab in week 7 when you are otherwise busy studying for exams.
I don’t ask you to spend much time outside of the lab this week, which is why this is a 3 week
not a 2 week assignment. (4 marks)

Bonus: Describe how you used gdb to solve a debugging problem in one of the previous
exercises. (2 marks)

2. Preprocess one (or more) of your programs using the -E option


a. Record and describe 2 typedefs that you found other than time_t or size_t. The
typedefs should be in different files. (1)
b. Look up and record the definition of both _IO_FILE and stdin, stdout and stderr In
what file(s) were they declared? Go into each file in the /usr/include directory and
record the line numbers they are on. (1)
c. Preprocess your getopt loop program. What are the declarations for optind, optopt and
optarg? (1)

3. In Lab 2 you probably created a .h file for your data type declaration. (or use either my PERSON
or FLIGHT example). Move the .h file to a subdirectory. Recompile using the -I option. Record
the command line and report if the compile was successful.

There are 40 available marks listed on this assignment. It will be marked out of 35. You may
suggest and create extra features for up to 5 extra bonus marks if you contact me before the due
date and get approval. Duplicate requests will may be turned down so ask early. For example
you might decide to modify one of the exercises to use getopt or getopt_long flags or map out and
create a data structure diagram one of the data structures used by system function calls.

S-ar putea să vă placă și