CMSC216 HW10: UNIX I/O and Directories
- Due: 11:59pm Sun 23-Nov-2025 on Gradescope
- Approximately 0.83% of total grade
CODE DISTRIBUTION: hw10-code.zip
- Download the code distribution every HW
- See further setup instructions below
CHANGELOG: Empty
Table of Contents
1 Rationale
Familiarity with basic File and Directory operations are essential to systems programming. This exercise shows examples of performing file I/O along with a common pattern accumulating all data read into a single contiguous block of memory.
Additionally a demonstration program covers how to traverse files in a directory programmatically and gather information about them. This requires the use of several system calls which are introduced.
1.1 Associated Reading
Bryant and O'Hallaron Chapter 10 covers the system calls used in this
homework including open() / close(), read() / write(), stat() and so
forth. Section 10.7 in particular covers the opendir() / readdir()
calls which allow traversal of directory contents, the focus of one of
the HW problems.
Consult C documentation for information on the malloc(), free() and
realloc() functions to understand their use. In particular,
realloc() is a useful standard C function that is worth knowing about.
1.2 Grading Policy
Credit for this HW is earned by taking the associated HW Quiz which is
linked under Gradescope. The quiz will ask similar questions as
those that are present in the QUESTIONS.txt file and those that
complete all answers in QUESTIONS.txt should have no trouble with
the quiz.
Homework and Quizzes are open resource/open collaboration. You must submit your own work but you may freely discuss HW topics with other members of the class.
See the full policies in the course syllabus.
2 Codepack
The codepack for the HW contains the following files:
| File | State | Description |
|---|---|---|
QUESTIONS.txt |
Edit | Answer questions in the file to complete the HW |
append_all.c |
Edit | Problem 1 file to analyze/edit |
dirops.c |
Edit | Problem 2 file to analyze/edit |
3 Questions
________________
HW10 QUESTIONS
________________
Write your answers to the questions below directly in this text file to
prepare for the associated quiz. Credit for the HW is earned by
completing the associated online quiz on Gradescope.
PROBLEM 1: `append_all.c'
=========================
Examine the code in `append_all.c' which makes use of the `read()' and
`write()' I/O system calls in an interesting pattern.
A
~
Compile and run the program and experiment with entering data into it
and pressing 'Enter'. Start with simple single letter inputs and then
extend you inputs to longer strings. Show your session and explain
why read() always seems to read one more character than you type.
B
~
Describe the initialize size of the array `input' in `append_all.c'
and how it changes over the run of the program.
- What standard C function is used to initially allocate memory for
`input'? How does it work and how much space does `input' initially
occupy?
- what C function is used to alter its size? How does it work?
C
~
Restart the `append_all.c' program and type the specific input below
in at the prompts. Show the output produced and describe *exactly how
many read() calls* result from entering this input. This should
solidify your understanding of the main loop in the program.
,----
| > 123456
`----
D
~
In append_all.c, the read call is followed by a commented line:
,----
| int nread = read(STDIN_FILENO, input+cur_pos, max_read); // perform read()
| // int nread = read(STDIN_FILENO, input, max_read); // this read() call would be an error: why?
`----
This commented line contains a common error for those new to `read()'
system call or input accumulation in general.
Comment the current read() call and uncomment the line marked as an
error so that the code reads:
,----
| // int nread = read(STDIN_FILENO, input+cur_pos, max_read); // perform read()
| int nread = read(STDIN_FILENO, input, max_read); // this read() call would be an error: why?
`----
Recompile the program and run it entering various inputs. Describe why
this line is error and relate it to what the program now erroneously
produces for output.
PROBLEM 2: `dirops.c'
=====================
A
~
Examine the source code of `dirops.c' closely. It makes use of a
variety of system calls to produce a semi-interesting effect. Compile
and run it several times. Describe the overall intent of the program
based on its output and the code you understand.
B
~
What set of system calls is used by the program to determine all the
files in the current directory? Describe briefly how these calls work
together.
C
~
Identify the system call that `dirops.c' uses to find the sizes of
files. Describe briefly how this call works.
D
~
The following line sets up the read/write permissions that the copied
file will have.
,----
| mode_t perms = S_IRUSR | S_IWUSR;
`----
Modify this line so that the copied file is readable by the group in
addition to the other permissions present.
/Optional challenge:/ Set the permissions to be identical to the
original file. `stat()' is one way to find out the permissions for the
original file and the manual page for the system call shows the fields
of the `struct stat', one of which shows the permissions/mode. Note:
you may need to use `man 2 stat' to get the system call version of
`stat' rather than the command line utility of the same name.
E
~
`dirops.c' contains a subtle bug in the following bit of code towards
the end of the file.
,----
| while( (nbytes = read(infd, buf, BUFSIZE)) > 0){
| write(outfd, buf, BUFSIZE);
| }
`----
You should observe that every time program is run, it will identify a
copied file as the largest and make another copy due to this bug. It
may help to examine the ends of the copied files with the `tail
file.txt.copy' command which will show the last 10 lines.
Explain what is wrong with the loop and paste a fixed version below.