From 75f03ea696c20af56f20e2327c9c692683c6810a Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Tue, 24 Mar 2026 20:06:09 +0000 Subject: util/mkhtemp: add directory override (-p) option. -p dir to override TMPDIR Signed-off-by: Leah Rowe --- util/libreboot-utils/mkhtemp.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'util/libreboot-utils') diff --git a/util/libreboot-utils/mkhtemp.c b/util/libreboot-utils/mkhtemp.c index c289c5b4..1f7c8b79 100644 --- a/util/libreboot-utils/mkhtemp.c +++ b/util/libreboot-utils/mkhtemp.c @@ -67,6 +67,11 @@ 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; @@ -87,26 +92,34 @@ main(int argc, char *argv[]) #if defined(__OpenBSD__) && defined(OpenBSD) #if (OpenBSD) >= 509 if (pledge("stdio flock rpath wpath cpath", NULL) == -1) - err_no_cleanup(errno, "pledge, main"); + goto err_usage; #endif #endif - while ((c = - getopt(argc, argv, "d")) != -1) { + while ((c = + getopt(argc, argv, "dp:")) != -1) { - switch(c) { + switch (c) { case 'd': - type = MKHTEMP_DIR; break; - default: - err_no_cleanup(EINVAL, - "usage: mkhtemp [-d]\n"); + case 'p': + tmpdir = optarg; + break; + + default: + goto err_usage; } } - if (new_tmp_common(&fd, &s, type, NULL) < 0) + if (optind < argc) + template = argv[optind]; + if (optind + 1 < argc) + err_no_cleanup(EINVAL, + "usage: mkhtemp [-d] [-p dir] [template]\n"); + + if (new_tmp_common(&fd, &s, type, tmpdir) < 0) err_no_cleanup(errno, "%s", s); #if defined(__OpenBSD__) && defined(OpenBSD) @@ -128,6 +141,10 @@ main(int argc, char *argv[]) printf("%s\n", s); return EXIT_SUCCESS; + +err_usage: + err_no_cleanup(EINVAL, + "usage: %s [-d] [-p dir] [template]\n", getnvmprogname()); }/* -- cgit v1.2.1