Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Chapter 4 Exercises 141


system, and we’ve seen how to navigate the file system namespace. Athorough
understanding of all the properties of files and directories and all the functions that
operate on them is essential to UNIX programming.

Exercises


4.1 Modify the program in Figure4.3 to usestatinstead oflstat.What changes if one of the
command-line arguments is a symbolic link?
4.2 What happens if the file mode creation mask is set to 777 (octal)?Verify the results using
your shell’sumaskcommand.
4.3 Verify that turning offuser-read permission for a file that you own denies your access to
the file.
4.4 Run the program in Figure4.9aftercreating the filesfooandbar.What happens?
4.5 In Section 4.12, we said that a file size of 0 is valid for a regular file.We also said that the
st_sizefield is defined for directories and symbolic links. Should we ever see a file size
of 0 for a directory or a symbolic link?
4.6 Write a utility likecp( 1 )that copies a file containing holes, without writing the bytes of 0 to
the output file.
4.7 Note in the output from thels command in Section 4.12 that the filescoreand
core.copyhave different access permissions. If theumaskvalue didn’t change between
the creation of the two files, explain how the difference could have occurred.
4.8 When running the program in Figure4.16, we check the available disk space with thedf( 1 )
command. Why didn’t we use thedu( 1 )command?
4.9 In Figure4.20, we show theunlinkfunction as modifying the changed-status time of the
file itself. How can this happen?
4.10 In Section 4.22, how does the system’s limit on the number of open files affect themyftw
function?
4.11 In Section 4.22, our version offtwnever changes its directory.Modify this routine so that
each time it encounters a directory, it uses thechdirfunction to change to that directory,
allowing it to use the filename and not the pathname for each call tolstat.When all the
entries in a directory have been processed, executechdir("..").Comparethe time used
by this version and the version in the text.
4.12 Each process also has a root directory that is used for resolution of absolute pathnames.
This root directory can be changed with thechrootfunction. Look up the description for
this function in your manuals. When might this function be useful?
4.13 How can you set only one of the two time values with theutimesfunction?
4.14 Some versions of thefinger( 1 )command output ‘‘New mail received ...’’and ‘‘unread
since ...’’where... arethe corresponding times and dates. How can the program determine
these two times and dates?
Free download pdf