Pre-lab 11

The Unix File System Interface

Goals

Gain more practice in working with the Unix file system interface:This lab will have you work with functions to inspect various attributes of files, as you modify a program for traversing a directory tree.

Gain more practice with the higher level file I/O interface:By now, you have used the C/Unix file I/O APIs for lower level syscalls (open(2), read(2), etc.) and for higher level streams (fopen(3), fread(3), etc.). It pays off to think critically about the differences between the two kinds of interface, so that you understand a little more about the design of the operating system.

Understand how Unix differentiates file types: In some operating systems, a file name extension is used to designate a file type, so that both the user and the system know what type of application generated the file and/or what type of application can manipulate that file. In Unix, file name extensions are considered just part of the file name and defined by conventions. In this assignment, you will work to develop the understanding of how Unix can figure out file types in the absence of an authoritative extension or any other type of metadata.

Credits

This pre-lab assignment was created by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this credits note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e-mail to perrone[at]bucknell[dot]edu

Set Up

In your git repo, create a working directory for this lab:

~/csci315/Labs/Lab11/

Copy the following directory recursively to your git repository; this will be your working directory for this lab:

~cs315/Labs/Lab11/

In your top level directory for Lab11, create a file called prelab.txt.

Problem 1 (15 points)

Create a C program called fdump.c that works as described below.

The program must accept the following command line parameters, in the order given: filename (a C array), offset (unsigned integer), and size (unsigned integer). If the three command line parameters are not provided by the user, the program must terminate immediately with an error message and indicate the proper usage (that is, the order and the types of command line parameters expected).

The program opens the file indicated by filename with fopen(3), moves forward the file position indicator by the number of bytes indicated by offset, and reads size bytes from filename into a buffer. (Hint: you will need to make a call to a random access library function to get to the right read location into the user-specified file.)

Once that data is read into your program’s buffer, make it call the function hexdump provided to you in files hexdump.h and hexdump.c. The output generated will resemble the exemple below, which is similar to what is produced by the xxd Unix utility.

1

2

3

4

5

6

7

8

9

0000000:d4c3 b2a1020004000000000000000000................

0000010:ffff00000100000047c5a943 fd140300........G..C....

0000020:2a0000002a000000ffff ffff ffff0001*...*...........

0000030:039cffbd080600010800060400010001................

0000040:039cffbd c0a80166000000000000c0a8.......f........

0000050:010149c5a9435eed0d00400000004000..I..C^...@...@.

0000060:00000001039cffbd000c41a1f5da0806..........A.....

0000070:0001080006040002000c41a1f5da c0a8..........A.....

...

Close the file and terminate.

Create a Makefile to generate your fdump executable. You should compile hexdump.c separately into an object that gets linked with the compilation of fdump.c at a later point.

When you are done with this, you need to:

cd ~/csci315/Labs/Lab11

git add hexdump.h

git add hexdump.c

git add Makefile

git add fdump.c

git commit -m “Pre-lab 11.1 completed”

git push

Problem 2 (15 points)

When you are done coding and debugging, record your answers to the questions below in prelab.txt:

(2.1): Run your fdump program with the following parameters: filename = hexdump.c, offset=1000, size=128. Explain what you see.

(2.2): Run your fdump program with the following parameters: filename = fdump, offset=500, size=128. Explain what you see.

(2.3): Compare the output you produced for answers (2.1) and (2.2). Looking at the hexadecimal dump on the left, in both cases, makes it clear that inside both files, you store information in binary encoding. However, the data to the right of the hexadecimal dump shows something human-readable for (2.1) and non-human readable for (2.2). In this answer, you are asked to explain why this is the case.

(2.4): Now, run thefile(1) command on the files below and do your best to interprete the results. (You can always look at the man page for file to learn what it does…)

/usr/bin/file,

~cs315/Labs/Lab11/work

~cs315/Labs/Lab11/beauty

Your separately compiled hexdump.o

hexdump.h

Do some research to discover how file(1) figures out the type of content in these files. Record your findings in this answer along with citations to the sources you used.