diff --git a/src/procval.c b/src/procval.c index eb42091..fa72bd3 100644 --- a/src/procval.c +++ b/src/procval.c @@ -83,6 +83,20 @@ char *readsnf(char *line, size_t len, const char *fmt, ...) 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. * @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) return -1; - fprintf(fp, "%s\n", str); - return fclose(fp); + return wclose(fp, fprintf(fp, "%s\n", str)); } /** @@ -192,8 +205,7 @@ int writellf(long long value, const char *mode, const char *fmt, ...) if (!fp) return -1; - fprintf(fp, "%lld\n", value); - return fclose(fp); + return wclose(fp, fprintf(fp, "%lld\n", value)); } @@ -216,8 +228,7 @@ int writedf(int value, const char *mode, const char *fmt, ...) if (!fp) return -1; - fprintf(fp, "%d\n", value); - return fclose(fp); + return wclose(fp, fprintf(fp, "%d\n", value)); } diff --git a/test/.gitignore b/test/.gitignore index 6701e84..50ce41a 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -15,6 +15,7 @@ /makepath /pidfile /printhdr +/procval /progress /rsync /runbg diff --git a/test/Makefile.am b/test/Makefile.am index 36e1d2c..ef0e8dc 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -30,6 +30,7 @@ TESTS += lfile TESTS += makepath TESTS += pidfile TESTS += printhdr +TESTS += procval TESTS += progress TESTS += rsync TESTS += runbg diff --git a/test/procval.c b/test/procval.c new file mode 100644 index 0000000..f98187a --- /dev/null +++ b/test/procval.c @@ -0,0 +1,61 @@ +#include +#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: + */