mirror of
https://github.com/troglobit/libite.git
synced 2026-11-17 19:30:17 +00:00
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:
1 parent
8befd09cf5
commit
0013f4fcf4
4 files changed
+80
-6
No files matched your search
+17
-6
@@ -83,6 +83,20 @@ char *readsnf(char *line, size_t len, const char *fmt, ...)
|
|||||||
return ln;
|
return ln;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Close fp, keeping errno from a failed fprintf(), its result in rc */
|
||||||
|
static int wclose(FILE *fp, int rc)
|
||||||
|
{
|
||||||
|
int err = errno;
|
||||||
|
|
||||||
|
if (rc >= 0)
|
||||||
|
return fclose(fp);
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
errno = err;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a string buffer to a file composed from fmt and optional args.
|
* Write a string buffer to a file composed from fmt and optional args.
|
||||||
* @param str Pointer to string buffer, may be multiple lines.
|
* @param str Pointer to string buffer, may be multiple lines.
|
||||||
@@ -102,8 +116,7 @@ int writesf(const char *str, const char *mode, const char *fmt, ...)
|
|||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
fprintf(fp, "%s\n", str);
|
return wclose(fp, fprintf(fp, "%s\n", str));
|
||||||
return fclose(fp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,8 +205,7 @@ int writellf(long long value, const char *mode, const char *fmt, ...)
|
|||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
fprintf(fp, "%lld\n", value);
|
return wclose(fp, fprintf(fp, "%lld\n", value));
|
||||||
return fclose(fp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -216,8 +228,7 @@ int writedf(int value, const char *mode, const char *fmt, ...)
|
|||||||
if (!fp)
|
if (!fp)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
fprintf(fp, "%d\n", value);
|
return wclose(fp, fprintf(fp, "%d\n", value));
|
||||||
return fclose(fp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
/makepath
|
/makepath
|
||||||
/pidfile
|
/pidfile
|
||||||
/printhdr
|
/printhdr
|
||||||
|
/procval
|
||||||
/progress
|
/progress
|
||||||
/rsync
|
/rsync
|
||||||
/runbg
|
/runbg
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ TESTS += lfile
|
|||||||
TESTS += makepath
|
TESTS += makepath
|
||||||
TESTS += pidfile
|
TESTS += pidfile
|
||||||
TESTS += printhdr
|
TESTS += printhdr
|
||||||
|
TESTS += procval
|
||||||
TESTS += progress
|
TESTS += progress
|
||||||
TESTS += rsync
|
TESTS += rsync
|
||||||
TESTS += runbg
|
TESTS += runbg
|
||||||
|
|||||||
@@ -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:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user