diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-24 22:19:40 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-24 22:19:40 +0000 |
| commit | f8d9c51a364eb941f6651d1ea7d4977cb5b83ca7 (patch) | |
| tree | 49ea1fabc9efa2f7229fe3fabd46a94a9cb54931 /util/libreboot-utils/lib/mkhtemp.c | |
| parent | b8a045ef86ab439accc0717daeb841aa065b86d6 (diff) | |
util/mkhtemp: template support on util
just add a template like yo uwould on other mktemp.
it works perfectly now.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/mkhtemp.c')
| -rw-r--r-- | util/libreboot-utils/lib/mkhtemp.c | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c index cd4a9cde..6b4898fd 100644 --- a/util/libreboot-utils/lib/mkhtemp.c +++ b/util/libreboot-utils/lib/mkhtemp.c @@ -35,16 +35,20 @@ /* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */ int -new_tmpfile(int *fd, char **path, char *tmpdir) +new_tmpfile(int *fd, char **path, char *tmpdir, + const char *template) { - return new_tmp_common(fd, path, MKHTEMP_FILE, tmpdir); + return new_tmp_common(fd, path, MKHTEMP_FILE, + tmpdir, template); } /* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */ int -new_tmpdir(int *fd, char **path, char *tmpdir) +new_tmpdir(int *fd, char **path, char *tmpdir, + const char *template) { - return new_tmp_common(fd, path, MKHTEMP_DIR, tmpdir); + return new_tmp_common(fd, path, MKHTEMP_DIR, + tmpdir, template); } /* note: tmpdir is an override of TMPDIR or /tmp or /var/tmp */ @@ -65,7 +69,7 @@ new_tmpdir(int *fd, char **path, char *tmpdir) */ int new_tmp_common(int *fd, char **path, int type, - char *tmpdir) + char *tmpdir, const char *template) { #if defined(PATH_LEN) && \ (PATH_LEN) >= 256 @@ -75,7 +79,8 @@ new_tmp_common(int *fd, char **path, int type, #endif struct stat st; - char suffix[] = "tmp.XXXXXXXXXX"; + const char *suffix; + size_t suffix_len; size_t dirlen; size_t destlen; @@ -138,10 +143,18 @@ new_tmp_common(int *fd, char **path, int type, if (*tmpdir != '/') goto err; + if (template != NULL) + suffix = template; + else + suffix = "tmp.XXXXXXXXXX"; + + if (slen(suffix, maxlen, &suffix_len) < 0) + goto err; + /* sizeof adds an extra byte, useful * because we also want '.' or '/' */ - destlen = dirlen + sizeof(suffix); + destlen = dirlen + 1 + suffix_len; if (destlen > maxlen - 1) { errno = EOVERFLOW; goto err; @@ -155,7 +168,7 @@ new_tmp_common(int *fd, char **path, int type, memcpy(dest, tmpdir, dirlen); *(dest + dirlen) = '/'; - memcpy(dest + dirlen + 1, suffix, sizeof(suffix) - 1); + memcpy(dest + dirlen + 1, suffix, suffix_len); *(dest + destlen) = '\0'; fname = dest + dirlen + 1; |
