Unix Systems Programming: Lab 3 - stat, file I/O, libraries & make
Due: Monday,
February 4 @ 5:00 pm
Purpose and Rationale
The purpose of this lab is to allow students to become comfortable with
Unix file I/O operations,
make
and makefiles, and building libraries.
Resources
FAQ
(submission instructions and other useful stuff)
If you are not in our course email list, please subscribe to the
cspp51081 email list here:
http://mailman.cs.uchicago.edu/mailman/listinfo/cspp51081
Lecture 3 is the primary source for this lab.
All work should be done on a machine in the department's
Linux cluster. You can refer to ssh for
more
information on
how to log into a remote machine.
Marks Distribution
Exercise 1 |
5 points |
Exercise 2 |
5 points |
Exercise 3 |
6 points |
TOTAL |
16 points |
LAB 3
- EXERCISE 1: Implement the stat command in
C. Write a C program
called my_stat that implements the default behavior
of the UNIX stat command. Your output
should exactly match the default output of the stat
command. For instance:
File: "/usr/bin/stat"
Size: 7212
Blocks:
16
IO Block: 4096 Regular File
Device: 303h/771d Inode:
311082 Links:
1
Access: (0755/-rwxr-xr-x) Uid:
( 0/
root) Gid: (
0/ root)
Access: Wed Jul 10 23:00:30 2002
Modify: Wed Aug 8 10:49:41 2001
Change: Thu Apr 4 12:34:28 2002
Hint: "man stat" and "man 2 stat".
Again, the output of your stat program with the option should
match the output of the original stat program. For those of
you using
Molay's text, you will find his discussion on the stat call on pages
82ff
quite helpful in using the stat call and formatting the output
correctly.
For extra credit (5 points): Implement
handling of any one of the following two
flags:
-L (follow Links)
-c --format (implement a subset of 5 of the formatting options (your
choice which 5))
DELIVERABLES: your source file(s)
(e.g., mystat.c), a makefile, and a README as
described below in Deliverables
- EXERCISE 2: Implement a simplified version of the UNIX
program "cp"
, which copies one file to another. Call the file filecopy.c.
This
version copies only files, it does not permit the second argument to be
a
directory. We also ignore all options in command "filecopy".
Please follow these steps:
- Read the chapters in the Kerrisk text as per the syllabus.
- Read and understand these examples:
read.c and
write.c; (man 2 read; man 2 write if necessary)
- Use Low-level File Access functions such as creat,
open, read,
write, close, etc.) to implement this command
- You must handle the situation when the second argument
is the same
as the first, i.e., you should not allow a file to be copied over
itself.
Handle this situation intelligently.
- Acctually, you only need to integrate above examples
and add only serveral lines.
DELIVERABLES: your source file(s)
(e.g., filecopy.c), a makefile, and a README as
described below in Deliverables
-
EXERCISE
3: Build Static and Shared Libraries using a
Makefile
-
Read "make and Makefile" session in Chaper 10 of BLP
textbook
if you're using that text. Certainly refer to the online GNU
documentation
for GNU
make.
- Write 3 very simple c source files: hello.c,
here.c, bye.c, mylib.h
. In hello.c, a function should print out
"Hello!"; in here.c
, another function should print out "I am here!"; and yet another
function
should print out "Bye bye!" in bye.c. So,
actually, in each code
file, there is only one C printf statement. mylib.h should include
prototypes for the 3 functions you will be writing.
- Write a main.c to call above 3
functions so that the result should display like following
"Hello!"
"I am here!"
"Bye bye!"
Please note there is no C printf statement in main.c;
- Read and understand this
makefile first and write your own Makefile called Makefile1
to compile them and build a static lib called mylib.a,
comprised
of hello.c, here.c, and bye.c (but NOT main.c).
Review "shared libraries naming structure" and
"building a shared library" in lecture 3.
- Read and understand this makefile
first and write your own makefile called Makefile2
to compile them and build a shared lib with the soname of
libmylib.so.5. Make the minor
version .1 and the release .10. This will mean your actual
shared library
file will be named "libmylib.so.5.1.10" with .so links set accordingly.
Note that the two makefiles actually build separate
libraries based
on the same source code files, one static, the other shared.
DELIVERABLES: your source file(s)
(e.g., main.c, hello.c, bye.c, here.c, mylib.h), two makefiles, and a README as
described below in Deliverables
Deliverables
Carefully follow the steps below.
- Please create subdirectories ex1, ex2, and ex3, and place each
exercise's materials in its respective directory as described below:
- Directory ex1 will include at least three files.
- Source code files: You may organize your files anyway you
like.
- Makefile:
it must build your executable named mystat when we type make on the command line.
- README :
This file must include the correct command line syntax (including the
available option in you do the bonus), a brief explanation of your
command (including the extra functionality if you do the bonus) and the
transcript of one example of your command in the default mode (if you
are doing the bonus option, submit a second example showing your option
at work.)
- Directory ex2 will include at least three files.
- Source code files: You may organize your files anyway you
like.
- Makefile:
it must build your executable name filecopy when we type make on the command line.
- README :
This file must include the correct command line syntax of filecopy and a brief
explanation of what it does. There is no need to send a transcript of
it in action.
- Directory ex3 will include eight files.
- Source code files : main.c, hello.c, bye.c, here.c, mylib.h
- Two Make files, which will build an executable called : main
Makefile1 : for building static library
Makefile2 : for building shared library
- README : it will contain two lines, the exact command line argument to build an executable called main
make -f Makefile1 target
make -f Makefile2 target
- When you are finished with your directory you will create a
compressed archive file using tar
(this utility stores your directory as a single file, then compresses
its size.)
tar -czvf username.lab3.tgz username.lab3
- You will email your file to our grader as an attachment. She will send an
acknowledgement that your assignment has been
received.
- Maria Power