For write, 0 may not mean an error at all. We need to instead check for the length not being the same.
With fwrite, because 0 could mean an error, but not always. We must check that we wrote the entire file!
Note that unlike write, fwrite's description according to POSIX does not mention returning a negative type at all. Nor does it say you can retry unlike write.
Finally, with write, we need to check less than 0, not 0, as 0 is a valid return and does not mean an error.
#endif
#include <stdio.h>
+#include <errno.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
while (s > 0) {
written = write(2, m, s);
- if (written <= 0)
+ if (written == 0)
return;
+ if (written < 0)
+ {
+ if (errno == EINTR)
+ continue;
+ return;
+ }
m += written;
s -= written;
}
mine = (struct write_fd_data *)client_data;
for (;;) {
bytesWritten = write(mine->fd, buff, length);
- if (bytesWritten <= 0) {
+ if (bytesWritten < 0) {
if (errno == EINTR)
continue;
archive_set_error(a, errno, "Write error");
size_t bytesWritten;
mine = client_data;
- for (;;) {
- bytesWritten = fwrite(buff, 1, length, mine->f);
- if (bytesWritten <= 0) {
- if (errno == EINTR)
- continue;
- archive_set_error(a, errno, "Write error");
- return (-1);
- }
- return (bytesWritten);
+ bytesWritten = fwrite(buff, 1, length, mine->f);
+ if (bytesWritten != length) {
+ archive_set_error(a, errno, "Write error");
+ return (-1);
}
+ return (bytesWritten);
}
static int
mine = (struct write_file_data *)client_data;
for (;;) {
bytesWritten = write(mine->fd, buff, length);
- if (bytesWritten <= 0) {
+ if (bytesWritten < 0) {
if (errno == EINTR)
continue;
archive_set_error(a, errno, "Write error");