Add a safe wrapper for write and fix some warnings
1. Add a function writeall and make swrite wrap that function. Use either writeall or swrite, depending on whether we want to exit on errors or not. 2. Fix warnings when compiling with a higher optimisation level. (CFLAGS ?= -pipe -O3 -march=native -mtune=native -freorder-blocks-and-partition) Signed-off-by: hwangcc <hwangcc@csie.nctu.edu.tw>
This commit is contained in:
@ -8,8 +8,10 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "libi3.h"
|
||||
|
||||
@ -56,3 +58,30 @@ int sasprintf(char **strp, const char *fmt, ...) {
|
||||
va_end(args);
|
||||
return result;
|
||||
}
|
||||
|
||||
ssize_t writeall(int fd, const void *buf, size_t count) {
|
||||
int written = 0;
|
||||
ssize_t n = 0;
|
||||
|
||||
while (written < count) {
|
||||
n = write(fd, buf + written, count - written);
|
||||
if (n == -1) {
|
||||
if (errno == EINTR || errno == EAGAIN)
|
||||
continue;
|
||||
return n;
|
||||
}
|
||||
written += n;
|
||||
}
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
ssize_t swrite(int fd, const void *buf, size_t count) {
|
||||
ssize_t n;
|
||||
|
||||
n = writeall(fd, buf, count);
|
||||
if (n == -1)
|
||||
err(EXIT_FAILURE, "Failed to write %d", fd);
|
||||
else
|
||||
return n;
|
||||
}
|
||||
|
Reference in New Issue
Block a user