diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-06-05 16:51:31 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-06-07 17:38:49 +0200 |
commit | 3f4030399350a853803aad244812b83c1dda3a35 (patch) | |
tree | ed5dcc75d432ac7ae048d3ccdb7c584851ece99d /src | |
parent | c640710883f547cbdd4732e2a1ee6832348dee29 (diff) | |
download | systemd-3f4030399350a853803aad244812b83c1dda3a35.tar.gz |
util: allow mkdtemp_malloc() to be called with NULL pattern
In that case simply place the directory in /tmp.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fileio.c | 22 |
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; } |