Report write errors from writesf(), writellf(), and writedf()

The write functions return success when nothing was written, e.g., on
a stream that is not open for writing:

    writesf("data", "r", "/tmp/file");

They return the result of fclose() and drop the one from fprintf().

Return -1 when fprintf() fails, with errno from the failed write.

Closes #25
This commit is contained in:
omrzk authored and Joachim Wiberg committed 2026-11-16 14:13:07 +01:00
1 parent 8befd09cf5
commit 0013f4fcf4
4 files changed
+80 -6

No files matched your search

+1
View File
@@ -15,6 +15,7 @@
/makepath
/pidfile
/printhdr
/procval
/progress
/rsync
/runbg
+1
View File
@@ -30,6 +30,7 @@ TESTS += lfile
TESTS += makepath
TESTS += pidfile
TESTS += printhdr
TESTS += procval
TESTS += progress
TESTS += rsync
TESTS += runbg
+61
View File
@@ -0,0 +1,61 @@
#include <errno.h>
#include "check.h"
int main(void)
{
char path[] = "/tmp/libite-procval-XXXXXX";
char buf[32];
long long llval;
int fd, val;
fd = mkstemp(path);
fail_unless(fd >= 0);
close(fd);
fail_unless(writesf("first", "w", "%s", path) == 0);
fail_unless(writesf("second", "a", "%s", path) == 0);
fail_unless(readsnf(buf, sizeof(buf), "%s", path) == buf);
fail_unless(!strcmp(buf, "first"));
fail_unless(writesf("42", "w", "%s", path) == 0);
fail_unless(readdf(&val, "%s", path) == 0);
fail_unless(val == 42);
fail_unless(writedf(-7, "w", "%s", path) == 0);
fail_unless(readdf(&val, "%s", path) == 0);
fail_unless(val == -7);
fail_unless(writellf(1LL << 40, "w", "%s", path) == 0);
fail_unless(readllf(&llval, "%s", path) == 0);
fail_unless(llval == 1LL << 40);
/* Not open for writing, fprintf() fails but fclose() does not */
errno = 0;
fail_unless(writesf("data", "r", "%s", path) == -1);
fail_unless(errno == EBADF);
fail_unless(writedf(1, "r", "%s", path) == -1);
fail_unless(writellf(1, "r", "%s", path) == -1);
if (!access("/dev/full", W_OK)) {
char big[4 * BUFSIZ];
memset(big, 'x', sizeof(big) - 1);
big[sizeof(big) - 1] = 0;
errno = 0;
fail_unless(writesf(big, "w", "/dev/full") == -1);
fail_unless(errno == ENOSPC);
}
fail_unless(unlink(path) == 0);
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* compile-command: "make procval && ./procval"
* End:
*/