Figure 1234 shows the code for a concurrent echo

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: lse if (read_cnt == 0) return 0; read_ptr = read_buf; } read_cnt--; *ptr = *read_ptr++; return 1; } ssize_t readline(int fd, void *buf, size_t maxlen) { int n, rc; char c, *ptr = buf; for (n = 1; n < maxlen; n++) { /* notice that loop starts at 1 */ if ( (rc = my_read(fd, &c)) == 1) { *ptr++ = c; if (c == ’\n’) break; /* newline is stored, like fgets() */ } else if (rc == 0) { if (n == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ } else return -1; /* error, errno set by read() */ } *ptr = 0; /* null terminate like fgets() */ return n; } code/src/csapp.c Figure 12.16: readline package: Reads a text line from a descriptor. Adapted from [77]. 12.4. UNIX FILE I/O #include <unistd.h> #include <sys/stat.h> int stat(const char *filename, struct stat *buf); 625 returns: 0 if OK, -1 on error The stat function takes as input a filename, such as /usr/dict/words, and fills in the members of a stat structure shown in Figure 12.17. We will need the st mode and st size members of the stat structure when we discuss...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online