Unformatted text preview: hould use the readline function  whenever you input a text line.
#include "csapp.h" ssize t readline(int fd, void *buf, size t maxlen);
returns: number of bytes read (0 if EOF), -1 on error The readline function has the same semantics as the fgets function in the C Standard I/O library. It reads the next text line from ﬁle fd (including the terminating newline character), copies it to memory location buf, and terminates the text line with the null character. Readline reads at most maxlen1 bytes, leaving room for the terminating zero. If the text line is longer than maxlen-1 bytes, then readline simply returns the ﬁrst maxlen-1 bytes of the line. Figure 12.16 shows the code for the readline package. It is somewhat subtle and needs to be studied carefully. The my read function copies the next character in the ﬁle to location ptr. It returns ½ on error (with errno) set appropriately, 0 on EOF, and 1 otherwise. Notice that my read is a static function, and thus is not visible to applications. To improve efﬁciency, my read maintains a static buffer...
View Full Document