util/mkhtemp: add directory override (-p) option.

-p dir

to override TMPDIR

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-24 20:06:09 +00:00
parent 88ff5f7380
commit 75f03ea696

View File

@@ -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());
}/*