C Programming HW
CS 270 Systems Programming Fall 2021
Homework 7
Out: Thu 28 October 2021 Due: Wed 3 November 2021
These problems are based on Sections 10.5, 10.9 through 10.11, and Chapter 11 in your textbook Computer Systems: A Programmer’s Perspective, 3rd edition. You are responsible for familiarizing yourself with the content of those sections; it is recommended that you work all the practice problems as well. (Do NOT turn in the practice problems.) This is an individual homework. Whatever you turn in must be your own work. Each part of each problem is worth up to 2 points—1 point for giving some answer, and a second point for the correctness of your answer. (The possible scores on each part are 0, 1 and 2.)
Problem 0. Assume strfile is a stream that references a regular file stored on disk. Write a line or two of code that would accomplish the following I/O operations. (Hint: man fseek.
a. Write the ASCII character ’e’ as the 100th byte of the regular file referenced by strfile.
b. Put the last byte of strfile in the char c.
Problem 1. As discussed in class, directories in Unix are structured files that contain a sequence of (name, inode number) pairs. The functions opendir, readdir, and closedir, and the type DIR are provided for dealing with directories. Write a function
ino_t findindir(char *filename, char *dirpath);
that searches the directory identified by dirpath for the file with name filename. If found, it returns the inode number associated with the filename; otherwise (not found or other error), it returns 0.
Problem 2. The Rio functions presented in Section 10.5 of your textbook are useful to understand— you can re-use them in case you do any system programming in the future. However, each is intended for a certain purpose. Say which of the functions described in that section (rio_read, rio_readlineb, rio_readn) would be best to use for each of the following jobs?
a. Reading the file /etc/hosts, which lists hostname-address pairs, one per line.
b. Reading input from a user (terminal) via standard input.
c. Reading a long value from a pipe.
Problem 3. Some systems have files that are structured as a sequence of variable-length records. A record is stored as a 16-bit integer that encodes the (binary) length of the record in little-endian byte order, followed by that many bytes of record data. Records in this system may contain arbitrary data. Write a function rio_readLVrec (“LV” stands for “length-value”) that takes the same parameters as rio_readnb, reads a record into the caller-supplied usrbuf, and returns the length of the record. In case of error—e.g., the file ends in the middle of a record—your function should return -1. (Hint: call rio_readb twice.)
Problem 4. Some systems represent files containing variable-length records in a different way, using delimiters. For example, records might be separated by the special byte value 0x1E, which is called (what else?) RS, ’Record Separator’ (see the man page for ASCII. Which of the rio_read* functions in Section 10.5 would you use as the basis of a rio_readRSrec function, and how would you have to modify it?
1
Problem 5. Hypertext Transfer Protocol (HTTP) is the protocol used by web browsers to com- municate with web servers. Read Chapter 11 and look over the code for the Tiny web server, described in 11.6. In HTTP, messages are structured as a sequence of text lines, delimited by the two character sequence CR-NL ("\r\n", 0xd, 0xa).
a. The read_requesthdrs() function calls Rio_readlineb, which looks for a single-character delimiter to mark the end of a text line. Why does this work?
b. The boundary between the headers (control information for HTTP) and the content being transferred is marked by an empty line. How does read_requesthdr() recognize this bound- ary?
2