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/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/mkhtemp.c')
| -rw-r--r-- | util/libreboot-utils/mkhtemp.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c index 3fa4819f..4408f763 100644 --- a/util/libreboot-utils/mkhtemp.c +++ b/util/libreboot-utils/mkhtemp.c @@ -67,27 +67,26 @@ int main(int argc, char *argv[]) { - const char usage_str[] = "usage: %s [-d] [-p dir] [template]"; - - char *tmpdir = NULL; - char *template = NULL; - - char *s = NULL; - int fd = -1; - char c; - int type = MKHTEMP_FILE; - size_t len; - - char *rp; - #if defined (PATH_LEN) && \ (PATH_LEN) >= 256 size_t maxlen = PATH_LEN; #else size_t maxlen = 4096; #endif + size_t len; + size_t tlen; + size_t xc = 0; + char *tmpdir = NULL; + char *template = NULL; + char *p; + char *s = NULL; + char *rp; char resolved[maxlen]; + char c; + + int fd = -1; + int type = MKHTEMP_FILE; if (lbgetprogname(argv[0]) == NULL) err_no_cleanup(errno, "could not set progname"); @@ -123,6 +122,19 @@ main(int argc, char *argv[]) err_no_cleanup(EINVAL, "usage: mkhtemp [-d] [-p dir] [template]\n"); + /* custom template e.g. foo.XXXXXXXXXXXXXXXXXXXXX */ + if (template != NULL) { + if (slen(template, maxlen, &tlen) < 0) + err_no_cleanup(EINVAL, + "invalid template"); + + for (p = template + tlen; + p > template && *--p == 'X'; xc++); + + if (xc < 6) + err_no_cleanup(EINVAL, + "template must end in at least 6 X"); + } /* user supplied -p PATH - WARNING: * this permits symlinks, but only here, @@ -140,7 +152,8 @@ main(int argc, char *argv[]) tmpdir = resolved; } - if (new_tmp_common(&fd, &s, type, tmpdir) < 0) + if (new_tmp_common(&fd, &s, type, + tmpdir, template) < 0) err_no_cleanup(errno, "%s", s); #if defined(__OpenBSD__) && defined(OpenBSD) |
