void run (int numBlocks) { char buf [8]; for (int blockno = 0; blockno < numBlocks; blockno++) { syncRead (buf, sizeof (buf), blockno); handleRead (buf, sizeof (buf), blockno); } }
/* Read a line making sure that every char will not require more than 'timeout' * milliseconds to be read. * * On success the number of bytes read is returned, otherwise -1. * On success the string is always correctly terminated with a 0 byte. */ ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout) { ssize_t nread = 0; size--; while(size) { char c; if (syncRead(fd,&c,1,timeout) == -1) return -1; if (c == '\n') { *ptr = '\0'; if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0'; return nread; } else { *ptr++ = c; *ptr = '\0'; nread++; } } return nread; }