From b3cd4386fac093c3b013c6663c54152ae4aeaef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 1 Dec 2015 19:21:50 +0000 Subject: [PATCH] In t_fopen(), resolve the full path to the file before opening it. In t_fprintf(), immediately flush the file after writing to it. git-svn-id: svn+ssh://svn.openpam.org/svn/openpam/trunk@871 185d5e19-27fe-0310-9dcf-9bff6b9f3609 --- t/t_file.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/t/t_file.c b/t/t_file.c index 4bda36a..d3205b0 100644 --- a/t/t_file.c +++ b/t/t_file.c @@ -55,21 +55,25 @@ struct t_file * t_fopen(const char *filename) { struct t_file *tf; - int fd; + int fd, dynfn; if ((tf = calloc(sizeof *tf, 1)) == NULL) err(1, "%s(): calloc()", __func__); if (filename) { - if ((tf->name = strdup(filename)) == NULL) - err(1, "%s(): strdup()", __func__); + dynfn = 0; } else { - asprintf(&tf->name, "%s.%lu.%p.tmp", + asprintf(&filename, "%s.%lu.%p.tmp", t_progname, (unsigned long)getpid(), (void *)tf); - if (tf->name == NULL) + if (filename == NULL) err(1, "%s(): asprintf()", __func__); + dynfn = 1; } - if ((fd = open(tf->name, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0) + if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0) err(1, "%s(): %s", __func__, tf->name); + if ((tf->name = realpath(filename, NULL)) == NULL) + err(1, "%s(): realpath()", __func__); + if (dynfn) + free(filename); if ((tf->file = fdopen(fd, "r+")) == NULL) err(1, "%s(): fdopen()", __func__); if ((tf->next = tflist) != NULL) @@ -92,6 +96,8 @@ t_fprintf(struct t_file *tf, const char *fmt, ...) va_end(ap); if (ferror(tf->file)) err(1, "%s(): vfprintf()", __func__); + if (fflush(tf->file) != 0) + err(1, "%s(): fflush()", __func__); return (len); }