3 ms·
Unrelated: the code is equivalent to read(fd, buf, RECORD_SIZE) that may read less than RECORD_SIZE. The function should read in a loop, to fulfill its contrac
by d0mine 10y ago
Unrelated: the code is equivalent to read(fd, buf, RECORD_SIZE) that may read less than RECORD_SIZE.
The function should read in a loop, to fulfill its contract (read a record).
#include <unistd.h>
ssize_t read_record(int fd, void* buf, size_t record_size, size_t* written) {
for (*written = 0; *written < record_size; ) {
ssize_t n = read(fd, (char*)buf + *written, record_size - *written);
if (n < 1)
return n; /* error or EOF */
*written += n;
}
return *written;
}