summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-24 22:19:40 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-24 22:19:40 +0000
commitf8d9c51a364eb941f6651d1ea7d4977cb5b83ca7 (patch)
tree49ea1fabc9efa2f7229fe3fabd46a94a9cb54931 /util/libreboot-utils/lib
parentb8a045ef86ab439accc0717daeb841aa065b86d6 (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')
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c29
-rw-r--r--util/libreboot-utils/lib/state.c2
2 files changed, 22 insertions, 9 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;
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index e3cb0890..a1120906 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -128,7 +128,7 @@ xstart(int argc, char *argv[])
if (us.f.dirfd < 0)
err_no_cleanup(errno, "%s: open dir", dir);
- if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir) < 0)
+ if (new_tmpfile(&us.f.tmp_fd, &us.f.tname, dir, NULL) < 0)
err_no_cleanup(errno, "%s", us.f.tname);
if (fs_dirname_basename(us.f.tname,