Directories are special files that store the names and locations of
the related files/directories — itself, its parent, and all
of its children (i.e., the directory's contents).
You can take CSE 451 to learn more about the directory structure,
but for this class, you can access directory information in C/C++
using the struct dirent structure and the POSIX
library functions found in dirent.h.
As discussed in lecture, C standard library I/O is implemented using system calls (POSIX) with an internal buffer to optimize performance. In this exercise, we will focus on buffered reading, where chunks of data from the file are stored in an internal buffer so that requests from the user can be returned more quickly from the internal buffer in memory rather than making a request to the file on disk each time. We must manage the internal buffer properly so that it is invisible to the client:
This exercise will be broken into multiple parts:
We have provided you with the following seven source files, which can be downloaded from or with the commands:
$ wget https://courses.cs.washington.edu/courses/cse333/23sp/exercises/ex4_files/<filename>
ex4.c — Contains an empty
main for the directory program, but does have
helper functions to handle the major C-string operations.ro_file.h — Provides the public
interface for our file I/O module, which includes a file
management information data type and file I/O operations.
Note that struct ro_file_st is defined in
ro_file.c.ro_file.c — Implementation file for
our file I/O module that you will need to complete.test_ro_file.c — Implements an
executable that makes use of the file I/O operations provided by
ro_file.h.test.txt and test2.txt
— Two text files used in test_ro_file.c.Makefile — Provided for your
convenience in compiling the executables ex4 and
test_ro_file.ex4.c and ro_file.c
so you should not modify the other source files.
Do feel free to modify the test files, though, or create your own.
Write main in ex4.c.
Your program should:
stdout.
Do NOT add any additional characters or formatting
(e.g., don't add newlines between the contents of
different files)!We have not provided any tests for you in this part, but we encourage you to test it on various inputs.
struct ro_file_st definition in
ro_file.c, and then read the function comments in
ro_file.h.ro_file.c.test_ro_file.c.
Now go back and revise ex4.c to replace the
C standard library functions for file reading with calls to the
file I/O module.
You are allowed to keep using C standard library functions to write
to stdout.
Recommendations:
ex4.c from Step 1
somewhere for partial credit opportunities in the event that you
don't finish debugging ro_file.c.You will be reading in user input from a command-line argument. You should handle various inputs from the user, which may be in an unexpected format. For each input, you should take some time to reason through your options for handling it gracefully (i.e., without unexpectedly crashing), decide which one seems "best", and document your decision in your code. Refer to the Step 1 instuctions above for some directory name considerations.
There are various levels of testing to be done in this exercise, including (1) user input testing, (2) input file testing, and (3) testing individual functions that you've written (i.e., debugging). For input file testing, you are encouraged to write your own files (.txt and otherwise) but can also find plenty of .
We are restricting ourselves to only POSIX library functions for
finishing our ro_file implementation; you are NOT
allowed to use functions from cstdio.
The POSIX functions you will likely be most interested in are:
open(), close(), read(),
lseek(), opendir(),
readdir(), and closedir().
Library functions (e.g., C standard library and POSIX)
have many possible errors that may arise during execution.
It is your responsibility to make sure that you handle these errors
correctly by retrying in the case of recoverable errors
(EAGAIN and EINTR) and returning an error
status in the case of a non-recoverable error.
Make sure that you clean up system resources in all
possible cases.
For the sake of our autograder, you may not modify
ro_file.h.
You are free to modify any other files as you see fit for design
and/or testing purposes, but recall that you will only be
submitting ex4.c and ro_file.c.
If you add internal data types, constants, or functions to
ro_file.c, you should document your changes and
explain why you made them.
Since you have been provided code, it is important to match the
given style for consistency, including commenting style,
indentation, data types (e.g., size_t,
ssize_t, off_t), and error checking.
In addition, pay particular attention to comments, as some have been provided to help you plan your implementation but may or may not make sense to keep in your submitted version. Remember that comments should be up-to-date and meaningful; don't assume that all provided comments can be left as-is.
You will submit:
ex4.c and ro_file.c.
Your code must:
attu, or CSE home VM).gcc and valgrind).Makefile..c
files with your name(s) and CSE or UW email address(es).cpplint.py --clint).Submit your code on . Don't forget to add your partner if you have one.