summaryrefslogtreecommitdiffstats
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 5837163fed..11223d5db8 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -1560,21 +1560,29 @@ int read_nul_string(FILE *f, char **ret) {
}
int mkdtemp_malloc(const char *template, char **ret) {
- char *p;
+ _cleanup_free_ char *p = NULL;
+ int r;
- assert(template);
assert(ret);
- p = strdup(template);
+ if (template)
+ p = strdup(template);
+ else {
+ const char *tmp;
+
+ r = tmp_dir(&tmp);
+ if (r < 0)
+ return r;
+
+ p = strjoin(tmp, "/XXXXXX");
+ }
if (!p)
return -ENOMEM;
- if (!mkdtemp(p)) {
- free(p);
+ if (!mkdtemp(p))
return -errno;
- }
- *ret = p;
+ *ret = TAKE_PTR(p);
return 0;
}